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.
124 lines
3.2 KiB
124 lines
3.2 KiB
8 years ago
|
/* -*- mode: c++ -*-
|
||
8 years ago
|
* Kaleidoscope-Syster -- Symbolic input system
|
||
6 years ago
|
* Copyright (C) 2017, 2018, 2019 Keyboard.io, Inc
|
||
8 years ago
|
*
|
||
7 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
|
*
|
||
7 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
|
*
|
||
7 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-Syster.h>
|
||
5 years ago
|
#include "kaleidoscope/keyswitch_state.h"
|
||
|
#include "kaleidoscope/key_events.h"
|
||
8 years ago
|
|
||
8 years ago
|
#undef SYSTER
|
||
|
|
||
|
namespace kaleidoscope {
|
||
6 years ago
|
namespace plugin {
|
||
8 years ago
|
|
||
8 years ago
|
// --- state ---
|
||
8 years ago
|
char Syster::symbol_[SYSTER_MAX_SYMBOL_LENGTH + 1];
|
||
|
uint8_t Syster::symbol_pos_;
|
||
|
bool Syster::is_active_;
|
||
8 years ago
|
|
||
8 years ago
|
// --- helpers ---
|
||
8 years ago
|
#define isSyster(k) (k == kaleidoscope::ranges::SYSTER)
|
||
8 years ago
|
|
||
8 years ago
|
// --- api ---
|
||
8 years ago
|
void Syster::reset(void) {
|
||
|
symbol_pos_ = 0;
|
||
|
symbol_[0] = 0;
|
||
|
is_active_ = false;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
bool Syster::is_active(void) {
|
||
|
return is_active_;
|
||
|
}
|
||
|
|
||
8 years ago
|
// --- hooks ---
|
||
6 years ago
|
EventHandlerResult Syster::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr, uint8_t keyState) {
|
||
8 years ago
|
if (!is_active_) {
|
||
|
if (!isSyster(mapped_key))
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
8 years ago
|
|
||
7 years ago
|
if (keyToggledOn(keyState)) {
|
||
8 years ago
|
is_active_ = true;
|
||
8 years ago
|
systerAction(StartAction, NULL);
|
||
8 years ago
|
}
|
||
7 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
if (keyState & INJECTED)
|
||
|
return EventHandlerResult::OK;
|
||
8 years ago
|
|
||
7 years ago
|
if (isSyster(mapped_key)) {
|
||
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
if (mapped_key == Key_Backspace && symbol_pos_ == 0) {
|
||
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
if (keyToggledOff(keyState)) {
|
||
8 years ago
|
if (mapped_key == Key_Spacebar) {
|
||
|
for (uint8_t i = 0; i <= symbol_pos_; i++) {
|
||
6 years ago
|
handleKeyswitchEvent(Key_Backspace, UnknownKeyswitchLocation, IS_PRESSED | INJECTED);
|
||
5 years ago
|
kaleidoscope::Runtime.hid().keyboard().sendReport();
|
||
6 years ago
|
handleKeyswitchEvent(Key_Backspace, UnknownKeyswitchLocation, WAS_PRESSED | INJECTED);
|
||
5 years ago
|
kaleidoscope::Runtime.hid().keyboard().sendReport();
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
systerAction(EndAction, NULL);
|
||
8 years ago
|
|
||
8 years ago
|
symbol_[symbol_pos_] = 0;
|
||
|
systerAction(SymbolAction, symbol_);
|
||
8 years ago
|
reset();
|
||
8 years ago
|
|
||
7 years ago
|
return EventHandlerResult::EVENT_CONSUMED;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
if (keyToggledOn(keyState)) {
|
||
8 years ago
|
if (mapped_key == Key_Backspace) {
|
||
|
if (symbol_pos_ > 0)
|
||
|
symbol_pos_--;
|
||
8 years ago
|
} else {
|
||
8 years ago
|
const char c = keyToChar(mapped_key);
|
||
8 years ago
|
if (c)
|
||
8 years ago
|
symbol_[symbol_pos_++] = c;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
return EventHandlerResult::OK;
|
||
|
}
|
||
|
|
||
6 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
__attribute__((weak)) const char keyToChar(Key key) {
|
||
5 years ago
|
if (key.getFlags() != 0)
|
||
8 years ago
|
return 0;
|
||
8 years ago
|
|
||
5 years ago
|
switch (key.getKeyCode()) {
|
||
|
case Key_A.getKeyCode() ... Key_Z.getKeyCode():
|
||
|
return 'a' + (key.getKeyCode() - Key_A.getKeyCode());
|
||
|
case Key_1.getKeyCode() ... Key_0.getKeyCode():
|
||
|
return '1' + (key.getKeyCode() - Key_1.getKeyCode());
|
||
8 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
__attribute__((weak)) void systerAction(kaleidoscope::plugin::Syster::action_t action, const char *symbol) {
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
kaleidoscope::plugin::Syster Syster;
|