openMSX
UnicodeKeymap.hh
Go to the documentation of this file.
1#ifndef UNICODEKEYMAP_HH
2#define UNICODEKEYMAP_HH
3
4#include "CommandException.hh"
5#include "MsxChar2Unicode.hh"
6#include "narrow.hh"
7#include <array>
8#include <cassert>
9#include <cstdint>
10#include <optional>
11#include <string_view>
12#include <vector>
13
14namespace openmsx {
15
18class KeyMatrixPosition final {
19 static constexpr unsigned INVALID = 0xFF;
20
21public:
23 static constexpr unsigned NUM_ROWS = 16;
25 static constexpr unsigned NUM_COLS = 8;
27 static constexpr unsigned NUM_ROWCOL = 1 << 7;
28
32 constexpr KeyMatrixPosition() = default;
33
37 constexpr KeyMatrixPosition(uint8_t rowCol_)
38 : KeyMatrixPosition(rowCol_ >> 4, rowCol_ & 0x0F)
39 {
40 }
41
44 constexpr KeyMatrixPosition(unsigned row, unsigned col)
45 : rowCol(narrow<uint8_t>((row << 3) | col))
46 {
47 assert(row < NUM_ROWS);
48 assert(col < NUM_COLS);
49 assert(isValid());
50 }
51
54 [[nodiscard]] constexpr bool isValid() const {
55 return rowCol != INVALID;
56 }
57
61 [[nodiscard]] constexpr uint8_t getRow() const {
62 assert(isValid());
63 return rowCol >> 3;
64 }
65
69 [[nodiscard]] constexpr uint8_t getColumn() const {
70 assert(isValid());
71 return rowCol & 0x07;
72 }
73
78 [[nodiscard]] constexpr uint8_t getRowCol() const {
79 assert(isValid());
80 return rowCol;
81 }
82
87 [[nodiscard]] constexpr uint8_t getMask() const {
88 assert(isValid());
89 return uint8_t(1 << getColumn());
90 }
91
92 [[nodiscard]] constexpr bool operator==(const KeyMatrixPosition&) const = default;
93
94private:
95 uint8_t rowCol = INVALID;
96};
97
99{
100public:
101 struct KeyInfo {
103 // Modifier masks:
104 static constexpr uint8_t SHIFT_MASK = 1 << SHIFT;
105 static constexpr uint8_t CTRL_MASK = 1 << CTRL;
106 static constexpr uint8_t GRAPH_MASK = 1 << GRAPH;
107 static constexpr uint8_t CAPS_MASK = 1 << CAPS;
108 static constexpr uint8_t CODE_MASK = 1 << CODE;
109
110 constexpr KeyInfo() = default;
111 constexpr KeyInfo(KeyMatrixPosition pos_, uint8_t modMask_)
112 : pos(pos_), modMask(modMask_)
113 {
114 assert(pos.isValid());
115 }
116 [[nodiscard]] constexpr bool isValid() const {
117 return pos.isValid();
118 }
120 uint8_t modMask = 0;
121 };
122
123 explicit UnicodeKeymap(std::string_view keyboardType);
124
125 [[nodiscard]] KeyInfo get(unsigned unicode) const;
126 [[nodiscard]] KeyInfo getDeadKey(unsigned n) const;
127
134 [[nodiscard]] uint8_t getRelevantMods(const KeyInfo& keyInfo) const {
135 return relevantMods[keyInfo.pos.getRowCol()];
136 }
137
138 [[nodiscard]] const MsxChar2Unicode& getMsxChars() const {
139 if (!msxChars) throw CommandException("Missing MSX-Video-characterset file"); // TODO make this required for MSX/SVI machines
140 return *msxChars;
141 }
142
143private:
144 static constexpr unsigned NUM_DEAD_KEYS = 3;
145
146 void parseUnicodeKeyMapFile(std::string_view data);
147
148private:
149 struct Entry {
150 unsigned unicode;
151 KeyInfo keyInfo;
152 };
153 std::vector<Entry> mapData; // sorted on unicode
154
158 std::array<uint8_t, KeyMatrixPosition::NUM_ROWCOL> relevantMods;
159 std::array<KeyInfo, NUM_DEAD_KEYS> deadKeys;
160
161 std::optional<MsxChar2Unicode> msxChars; // TODO should this be required for MSX/SVI machines?
162};
163
164} // namespace openmsx
165
166#endif
A position (row, column) in a keyboard matrix.
constexpr uint8_t getColumn() const
Returns the matrix column.
constexpr KeyMatrixPosition(uint8_t rowCol_)
Creates a key matrix position from a uint8_t: the row is stored in the high nibble,...
constexpr bool isValid() const
Returns true iff this position is valid.
static constexpr unsigned NUM_ROWCOL
Combined row and column values are in the range [0..NUM_ROWCOL).
constexpr KeyMatrixPosition(unsigned row, unsigned col)
Creates a key matrix position with a given row and column.
constexpr uint8_t getRowCol() const
Returns the matrix row and column combined in a single uint8_t: the column is stored in the lower 3 b...
constexpr bool operator==(const KeyMatrixPosition &) const =default
constexpr KeyMatrixPosition()=default
Creates an invalid key matrix position, which can be used when a key does not exist on a particular k...
static constexpr unsigned NUM_COLS
Columns are in the range [0..NUM_COLS).
constexpr uint8_t getMask() const
Returns a mask with the bit corresponding to this position's column set, all other bits clear.
static constexpr unsigned NUM_ROWS
Rows are in the range [0..NUM_ROWS).
constexpr uint8_t getRow() const
Returns the matrix row.
KeyInfo getDeadKey(unsigned n) const
KeyInfo get(unsigned unicode) const
uint8_t getRelevantMods(const KeyInfo &keyInfo) const
Returns a mask in which a bit is set iff the corresponding modifier is relevant for the given key.
const MsxChar2Unicode & getMsxChars() const
This file implemented 3 utility functions:
Definition Autofire.cc:9
constexpr To narrow(From from) noexcept
Definition narrow.hh:37
constexpr KeyInfo(KeyMatrixPosition pos_, uint8_t modMask_)
static constexpr uint8_t CAPS_MASK
constexpr KeyInfo()=default
constexpr bool isValid() const
static constexpr uint8_t SHIFT_MASK
static constexpr uint8_t GRAPH_MASK
static constexpr uint8_t CODE_MASK
static constexpr uint8_t CTRL_MASK