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 <cassert>
8 #include <optional>
9 #include <string_view>
10 #include <vector>
11 
12 namespace openmsx {
13 
16 class KeyMatrixPosition final {
17  static constexpr unsigned INVALID = 0xFF;
18 
19 public:
21  static constexpr unsigned NUM_ROWS = 16;
23  static constexpr unsigned NUM_COLS = 8;
25  static constexpr unsigned NUM_ROWCOL = 1 << 7;
26 
30  constexpr KeyMatrixPosition() = default;
31 
35  constexpr KeyMatrixPosition(byte rowCol_)
36  : KeyMatrixPosition(rowCol_ >> 4, rowCol_ & 0x0F)
37  {
38  }
39 
42  constexpr KeyMatrixPosition(unsigned row, unsigned col)
43  : rowCol((row << 3) | col)
44  {
45  assert(row < NUM_ROWS);
46  assert(col < NUM_COLS);
47  assert(isValid());
48  }
49 
52  [[nodiscard]] constexpr bool isValid() const {
53  return rowCol != INVALID;
54  }
55 
59  [[nodiscard]] constexpr unsigned getRow() const {
60  assert(isValid());
61  return rowCol >> 3;
62  }
63 
67  [[nodiscard]] constexpr unsigned getColumn() const {
68  assert(isValid());
69  return rowCol & 0x07;
70  }
71 
76  [[nodiscard]] constexpr byte getRowCol() const {
77  assert(isValid());
78  return rowCol;
79  }
80 
85  [[nodiscard]] constexpr unsigned getMask() const {
86  assert(isValid());
87  return 1 << getColumn();
88  }
89 
90  [[nodiscard]] constexpr bool operator==(const KeyMatrixPosition&) const = default;
91 
92 private:
93  byte rowCol = INVALID;
94 };
95 
97 {
98 public:
99  struct KeyInfo {
101  // Modifier masks:
102  static constexpr byte SHIFT_MASK = 1 << SHIFT;
103  static constexpr byte CTRL_MASK = 1 << CTRL;
104  static constexpr byte GRAPH_MASK = 1 << GRAPH;
105  static constexpr byte CAPS_MASK = 1 << CAPS;
106  static constexpr byte CODE_MASK = 1 << CODE;
107 
108  constexpr KeyInfo() = default;
109  constexpr KeyInfo(KeyMatrixPosition pos_, byte modmask_)
110  : pos(pos_), modmask(modmask_)
111  {
112  assert(pos.isValid());
113  }
114  [[nodiscard]] constexpr bool isValid() const {
115  return pos.isValid();
116  }
118  byte modmask = 0;
119  };
120 
121  explicit UnicodeKeymap(std::string_view keyboardType);
122 
123  [[nodiscard]] KeyInfo get(unsigned unicode) const;
124  [[nodiscard]] KeyInfo getDeadkey(unsigned n) const;
125 
132  [[nodiscard]] byte getRelevantMods(const KeyInfo& keyInfo) const {
133  return relevantMods[keyInfo.pos.getRowCol()];
134  }
135 
136  [[nodiscard]] const MsxChar2Unicode& getMsxChars() const {
137  if (!msxChars) throw CommandException("Missing MSX-Video-characterset file"); // TODO make this required for MSX/SVI machines
138  return *msxChars;
139  }
140 
141 private:
142  static constexpr unsigned NUM_DEAD_KEYS = 3;
143 
144  void parseUnicodeKeymapfile(std::string_view data);
145 
146 private:
147  struct Entry {
148  unsigned unicode;
149  KeyInfo keyInfo;
150  };
151  std::vector<Entry> mapdata; // sorted on unicode
152 
156  byte relevantMods[KeyMatrixPosition::NUM_ROWCOL];
157  KeyInfo deadKeys[NUM_DEAD_KEYS];
158 
159  std::optional<MsxChar2Unicode> msxChars; // TODO should this be required for MSX/SVI machines?
160 };
161 
162 } // namespace openmsx
163 
164 #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...
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
KeyInfo getDeadkey(unsigned n) const
This file implemented 3 utility functions:
Definition: Autofire.cc:9
UnicodeKeymap::KeyInfo KeyInfo
Definition: Keyboard.cc:56
static constexpr byte CAPS_MASK
constexpr KeyInfo()=default
constexpr bool isValid() const
constexpr KeyInfo(KeyMatrixPosition pos_, byte modmask_)
static constexpr byte CTRL_MASK
static constexpr byte GRAPH_MASK
static constexpr byte CODE_MASK
static constexpr byte SHIFT_MASK