2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 19/06/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Typer.hpp"
|
2017-11-11 01:37:18 +00:00
|
|
|
|
2016-06-19 17:10:52 +00:00
|
|
|
using namespace Utility;
|
|
|
|
|
2020-03-01 23:44:26 +00:00
|
|
|
Typer::Typer(const std::string &string, HalfCycles delay, HalfCycles frequency, CharacterMapper &character_mapper, Delegate *delegate) :
|
2017-08-03 15:42:31 +00:00
|
|
|
frequency_(frequency),
|
2017-11-11 03:35:05 +00:00
|
|
|
counter_(-delay),
|
2017-08-03 15:42:31 +00:00
|
|
|
delegate_(delegate),
|
2020-03-01 23:44:26 +00:00
|
|
|
character_mapper_(character_mapper) {
|
2020-02-29 22:34:21 +00:00
|
|
|
// Retain only those characters that actually map to something.
|
|
|
|
if(sequence_for_character(Typer::BeginString)) {
|
|
|
|
string_ += Typer::BeginString;
|
|
|
|
}
|
|
|
|
if(sequence_for_character(Typer::EndString)) {
|
|
|
|
string_ += Typer::EndString;
|
|
|
|
}
|
|
|
|
|
|
|
|
append(string);
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void Typer::run_for(const HalfCycles duration) {
|
2020-02-29 22:34:21 +00:00
|
|
|
if(string_pointer_ >= string_.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(counter_ < 0 && counter_ + duration >= 0) {
|
|
|
|
if(!type_next_character()) {
|
|
|
|
delegate_->typer_reset(this);
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
2020-02-29 22:34:21 +00:00
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
counter_ += duration;
|
|
|
|
while(string_pointer_ < string_.size() && counter_ > frequency_) {
|
|
|
|
counter_ -= frequency_;
|
|
|
|
if(!type_next_character()) {
|
|
|
|
delegate_->typer_reset(this);
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
void Typer::append(const std::string &string) {
|
|
|
|
// Remove any characters that are already completely done;
|
|
|
|
// otherwise things may accumulate here indefinitely.
|
2020-03-01 00:22:54 +00:00
|
|
|
// Note that sequence_for_character may seek to look one backwards,
|
|
|
|
// so keep 'the character before' if there was one.
|
|
|
|
if(string_pointer_ > 1) {
|
|
|
|
string_.erase(string_.begin(), string_.begin() + ssize_t(string_pointer_) - 1);
|
|
|
|
string_pointer_ = 1;
|
|
|
|
}
|
2020-02-29 22:34:21 +00:00
|
|
|
|
|
|
|
// If the final character in the string is not Typer::EndString
|
|
|
|
// then this machine doesn't need Begin and End, so don't worry about it.
|
|
|
|
ssize_t insertion_position = ssize_t(string_.size());
|
|
|
|
if(string_.back() == Typer::EndString) --insertion_position;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
string_.reserve(string_.size() + string.size());
|
|
|
|
for(const char c : string) {
|
|
|
|
if(sequence_for_character(c)) {
|
|
|
|
string_.insert(string_.begin() + insertion_position, c);
|
|
|
|
++insertion_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint16_t *Typer::sequence_for_character(char c) const {
|
2020-03-01 23:44:26 +00:00
|
|
|
const uint16_t *const sequence = character_mapper_.sequence_for_character(c);
|
2018-10-25 01:59:30 +00:00
|
|
|
if(!sequence || sequence[0] == KeyboardMachine::MappedMachine::KeyNotMapped) {
|
2020-02-29 22:34:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return sequence;
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
uint16_t Typer::try_type_next_character() {
|
2020-02-29 22:34:21 +00:00
|
|
|
const uint16_t *const sequence = sequence_for_character(string_[string_pointer_]);
|
|
|
|
|
|
|
|
if(!sequence) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 23:51:55 +00:00
|
|
|
// Advance phase.
|
|
|
|
++phase_;
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
// If this is the start of the output sequence, start with a reset all keys.
|
2020-03-01 00:22:54 +00:00
|
|
|
// Then pause if either: (i) the machine requires it; or (ii) this is the same
|
|
|
|
// character that was just typed, in which case the gap in presses will need to
|
|
|
|
// be clear.
|
2020-02-29 23:51:55 +00:00
|
|
|
if(phase_ == 1) {
|
2020-02-29 23:12:32 +00:00
|
|
|
delegate_->clear_all_keys();
|
2020-03-01 23:44:26 +00:00
|
|
|
if(character_mapper_.needs_pause_after_reset_all_keys() ||
|
2020-03-01 00:22:54 +00:00
|
|
|
(string_pointer_ > 0 && string_[string_pointer_ - 1] == string_[string_pointer_])) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0xffff; // Arbitrarily. Anything non-zero will do.
|
|
|
|
}
|
2020-02-29 23:51:55 +00:00
|
|
|
++phase_;
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
// If the sequence is over, stop.
|
2020-02-29 23:51:55 +00:00
|
|
|
if(sequence[phase_ - 2] == KeyboardMachine::MappedMachine::KeyEndSequence) {
|
2020-02-29 23:12:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, type the key.
|
2020-02-29 23:51:55 +00:00
|
|
|
delegate_->set_key_state(sequence[phase_ - 2], true);
|
2020-02-29 23:12:32 +00:00
|
|
|
|
2020-02-29 23:51:55 +00:00
|
|
|
return sequence[phase_ - 2];
|
2017-08-03 15:42:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Typer::type_next_character() {
|
2017-12-29 20:26:03 +00:00
|
|
|
if(string_pointer_ == string_.size()) return false;
|
2016-08-06 18:33:24 +00:00
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
while(true) {
|
|
|
|
const uint16_t key_pressed = try_type_next_character();
|
|
|
|
|
|
|
|
if(!key_pressed) {
|
|
|
|
phase_ = 0;
|
|
|
|
++string_pointer_;
|
|
|
|
if(string_pointer_ == string_.size()) return false;
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:44:26 +00:00
|
|
|
if(character_mapper_.needs_pause_after_key(key_pressed)) {
|
2020-02-29 23:12:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
2016-08-06 18:33:24 +00:00
|
|
|
|
|
|
|
return true;
|
2016-06-19 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Character mapper
|
2017-07-22 01:18:51 +00:00
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
uint16_t *CharacterMapper::table_lookup_sequence_for_character(KeySequence *sequences, std::size_t length, char character) {
|
|
|
|
std::size_t ucharacter = static_cast<std::size_t>((unsigned char)character);
|
2020-02-29 23:35:13 +00:00
|
|
|
if(ucharacter >= (length / sizeof(KeySequence))) return nullptr;
|
2018-10-25 01:59:30 +00:00
|
|
|
if(sequences[ucharacter][0] == KeyboardMachine::MappedMachine::KeyNotMapped) return nullptr;
|
2017-07-22 01:18:51 +00:00
|
|
|
return sequences[ucharacter];
|
|
|
|
}
|
2020-03-02 01:25:12 +00:00
|
|
|
|