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/plugins/Kaleidoscope-Syster/src/kaleidoscope/plugin/Syster.cpp

129 lines
3.3 KiB

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