openMSX
UnicodeKeymap.hh
Go to the documentation of this file.
1 #ifndef UNICODEKEYMAP_HH
2 #define UNICODEKEYMAP_HH
3 
4 #include "openmsx.hh"
5 #include <cassert>
6 #include <string_view>
7 #include <vector>
8 
9 namespace openmsx {
10 
13 class KeyMatrixPosition final {
14  static constexpr unsigned INVALID = 0xFF;
15 
16 public:
18  static constexpr unsigned NUM_ROWS = 16;
20  static constexpr unsigned NUM_COLS = 8;
22  static constexpr unsigned NUM_ROWCOL = 1 << 7;
23 
27  constexpr KeyMatrixPosition() = default;
28 
32  constexpr KeyMatrixPosition(byte rowCol_)
33  : KeyMatrixPosition(rowCol_ >> 4, rowCol_ & 0x0F)
34  {
35  }
36 
39  constexpr KeyMatrixPosition(unsigned row, unsigned col)
40  : rowCol((row << 3) | col)
41  {
42  assert(row < NUM_ROWS);
43  assert(col < NUM_COLS);
44  assert(isValid());
45  }
46 
49  [[nodiscard]] constexpr bool isValid() const {
50  return rowCol != INVALID;
51  }
52 
56  [[nodiscard]] constexpr unsigned getRow() const {
57  assert(isValid());
58  return rowCol >> 3;
59  }
60 
64  [[nodiscard]] constexpr unsigned getColumn() const {
65  assert(isValid());
66  return rowCol & 0x07;
67  }
68 
73  [[nodiscard]] constexpr byte getRowCol() const {
74  assert(isValid());
75  return rowCol;
76  }
77 
82  [[nodiscard]] constexpr unsigned getMask() const {
83  assert(isValid());
84  return 1 << getColumn();
85  }
86 
87  [[nodiscard]] constexpr friend bool operator==(const KeyMatrixPosition& x, const KeyMatrixPosition& y) {
88  return x.rowCol == y.rowCol;
89  }
90 
91 private:
92  byte rowCol = INVALID;
93 };
94 
96 {
97 public:
98  struct KeyInfo {
100  // Modifier masks:
101  static constexpr byte SHIFT_MASK = 1 << SHIFT;
102  static constexpr byte CTRL_MASK = 1 << CTRL;
103  static constexpr byte GRAPH_MASK = 1 << GRAPH;
104  static constexpr byte CAPS_MASK = 1 << CAPS;
105  static constexpr byte CODE_MASK = 1 << CODE;
106 
107  constexpr KeyInfo() = default;
108  constexpr KeyInfo(KeyMatrixPosition pos_, byte modmask_)
109  : pos(pos_), modmask(modmask_)
110  {
111  assert(pos.isValid());
112  }
113  [[nodiscard]] constexpr bool isValid() const {
114  return pos.isValid();
115  }
117  byte modmask = 0;
118  };
119 
120  explicit UnicodeKeymap(std::string_view keyboardType);
121 
122  [[nodiscard]] KeyInfo get(unsigned unicode) const;
123  [[nodiscard]] KeyInfo getDeadkey(unsigned n) const;
124 
131  [[nodiscard]] byte getRelevantMods(const KeyInfo& keyInfo) const {
132  return relevantMods[keyInfo.pos.getRowCol()];
133  }
134 
135 private:
136  static constexpr unsigned NUM_DEAD_KEYS = 3;
137 
138  void parseUnicodeKeymapfile(std::string_view data);
139 
140 private:
141  struct Entry {
142  unsigned unicode;
143  KeyInfo keyInfo;
144  };
145  std::vector<Entry> mapdata; // sorted on unicode
146 
150  byte relevantMods[KeyMatrixPosition::NUM_ROWCOL];
151  KeyInfo deadKeys[NUM_DEAD_KEYS];
152 };
153 
154 } // namespace openmsx
155 
156 #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 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 friend bool operator==(const KeyMatrixPosition &x, const KeyMatrixPosition &y)
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)
KeyInfo getDeadkey(unsigned n) const
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:118
UnicodeKeymap::KeyInfo KeyInfo
Definition: Keyboard.cc:55
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