2016-06-19 17:10:52 +00:00
|
|
|
//
|
|
|
|
// Typer.hpp
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Typer_hpp
|
|
|
|
#define Typer_hpp
|
|
|
|
|
|
|
|
#include <memory>
|
2017-12-29 20:26:03 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-10-21 14:30:02 +00:00
|
|
|
#include "../KeyboardMachine.hpp"
|
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
2016-06-19 17:10:52 +00:00
|
|
|
|
|
|
|
namespace Utility {
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
An interface that provides a mapping from logical characters to the sequence of keys
|
|
|
|
necessary to type that character on a given machine.
|
|
|
|
*/
|
|
|
|
class CharacterMapper {
|
|
|
|
public:
|
2019-01-14 01:37:50 +00:00
|
|
|
virtual ~CharacterMapper() {}
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/// @returns The EndSequence-terminated sequence of keys that would cause @c character to be typed.
|
2020-05-21 03:34:26 +00:00
|
|
|
virtual const uint16_t *sequence_for_character(char character) const = 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
/// The typer will automatically reset all keys in between each sequence that it types.
|
|
|
|
/// By default it will pause for one key's duration when doing so. Character mappers
|
|
|
|
/// can eliminate that pause by overriding this method.
|
|
|
|
/// @returns @c true if the typer should pause after performing a reset; @c false otherwise.
|
2020-05-21 03:34:26 +00:00
|
|
|
virtual bool needs_pause_after_reset_all_keys() const { return true; }
|
2020-02-29 23:12:32 +00:00
|
|
|
|
|
|
|
/// The typer will pause between every entry in a keyboard sequence. On some machines
|
|
|
|
/// that may not be necessary — it'll often depends on whether the machine needs time to
|
|
|
|
/// observe a modifier like shift before it sees the actual keypress.
|
|
|
|
/// @returns @c true if the typer should pause after forwarding @c key; @c false otherwise.
|
2020-05-21 03:34:26 +00:00
|
|
|
virtual bool needs_pause_after_key(uint16_t key) const { return true; }
|
2020-02-29 23:12:32 +00:00
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
protected:
|
|
|
|
typedef uint16_t KeySequence[16];
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provided in the base class as a convenience: given the lookup table of key sequences @c sequences,
|
|
|
|
with @c length entries, returns the sequence for character @c character if it exists; otherwise
|
|
|
|
returns @c nullptr.
|
|
|
|
*/
|
2020-05-21 03:34:26 +00:00
|
|
|
uint16_t *table_lookup_sequence_for_character(KeySequence *sequences, std::size_t length, char character) const;
|
2017-08-03 15:42:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a stateful mechanism for typing a sequence of characters. Each character is mapped to a key sequence
|
|
|
|
by a character mapper. That key sequence is then replayed to a delegate.
|
|
|
|
|
|
|
|
Being given a delay and frequency at construction, the run_for interface can be used to produce time-based
|
|
|
|
typing. Alternatively, an owner may decline to use run_for and simply call type_next_character each time a
|
|
|
|
fresh key transition is ready to be consumed.
|
|
|
|
*/
|
2016-06-19 17:10:52 +00:00
|
|
|
class Typer {
|
|
|
|
public:
|
2020-04-02 03:19:34 +00:00
|
|
|
class Delegate: public MachineTypes::KeyActions {
|
2016-06-19 17:10:52 +00:00
|
|
|
public:
|
2020-02-29 22:34:21 +00:00
|
|
|
/// Informs the delegate that this typer has reached the end of its content.
|
2016-11-05 19:28:03 +00:00
|
|
|
virtual void typer_reset(Typer *typer) = 0;
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
2020-03-01 23:44:26 +00:00
|
|
|
Typer(const std::string &string, HalfCycles delay, HalfCycles frequency, CharacterMapper &character_mapper, Delegate *delegate);
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-02-29 22:34:21 +00:00
|
|
|
/// Advances for @c duration.
|
2017-07-28 02:05:29 +00:00
|
|
|
void run_for(const HalfCycles duration);
|
2020-02-29 22:34:21 +00:00
|
|
|
|
|
|
|
/// Types the next character now, if there is one.
|
|
|
|
/// @returns @c true if there was anything left to type; @c false otherwise.
|
2016-08-06 18:33:24 +00:00
|
|
|
bool type_next_character();
|
2020-02-29 22:34:21 +00:00
|
|
|
|
|
|
|
/// Adds the contents of @c str to the end of the current string.
|
|
|
|
void append(const std::string &str);
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2016-11-05 18:47:09 +00:00
|
|
|
const char BeginString = 0x02; // i.e. ASCII start of text
|
|
|
|
const char EndString = 0x03; // i.e. ASCII end of text
|
|
|
|
|
2016-06-19 17:10:52 +00:00
|
|
|
private:
|
2017-12-29 20:26:03 +00:00
|
|
|
std::string string_;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t string_pointer_ = 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-05-10 03:03:33 +00:00
|
|
|
const HalfCycles frequency_;
|
2017-07-28 01:53:45 +00:00
|
|
|
HalfCycles counter_;
|
2017-11-11 03:35:05 +00:00
|
|
|
int phase_ = 0;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-05-10 03:03:33 +00:00
|
|
|
Delegate *const delegate_;
|
2020-03-01 23:44:26 +00:00
|
|
|
CharacterMapper &character_mapper_;
|
2017-08-03 15:42:31 +00:00
|
|
|
|
2020-02-29 23:12:32 +00:00
|
|
|
uint16_t try_type_next_character();
|
2020-02-29 22:34:21 +00:00
|
|
|
const uint16_t *sequence_for_character(char) const;
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
Provides a default base class for type recipients: classes that want to attach a single typer at a time and
|
|
|
|
which may or may not want to nominate an initial delay and typing frequency.
|
|
|
|
*/
|
2020-04-02 03:19:34 +00:00
|
|
|
template <typename CMapper>
|
2016-06-19 17:10:52 +00:00
|
|
|
class TypeRecipient: public Typer::Delegate {
|
2018-03-09 20:19:02 +00:00
|
|
|
protected:
|
2020-03-01 23:44:26 +00:00
|
|
|
template <typename... Args> TypeRecipient(Args&&... args) : character_mapper(std::forward<Args>(args)...) {}
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/// Attaches a typer to this class that will type @c string using @c character_mapper as a source.
|
2020-03-01 23:44:26 +00:00
|
|
|
void add_typer(const std::string &string) {
|
|
|
|
if(!typer_) {
|
|
|
|
typer_ = std::make_unique<Typer>(string, get_typer_delay(), get_typer_frequency(), character_mapper, this);
|
|
|
|
} else {
|
|
|
|
typer_->append(string);
|
|
|
|
}
|
2016-06-19 17:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 01:25:12 +00:00
|
|
|
/*!
|
|
|
|
@returns @c true if the character mapper provides a mapping for @c c; @c false otherwise.
|
|
|
|
*/
|
2020-05-21 03:34:26 +00:00
|
|
|
bool can_type(char c) const {
|
2020-03-02 01:25:12 +00:00
|
|
|
const auto sequence = character_mapper.sequence_for_character(c);
|
2020-04-02 03:19:34 +00:00
|
|
|
return sequence && sequence[0] != MachineTypes::MappedKeyboardMachine::KeyNotMapped;
|
2020-03-02 01:25:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 15:42:31 +00:00
|
|
|
/*!
|
|
|
|
Provided in order to conform to that part of the Typer::Delegate interface that goes above and
|
|
|
|
beyond KeyboardMachine::Machine; responds to the end of typing by clearing all keys.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void typer_reset(Typer *typer) {
|
2016-11-05 19:28:03 +00:00
|
|
|
clear_all_keys();
|
2017-08-20 14:24:01 +00:00
|
|
|
|
|
|
|
// It's unsafe to deallocate typer right now, since it is the caller, but also it has a small
|
|
|
|
// memory footprint and it's desireable not to imply that the subclass need call it any more.
|
|
|
|
// So shuffle it off into a siding.
|
|
|
|
previous_typer_ = std::move(typer_);
|
|
|
|
typer_ = nullptr;
|
2016-11-05 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
virtual HalfCycles get_typer_delay() const { return HalfCycles(0); }
|
|
|
|
virtual HalfCycles get_typer_frequency() const { return HalfCycles(0); }
|
2016-12-03 15:55:50 +00:00
|
|
|
std::unique_ptr<Typer> typer_;
|
2017-08-20 14:24:01 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Typer> previous_typer_;
|
2020-04-02 03:19:34 +00:00
|
|
|
CMapper character_mapper;
|
2016-06-19 17:10:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Typer_hpp */
|