1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-30 22:29:56 +00:00
CLK/Machines/KeyboardMachine.hpp
Thomas Harte a8645f80bf Introduces 'non-exclusive' emulator-space keyboards.
i.e. sets of keys that don't amount to an entire keyboard in the modern sense. Experimentally used by the Master System for its reset key.
2018-10-24 21:59:30 -04:00

100 lines
2.6 KiB
C++

//
// KeyboardMachine.h
// Clock Signal
//
// Created by Thomas Harte on 05/11/2016.
// Copyright 2016 Thomas Harte. All rights reserved.
//
#ifndef KeyboardMachine_h
#define KeyboardMachine_h
#include <cstdint>
#include <string>
#include "../Inputs/Keyboard.hpp"
namespace KeyboardMachine {
/*!
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.
*/
virtual void set_key_state(uint16_t key, bool is_pressed) {}
/*!
Instructs that all keys should now be treated as released.
*/
virtual void clear_all_keys() {}
};
/*!
Describes an emulated machine which exposes a keyboard and accepts a typed string.
*/
class Machine: public KeyActions {
public:
/*!
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 &);
/*!
Provides a destination for keyboard input.
*/
virtual Inputs::Keyboard &get_keyboard() = 0;
};
/*!
Provides a base class for machines that want to provide a keyboard mapper,
allowing automatic mapping from keyboard inputs to KeyActions.
*/
class MappedMachine: public Inputs::Keyboard::Delegate, public Machine {
public:
MappedMachine();
/*!
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:
virtual uint16_t mapped_key_for_key(Inputs::Keyboard::Key key) = 0;
};
/// Terminates a key sequence from the character mapper.
static const uint16_t KeyEndSequence = 0xffff;
/*!
Indicates that a key is not mapped (for the keyboard mapper) or that a
character cannot be typed (for the character mapper).
*/
static const uint16_t KeyNotMapped = 0xfffe;
/*!
Allows individual machines to provide the mapping between host keys
as per Inputs::Keyboard and their native scheme.
*/
virtual KeyboardMapper *get_keyboard_mapper();
/*!
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;
private:
void keyboard_did_change_key(Inputs::Keyboard *keyboard, Inputs::Keyboard::Key key, bool is_pressed) override;
void reset_all_keys(Inputs::Keyboard *keyboard) override;
Inputs::Keyboard keyboard_;
};
}
#endif /* KeyboardMachine_h */