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.
251 lines
8.2 KiB
251 lines
8.2 KiB
8 years ago
|
/* -*- mode: c++ -*-
|
||
7 years ago
|
* Kaleidoscope-SpaceCadet -- Space Cadet Shift Extended
|
||
6 years ago
|
* Copyright (C) 2016, 2017, 2018 Keyboard.io, Inc, Ben Gemperline
|
||
8 years ago
|
*
|
||
6 years ago
|
* 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.
|
||
8 years ago
|
*
|
||
6 years ago
|
* 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.
|
||
8 years ago
|
*
|
||
6 years ago
|
* You should have received a copy of the GNU General Public License along with
|
||
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||
8 years ago
|
*/
|
||
|
|
||
8 years ago
|
#include <Kaleidoscope-SpaceCadet.h>
|
||
8 years ago
|
#include <kaleidoscope/hid.h>
|
||
8 years ago
|
|
||
8 years ago
|
namespace kaleidoscope {
|
||
6 years ago
|
namespace plugin {
|
||
8 years ago
|
|
||
7 years ago
|
//Constructor with input and output, and assume default timeout
|
||
7 years ago
|
SpaceCadet::KeyBinding::KeyBinding(Key input_, Key output_) {
|
||
7 years ago
|
input = input_;
|
||
|
output = output_;
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
//Constructor with all three set
|
||
7 years ago
|
SpaceCadet::KeyBinding::KeyBinding(Key input_, Key output_, uint16_t timeout_) {
|
||
7 years ago
|
input = input_;
|
||
|
output = output_;
|
||
|
timeout = timeout_;
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
//Space Cadet
|
||
7 years ago
|
SpaceCadet::KeyBinding * SpaceCadet::map;
|
||
7 years ago
|
uint16_t SpaceCadet::time_out = 1000;
|
||
7 years ago
|
bool SpaceCadet::disabled = false;
|
||
7 years ago
|
|
||
7 years ago
|
SpaceCadet::SpaceCadet() {
|
||
7 years ago
|
static SpaceCadet::KeyBinding initialmap[] = {
|
||
7 years ago
|
//By default, respect the default timeout
|
||
|
{Key_LeftShift, Key_LeftParen, 0}
|
||
|
, {Key_RightShift, Key_RightParen, 0}
|
||
|
//These may be uncommented, added, or set in the main sketch
|
||
|
/*,{Key_LeftGui,Key_LeftCurlyBracket, 250}
|
||
|
,{Key_RightAlt,Key_RightCurlyBracket, 250}
|
||
|
,{Key_LeftControl,Key_LeftBracket, 250}
|
||
|
,{Key_RightControl,Key_RightBracket, 250}*/
|
||
|
, SPACECADET_MAP_END
|
||
|
};
|
||
|
|
||
|
map = initialmap;
|
||
|
}
|
||
|
|
||
7 years ago
|
//Function to enable SpaceCadet behavior
|
||
|
void SpaceCadet::enable() {
|
||
|
disabled = false;
|
||
|
}
|
||
|
|
||
|
//Function to disable SpaceCadet behavior
|
||
|
void SpaceCadet::disable() {
|
||
|
disabled = true;
|
||
|
}
|
||
|
|
||
|
//Function to determine whether SpaceCadet is active (useful for Macros and other plugins)
|
||
|
bool SpaceCadet::active() {
|
||
|
return !disabled;
|
||
|
}
|
||
|
|
||
7 years ago
|
EventHandlerResult SpaceCadet::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
|
||
7 years ago
|
//Handle our synthetic keys for enabling and disabling functionality
|
||
7 years ago
|
if (mapped_key.raw >= kaleidoscope::ranges::SC_FIRST &&
|
||
|
mapped_key.raw <= kaleidoscope::ranges::SC_LAST) {
|
||
7 years ago
|
//Only fire the activate / deactivate on the initial press (not held or release)
|
||
|
if (keyToggledOn(key_state)) {
|
||
|
if (mapped_key == Key_SpaceCadetEnable) {
|
||
|
enable();
|
||
|
} else if (mapped_key == Key_SpaceCadetDisable) {
|
||
|
disable();
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
//if SpaceCadet is disabled, this was an injected key, it was NoKey,
|
||
|
//or if they key somehow came here without being either pressed or released,
|
||
|
//return the mapped key and just get out of here.
|
||
|
if (
|
||
|
disabled
|
||
|
|| (key_state & INJECTED)
|
||
|
|| mapped_key == Key_NoKey
|
||
|
|| (!keyIsPressed(key_state) && !keyWasPressed(key_state))
|
||
|
) {
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
7 years ago
|
}
|
||
|
|
||
|
// If a key has been just toggled on...
|
||
|
if (keyToggledOn(key_state)) {
|
||
7 years ago
|
|
||
7 years ago
|
//check to see if we found a valid key. Assume not valid.
|
||
|
bool valid_key = false;
|
||
|
|
||
|
//This will only set one key, and, if it isn't in our map, it clears everything for the non-pressed key
|
||
|
//Exit condition is if we reach the special SPACECADET_MAP_END sentinel
|
||
7 years ago
|
for (
|
||
|
uint8_t i = 0 ;
|
||
|
!(
|
||
7 years ago
|
map[i].input.raw == Key_NoKey.raw
|
||
|
&& map[i].output.raw == Key_NoKey.raw
|
||
|
&& map[i].timeout == 0
|
||
7 years ago
|
) ;
|
||
|
++i
|
||
|
) {
|
||
7 years ago
|
|
||
7 years ago
|
if (mapped_key.raw == map[i].input.raw) {
|
||
7 years ago
|
//The keypress was valid and a match. Mark it as flagged and reset the counter
|
||
7 years ago
|
map[i].flagged = true;
|
||
|
map[i].start_time = millis();
|
||
7 years ago
|
|
||
|
//yes, we found a valid key
|
||
|
valid_key = true;
|
||
|
|
||
7 years ago
|
} else {
|
||
7 years ago
|
//If the key entry we're looking at was flagged previously, add it to the
|
||
|
//report before we do anything else (this handles the situation where we
|
||
|
//hit another key after this -- if it's a modifier, we want the modifier
|
||
|
//key to be added to the report, for things like ctrl, alt, shift, etc)
|
||
|
if (map[i].flagged) {
|
||
|
handleKeyswitchEvent(map[i].input, row, col, IS_PRESSED | INJECTED);
|
||
|
}
|
||
|
|
||
|
//The keypress wasn't a match, so we need to mark it as not flagged and
|
||
|
//reset the timer for it to disable everything.
|
||
7 years ago
|
map[i].flagged = false;
|
||
|
map[i].start_time = 0;
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
//If we found a valid key in our map, we don't actually want to send anything.
|
||
|
//This gets around an issue in Windows if we map a SpaceCadet function on top
|
||
|
//of Alt -- sending Alt by itself activates the menubar. We don't want to send
|
||
|
//anything until we know that we're either sending the alternate key or we
|
||
|
//know for sure that we want to send the originally pressed key.
|
||
|
if (valid_key) {
|
||
7 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
7 years ago
|
}
|
||
|
|
||
|
//this is all we need to do on keypress, let the next handler do its thing too.
|
||
|
//This case assumes we weren't a valid key that we were watching, so we don't
|
||
|
//need to do anything else.
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// if the state is empty, that means that either an activating key wasn't pressed,
|
||
7 years ago
|
// or we used another key in the interim. in both cases, nothing special to do.
|
||
|
bool valid_key = false;
|
||
|
bool pressed_key_was_valid = false;
|
||
|
uint8_t index = 0;
|
||
|
|
||
7 years ago
|
//Look to see if any keys in our map are currently flagged.
|
||
|
//Exit condition is if we reach the special SPACECADET_MAP_END sentinel
|
||
7 years ago
|
for (
|
||
|
uint8_t i = 0 ;
|
||
|
!(
|
||
7 years ago
|
map[i].input.raw == Key_NoKey.raw
|
||
|
&& map[i].output.raw == Key_NoKey.raw
|
||
|
&& map[i].timeout == 0
|
||
7 years ago
|
);
|
||
7 years ago
|
++i
|
||
|
) {
|
||
7 years ago
|
|
||
7 years ago
|
//The key we're looking at was previously flagged (so perform action)
|
||
7 years ago
|
if (map[i].flagged) {
|
||
|
valid_key = true;
|
||
|
index = i;
|
||
|
}
|
||
7 years ago
|
|
||
|
//the key we're looking at was valid (in the map)
|
||
7 years ago
|
if (map[i].input.raw == mapped_key.raw) {
|
||
|
pressed_key_was_valid = true;
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
//If no valid mapped keys were pressed, simply return the key that
|
||
|
//was originally passed in.
|
||
7 years ago
|
if (!valid_key) {
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
7 years ago
|
}
|
||
|
|
||
|
//use the map index to find the local timeout for this key
|
||
7 years ago
|
uint16_t current_timeout = map[index].timeout;
|
||
7 years ago
|
//If that isn't set, use the global timeout setting.
|
||
|
if (current_timeout == 0) {
|
||
|
current_timeout = time_out;
|
||
|
}
|
||
|
|
||
7 years ago
|
//Check to determine if we have surpassed our timeout for holding this key
|
||
7 years ago
|
if ((millis() - map[index].start_time) >= current_timeout) {
|
||
7 years ago
|
// if we timed out, that means we need to keep pressing the mapped
|
||
|
// key, but we won't need to send the alternative key in the end
|
||
7 years ago
|
map[index].flagged = false;
|
||
|
map[index].start_time = 0;
|
||
7 years ago
|
|
||
|
//Just return this key itself (we won't run alternative keys check)
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
7 years ago
|
}
|
||
|
|
||
|
// If the key that was pressed isn't one of our mapped keys, just
|
||
|
// return. This can happen when another key is released, and that should not
|
||
|
// interrupt us.
|
||
|
if (!pressed_key_was_valid) {
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
7 years ago
|
}
|
||
|
|
||
|
// if a key toggled off (and that must be one of the mapped keys at this point),
|
||
|
// send the alternative key instead (if we were interrupted, we bailed out earlier).
|
||
|
if (keyToggledOff(key_state)) {
|
||
7 years ago
|
Key alternate_key = map[index].output;
|
||
7 years ago
|
|
||
|
//Since we are sending the actual key (no need for shift, etc),
|
||
7 years ago
|
//only need to send that key and not the original key.
|
||
|
|
||
|
//inject our new key
|
||
7 years ago
|
handleKeyswitchEvent(alternate_key, row, col, IS_PRESSED | INJECTED);
|
||
|
|
||
|
//Unflag the key so we don't try this again.
|
||
7 years ago
|
map[index].flagged = false;
|
||
|
map[index].start_time = 0;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
//Special case here for if we had a valid key that's continuing to be held.
|
||
|
//If it's a valid key, and it's continuing to be held, return NoKey.
|
||
|
//This prevents us from accidentally triggering a keypress that we didn't
|
||
|
//mean to handle.
|
||
|
if (valid_key) {
|
||
7 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
7 years ago
|
}
|
||
|
|
||
|
//Finally, as a final sanity check, simply return the passed-in key as-is.
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
|
}
|
||
|
|
||
6 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
6 years ago
|
kaleidoscope::plugin::SpaceCadet SpaceCadet;
|