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 "openmsx.hh"
7#include <array>
8#include <cassert>
9#include <optional>
10#include <string_view>
11#include <vector>
12
13namespace openmsx {
14
17class KeyMatrixPosition final {
18 static constexpr unsigned INVALID = 0xFF;
19
20public:
22 static constexpr unsigned NUM_ROWS = 16;
24 static constexpr unsigned NUM_COLS = 8;
26 static constexpr unsigned NUM_ROWCOL = 1 << 7;
27
31 constexpr KeyMatrixPosition() = default;
32
36 constexpr KeyMatrixPosition(byte rowCol_)
37 : KeyMatrixPosition(rowCol_ >> 4, rowCol_ & 0x0F)
38 {
39 }
40
43 constexpr KeyMatrixPosition(unsigned row, unsigned col)
44 : rowCol((row << 3) | col)
45 {
46 assert(row < NUM_ROWS);
47 assert(col < NUM_COLS);
48 assert(isValid());
49 }
50
53 [[nodiscard]] constexpr bool isValid() const {
54 return rowCol != INVALID;
55 }
56
60 [[nodiscard]] constexpr unsigned getRow() const {
61 assert(isValid());
62 return rowCol >> 3;
63 }
64
68 [[nodiscard]] constexpr unsigned getColumn() const {
69 assert(isValid());
70 return rowCol & 0x07;
71 }
72
77 [[nodiscard]] constexpr byte getRowCol() const {
78 assert(isValid());
79 return rowCol;
80 }
81
86 [[nodiscard]] constexpr unsigned getMask() const {
87 assert(isValid());
88 return 1 << getColumn();
89 }
90
91 [[nodiscard]] constexpr bool operator==(const KeyMatrixPosition&) const = default;
92
93private:
94 byte rowCol = INVALID;
95};
96
98{
99public:
100 struct KeyInfo {
102 // Modifier masks:
103 static constexpr byte SHIFT_MASK = 1 << SHIFT;
104 static constexpr byte CTRL_MASK = 1 << CTRL;
105 static constexpr byte GRAPH_MASK = 1 << GRAPH;
106 static constexpr byte CAPS_MASK = 1 << CAPS;
107 static constexpr byte CODE_MASK = 1 << CODE;
108
109 constexpr KeyInfo() = default;
110 constexpr KeyInfo(KeyMatrixPosition pos_, byte modMask_)
111 : pos(pos_), modMask(modMask_)
112 {
113 assert(pos.isValid());
114 }
115 [[nodiscard]] constexpr bool isValid() const {
116 return pos.isValid();
117 }
119 byte modMask = 0;
120 };
121
122 explicit UnicodeKeymap(std::string_view keyboardType);
123
124 [[nodiscard]] KeyInfo get(unsigned unicode) const;
125 [[nodiscard]] KeyInfo getDeadKey(unsigned n) const;
126
133 [[nodiscard]] byte getRelevantMods(const KeyInfo& keyInfo) const {
134 return relevantMods[keyInfo.pos.getRowCol()];
135 }
136
137 [[nodiscard]] const MsxChar2Unicode& getMsxChars() const {
138 if (!msxChars) throw CommandException("Missing MSX-Video-characterset file"); // TODO make this required for MSX/SVI machines
139 return *msxChars;
140 }
141
142private:
143 static constexpr unsigned NUM_DEAD_KEYS = 3;
144
145 void parseUnicodeKeyMapFile(std::string_view data);
146
147private:
148 struct Entry {
149 unsigned unicode;
150 KeyInfo keyInfo;
151 };
152 std::vector<Entry> mapData; // sorted on unicode
153
157 std::array<byte, KeyMatrixPosition::NUM_ROWCOL> relevantMods;
158 std::array<KeyInfo, NUM_DEAD_KEYS> deadKeys;
159
160 std::optional<MsxChar2Unicode> msxChars; // TODO should this be required for MSX/SVI machines?
161};
162
163} // namespace openmsx
164
165#endif
A position (row, column) in a keyboard matrix.
constexpr unsigned getRow() const
Returns the matrix row.
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 unsigned getMask() const
Returns a mask with the bit corresponding to this position's column set, all other bits clear.
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).
static constexpr unsigned NUM_ROWS
Rows are in the range [0..NUM_ROWS).
constexpr KeyMatrixPosition(byte rowCol_)
Creates a key matrix position from a byte: the row is stored in the high nibble, the column is stored...
constexpr unsigned getColumn() const
Returns the matrix column.
constexpr byte getRowCol() const
Returns the matrix row and column combined in a single byte: the column is stored in the lower 3 bits...
KeyInfo getDeadKey(unsigned n) const
byte getRelevantMods(const KeyInfo &keyInfo) const
Returns a mask in which a bit is set iff the corresponding modifier is relevant for the given key.
KeyInfo get(unsigned unicode) const
UnicodeKeymap(std::string_view keyboardType)
const MsxChar2Unicode & getMsxChars() const
This file implemented 3 utility functions:
Definition: Autofire.cc:9
static constexpr byte CAPS_MASK
constexpr KeyInfo()=default
constexpr KeyInfo(KeyMatrixPosition pos_, byte modMask_)
constexpr bool isValid() const
static constexpr byte CTRL_MASK
static constexpr byte GRAPH_MASK
static constexpr byte CODE_MASK
static constexpr byte SHIFT_MASK