2016-11-05 18:47:09 +00:00
|
|
|
//
|
|
|
|
// KeyboardMachine.h
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/11/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-11-05 18:47:09 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef KeyboardMachine_h
|
|
|
|
#define KeyboardMachine_h
|
|
|
|
|
2020-03-04 03:58:15 +00:00
|
|
|
#include <bitset>
|
2017-11-10 03:04:49 +00:00
|
|
|
#include <cstdint>
|
2017-12-29 23:41:26 +00:00
|
|
|
#include <string>
|
2019-09-22 17:14:09 +00:00
|
|
|
#include <set>
|
2017-11-10 03:04:49 +00:00
|
|
|
|
2017-10-13 02:25:02 +00:00
|
|
|
#include "../Inputs/Keyboard.hpp"
|
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
namespace MachineTypes {
|
2016-11-05 18:47:09 +00:00
|
|
|
|
2018-03-09 20:19:02 +00:00
|
|
|
/*!
|
|
|
|
Covers just the actions necessary to communicate keyboard state, as a purely abstract class.
|
|
|
|
*/
|
|
|
|
struct KeyActions {
|
|
|
|
/*!
|
|
|
|
Indicates that the key @c key has been either pressed or released, according to
|
|
|
|
the state of @c isPressed.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual void set_key_state([[maybe_unused]] uint16_t key, [[maybe_unused]] bool is_pressed) {}
|
2018-03-09 20:19:02 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Instructs that all keys should now be treated as released.
|
|
|
|
*/
|
2018-04-16 01:11:30 +00:00
|
|
|
virtual void clear_all_keys() {}
|
2020-11-28 02:00:48 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Indicates whether a machine most naturally accepts logical rather than physical input.
|
|
|
|
*/
|
|
|
|
virtual bool prefers_logical_input() { return false; }
|
2018-03-09 20:19:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
2018-10-25 01:59:30 +00:00
|
|
|
Describes an emulated machine which exposes a keyboard and accepts a typed string.
|
2018-03-09 20:19:02 +00:00
|
|
|
*/
|
2020-04-02 03:19:34 +00:00
|
|
|
class KeyboardMachine: public KeyActions {
|
2016-11-05 18:47:09 +00:00
|
|
|
public:
|
2017-12-29 23:30:46 +00:00
|
|
|
/*!
|
|
|
|
Causes the machine to attempt to type the supplied string.
|
|
|
|
|
|
|
|
This is best effort. Success or failure is permitted to be a function of machine and current state.
|
|
|
|
*/
|
|
|
|
virtual void type_string(const std::string &);
|
|
|
|
|
2020-03-02 01:25:12 +00:00
|
|
|
/*!
|
|
|
|
@returns @c true if this machine can type the character @c c as part of a @c type_string; @c false otherwise.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
virtual bool can_type([[maybe_unused]] char c) const { return false; }
|
2020-03-02 01:25:12 +00:00
|
|
|
|
2017-10-13 02:25:02 +00:00
|
|
|
/*!
|
|
|
|
Provides a destination for keyboard input.
|
|
|
|
*/
|
2018-10-25 01:59:30 +00:00
|
|
|
virtual Inputs::Keyboard &get_keyboard() = 0;
|
2020-03-04 03:58:15 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a standard bundle of logic for hosts that are able to correlate typed symbols
|
|
|
|
with keypresses. Specifically:
|
|
|
|
|
|
|
|
If map_logically is false:
|
|
|
|
|
|
|
|
(i) initially try to set @c key as @c is_pressed;
|
|
|
|
(ii) if this machine doesn't map @c key to anything but @c symbol is a printable ASCII character, attempt to @c type_string it.
|
|
|
|
|
|
|
|
If map_logically is true:
|
|
|
|
|
|
|
|
(i) if @c symbol can be typed and this is a key down, @c type_string it;
|
|
|
|
(ii) if @c symbol cannot be typed, set @c key as @c is_pressed
|
|
|
|
*/
|
|
|
|
bool apply_key(Inputs::Keyboard::Key key, char symbol, bool is_pressed, bool map_logically) {
|
|
|
|
Inputs::Keyboard &keyboard = get_keyboard();
|
|
|
|
|
|
|
|
if(!map_logically) {
|
|
|
|
// Try a regular keypress first, and stop if that works.
|
|
|
|
if(keyboard.set_key_pressed(key, symbol, is_pressed)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// That having failed, if a symbol has been supplied then try typing it.
|
2020-03-06 02:56:26 +00:00
|
|
|
if(is_pressed && symbol && can_type(symbol)) {
|
2020-03-04 03:58:15 +00:00
|
|
|
char string[2] = { symbol, 0 };
|
|
|
|
type_string(string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Try to type first.
|
|
|
|
if(is_pressed && symbol && can_type(symbol)) {
|
|
|
|
char string[2] = { symbol, 0 };
|
|
|
|
type_string(string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// That didn't work. Forward as a keypress. As, either:
|
|
|
|
// (i) this is a key down, but doesn't have a symbol, or is an untypeable symbol; or
|
|
|
|
// (ii) this is a key up, which it won't be an issue to miscommunicate.
|
|
|
|
return keyboard.set_key_pressed(key, symbol, is_pressed);
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 01:59:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides a base class for machines that want to provide a keyboard mapper,
|
|
|
|
allowing automatic mapping from keyboard inputs to KeyActions.
|
|
|
|
*/
|
2020-04-02 03:19:34 +00:00
|
|
|
class MappedKeyboardMachine: public Inputs::Keyboard::Delegate, public KeyboardMachine {
|
2018-10-25 01:59:30 +00:00
|
|
|
public:
|
2020-04-02 03:19:34 +00:00
|
|
|
MappedKeyboardMachine(const std::set<Inputs::Keyboard::Key> &essential_modifiers = {});
|
2017-10-13 02:25:02 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
A keyboard mapper attempts to provide a physical mapping between host keys and emulated keys.
|
|
|
|
See the character mapper for logical mapping.
|
|
|
|
*/
|
|
|
|
class KeyboardMapper {
|
|
|
|
public:
|
2020-05-21 03:34:26 +00:00
|
|
|
virtual uint16_t mapped_key_for_key(Inputs::Keyboard::Key key) const = 0;
|
2017-10-13 02:25:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Terminates a key sequence from the character mapper.
|
2020-05-13 02:22:21 +00:00
|
|
|
static constexpr uint16_t KeyEndSequence = 0xffff;
|
2017-10-13 02:25:02 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Indicates that a key is not mapped (for the keyboard mapper) or that a
|
|
|
|
character cannot be typed (for the character mapper).
|
|
|
|
*/
|
2020-05-13 02:22:21 +00:00
|
|
|
static constexpr uint16_t KeyNotMapped = 0xfffe;
|
2017-10-13 02:25:02 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Allows individual machines to provide the mapping between host keys
|
|
|
|
as per Inputs::Keyboard and their native scheme.
|
|
|
|
*/
|
2018-02-09 21:31:05 +00:00
|
|
|
virtual KeyboardMapper *get_keyboard_mapper();
|
2017-10-13 02:25:02 +00:00
|
|
|
|
2018-10-25 01:59:30 +00:00
|
|
|
/*!
|
|
|
|
Provides a keyboard that obtains this machine's keyboard mapper, maps
|
|
|
|
the key and supplies it via the KeyActions.
|
|
|
|
*/
|
|
|
|
virtual Inputs::Keyboard &get_keyboard() override;
|
|
|
|
|
2017-10-13 02:25:02 +00:00
|
|
|
private:
|
2020-03-03 03:08:54 +00:00
|
|
|
bool keyboard_did_change_key(Inputs::Keyboard *keyboard, Inputs::Keyboard::Key key, bool is_pressed) override;
|
2017-10-15 02:07:11 +00:00
|
|
|
void reset_all_keys(Inputs::Keyboard *keyboard) override;
|
2017-10-13 02:25:02 +00:00
|
|
|
Inputs::Keyboard keyboard_;
|
2016-11-05 18:47:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* KeyboardMachine_h */
|