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 <utility>
8 #include <vector>
9 
10 namespace openmsx {
11 
14 class KeyMatrixPosition final {
15  static constexpr unsigned INVALID = 0xFF;
16 
17 public:
19  static constexpr unsigned NUM_ROWS = 16;
21  static constexpr unsigned NUM_COLS = 8;
23  static constexpr unsigned NUM_ROWCOL = 1 << 7;
24 
28  constexpr KeyMatrixPosition() = default;
29 
33  constexpr KeyMatrixPosition(byte rowCol_)
34  : KeyMatrixPosition(rowCol_ >> 4, rowCol_ & 0x0F)
35  {
36  }
37 
40  constexpr KeyMatrixPosition(unsigned row, unsigned col)
41  : rowCol((row << 3) | col)
42  {
43  assert(row < NUM_ROWS);
44  assert(col < NUM_COLS);
45  assert(isValid());
46  }
47 
50  [[nodiscard]] constexpr bool isValid() const {
51  return rowCol != INVALID;
52  }
53 
57  [[nodiscard]] constexpr unsigned getRow() const {
58  assert(isValid());
59  return rowCol >> 3;
60  }
61 
65  [[nodiscard]] constexpr unsigned getColumn() const {
66  assert(isValid());
67  return rowCol & 0x07;
68  }
69 
74  [[nodiscard]] constexpr byte getRowCol() const {
75  assert(isValid());
76  return rowCol;
77  }
78 
83  [[nodiscard]] constexpr unsigned getMask() const {
84  assert(isValid());
85  return 1 << getColumn();
86  }
87 
88  [[nodiscard]] constexpr friend bool operator==(const KeyMatrixPosition& x, const KeyMatrixPosition& y) {
89  return x.rowCol == y.rowCol;
90  }
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 private:
137  static constexpr unsigned NUM_DEAD_KEYS = 3;
138 
139  void parseUnicodeKeymapfile(std::string_view data);
140 
141 private:
142  std::vector<std::pair<unsigned, KeyInfo>> mapdata;
146  byte relevantMods[KeyMatrixPosition::NUM_ROWCOL];
147  KeyInfo deadKeys[NUM_DEAD_KEYS];
148 };
149 
150 } // namespace openmsx
151 
152 #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:124
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