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  constexpr bool isValid() const {
51  return rowCol != INVALID;
52  }
53 
57  constexpr unsigned getRow() const {
58  assert(isValid());
59  return rowCol >> 3;
60  }
61 
65  constexpr unsigned getColumn() const {
66  assert(isValid());
67  return rowCol & 0x07;
68  }
69 
74  constexpr byte getRowCol() const {
75  assert(isValid());
76  return rowCol;
77  }
78 
83  constexpr unsigned getMask() const {
84  assert(isValid());
85  return 1 << getColumn();
86  }
87 
88  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  constexpr bool isValid() const {
115  return pos.isValid();
116  }
118  byte modmask = 0;
119  };
120 
121  explicit UnicodeKeymap(std::string_view keyboardType);
122 
123  KeyInfo get(unsigned unicode) const;
124  KeyInfo getDeadkey(unsigned n) const;
125 
132  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  std::vector<std::pair<unsigned, KeyInfo>> mapdata;
145  byte relevantMods[KeyMatrixPosition::NUM_ROWCOL];
146  KeyInfo deadKeys[NUM_DEAD_KEYS];
147 };
148 
149 } // namespace openmsx
150 
151 #endif
openmsx::UnicodeKeymap::KeyInfo::GRAPH
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::KeyInfo::pos
KeyMatrixPosition pos
Definition: UnicodeKeymap.hh:117
openmsx::UnicodeKeymap::KeyInfo::KeyInfo
constexpr KeyInfo()=default
openmsx.hh
openmsx::KeyMatrixPosition::NUM_ROWCOL
static constexpr unsigned NUM_ROWCOL
Combined row and column values are in the range [0..NUM_ROWCOL).
Definition: UnicodeKeymap.hh:23
openmsx::KeyMatrixPosition::KeyMatrixPosition
constexpr KeyMatrixPosition(unsigned row, unsigned col)
Creates a key matrix position with a given row and column.
Definition: UnicodeKeymap.hh:40
openmsx::KeyMatrixPosition
A position (row, column) in a keyboard matrix.
Definition: UnicodeKeymap.hh:14
openmsx::KeyMatrixPosition::NUM_ROWS
static constexpr unsigned NUM_ROWS
Rows are in the range [0..NUM_ROWS).
Definition: UnicodeKeymap.hh:19
openmsx::UnicodeKeymap::KeyInfo::modmask
byte modmask
Definition: UnicodeKeymap.hh:118
openmsx::KeyMatrixPosition::NUM_COLS
static constexpr unsigned NUM_COLS
Columns are in the range [0..NUM_COLS).
Definition: UnicodeKeymap.hh:21
openmsx::UnicodeKeymap::KeyInfo::CTRL
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::KeyInfo::CAPS
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::KeyInfo::NUM_MODIFIERS
Definition: UnicodeKeymap.hh:100
openmsx::KeyMatrixPosition::getRowCol
constexpr byte getRowCol() const
Returns the matrix row and column combined in a single byte: the column is stored in the lower 3 bits...
Definition: UnicodeKeymap.hh:74
openmsx::KeyMatrixPosition::KeyMatrixPosition
constexpr KeyMatrixPosition(byte rowCol_)
Creates a key matrix position from a byte: the row is stored in the high nibble, the column is stored...
Definition: UnicodeKeymap.hh:33
openmsx::UnicodeKeymap::KeyInfo::Modifier
Modifier
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::UnicodeKeymap
UnicodeKeymap(std::string_view keyboardType)
Definition: UnicodeKeymap.cc:86
openmsx::UnicodeKeymap::KeyInfo
Definition: UnicodeKeymap.hh:99
openmsx::UnicodeKeymap::KeyInfo::KeyInfo
constexpr KeyInfo(KeyMatrixPosition pos_, byte modmask_)
Definition: UnicodeKeymap.hh:109
openmsx::KeyMatrixPosition::getMask
constexpr unsigned getMask() const
Returns a mask with the bit corresponding to this position's column set, all other bits clear.
Definition: UnicodeKeymap.hh:83
openmsx::x
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1377
openmsx::UnicodeKeymap::KeyInfo::CTRL_MASK
static constexpr byte CTRL_MASK
Definition: UnicodeKeymap.hh:103
openmsx::UnicodeKeymap::KeyInfo::isValid
constexpr bool isValid() const
Definition: UnicodeKeymap.hh:114
openmsx::UnicodeKeymap::KeyInfo::GRAPH_MASK
static constexpr byte GRAPH_MASK
Definition: UnicodeKeymap.hh:104
openmsx::UnicodeKeymap::KeyInfo::CODE_MASK
static constexpr byte CODE_MASK
Definition: UnicodeKeymap.hh:106
openmsx::KeyMatrixPosition::KeyMatrixPosition
constexpr KeyMatrixPosition()=default
Creates an invalid key matrix position, which can be used when a key does not exist on a particular k...
openmsx::UnicodeKeymap::get
KeyInfo get(unsigned unicode) const
Definition: UnicodeKeymap.cc:100
openmsx::UnicodeKeymap::KeyInfo::SHIFT
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::KeyInfo::SHIFT_MASK
static constexpr byte SHIFT_MASK
Definition: UnicodeKeymap.hh:102
openmsx::UnicodeKeymap::getDeadkey
KeyInfo getDeadkey(unsigned n) const
Definition: UnicodeKeymap.cc:107
openmsx::KeyMatrixPosition::getColumn
constexpr unsigned getColumn() const
Returns the matrix column.
Definition: UnicodeKeymap.hh:65
openmsx::UnicodeKeymap::KeyInfo::CAPS_MASK
static constexpr byte CAPS_MASK
Definition: UnicodeKeymap.hh:105
openmsx::KeyMatrixPosition::getRow
constexpr unsigned getRow() const
Returns the matrix row.
Definition: UnicodeKeymap.hh:57
openmsx::UnicodeKeymap
Definition: UnicodeKeymap.hh:96
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::KeyMatrixPosition::isValid
constexpr bool isValid() const
Returns true iff this position is valid.
Definition: UnicodeKeymap.hh:50
openmsx::KeyMatrixPosition::operator==
constexpr friend bool operator==(const KeyMatrixPosition &x, const KeyMatrixPosition &y)
Definition: UnicodeKeymap.hh:88
openmsx::UnicodeKeymap::KeyInfo::CODE
Definition: UnicodeKeymap.hh:100
openmsx::UnicodeKeymap::getRelevantMods
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.
Definition: UnicodeKeymap.hh:132