12using std::string_view;
18[[nodiscard]]
static constexpr std::optional<unsigned> parseHex(string_view str)
24 for (
const char c : str) {
26 if (
'0' <= c && c <=
'9') {
28 }
else if (
'A' <= c && c <=
'F') {
29 value += c -
'A' + 10;
30 }
else if (
'a' <= c && c <=
'f') {
31 value += c -
'a' + 10;
43[[nodiscard]]
static constexpr bool isSep(
char c)
53static constexpr void skipSep(string_view& str)
55 while (!str.empty()) {
56 const char c = str.front();
60 while (!str.empty() && str.front() !=
'\n') str.remove_prefix(1);
70[[nodiscard]]
static constexpr string_view nextToken(string_view& str)
73 const auto* tokenBegin = str.data();
74 while (!str.empty() && str.front() !=
'\n' && !isSep(str.front())) {
78 return {tokenBegin, size_t(str.data() - tokenBegin)};
85 tmpStrCat(
"unicodemaps/unicodemap.", keyboardType));
88 auto buf = file.
mmap();
89 parseUnicodeKeyMapFile(
90 string_view(
reinterpret_cast<const char*
>(buf.data()), buf.size()));
94 if (!msxChars.has_value()) {
95 msxChars.emplace(
"MSXVID.TXT");
98 throw MSXException(
"Couldn't load unicode keymap file: ", filename);
104 auto m =
binary_find(mapData, unicode, {}, &Entry::unicode);
105 return m ? m->keyInfo :
KeyInfo();
110 assert(n < NUM_DEAD_KEYS);
114void UnicodeKeymap::parseUnicodeKeyMapFile(string_view data)
118 while (!data.empty()) {
119 if (data.front() ==
'\n') {
121 data.remove_prefix(1);
124 string_view token = nextToken(data);
130 if (token ==
"MSX-Video-Characterset:") {
131 auto vidFileName = nextToken(data);
132 if (vidFileName.empty()) {
133 throw MSXException(
"Missing filename for MSX-Video-Characterset");
135 msxChars.emplace(vidFileName);
140 unsigned unicode = 0;
141 unsigned deadKeyIndex = 0;
142 bool isDeadKey = token.starts_with(
"DEADKEY");
144 token.remove_prefix(strlen(
"DEADKEY"));
151 auto d = parseHex(token);
152 if (!d || *d > NUM_DEAD_KEYS) {
154 "Wrong deadkey number in keymap file. "
155 "It must be 1..", NUM_DEAD_KEYS);
157 deadKeyIndex = *d - 1;
160 auto u = parseHex(token);
161 if (!u || *u > 0x1FBFF) {
162 throw MSXException(
"Wrong unicode value in keymap file");
168 token = nextToken(data);
173 auto rowcol = parseHex(token);
174 if (!rowcol || *rowcol >= 0x100) {
176 (token.empty() ?
"Missing" :
"Wrong"),
177 " <ROW><COL> value in keymap file");
180 throw MSXException(
"Too high row value in keymap file");
183 throw MSXException(
"Too high column value in keymap file");
185 auto pos = KeyMatrixPosition(narrow_cast<uint8_t>(*rowcol));
190 token = nextToken(data);
193 }
else if (token ==
"SHIFT") {
195 }
else if (token ==
"CTRL") {
197 }
else if (token ==
"GRAPH") {
199 }
else if (token ==
"CAPSLOCK") {
201 }
else if (token ==
"CODE") {
205 "Invalid modifier \"", token,
"\" in keymap file");
212 "DEADKEY entry in keymap file cannot have modifiers");
214 deadKeys[deadKeyIndex] =
KeyInfo(pos, 0);
216 mapData.emplace_back(Entry{unicode,
KeyInfo(pos, modMask)});
218 relevantMods[pos.getRowCol()] |= modMask;
std::string resolve(std::string_view filename) const
std::span< const uint8_t > mmap()
Map file in memory.
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).
KeyInfo getDeadKey(unsigned n) const
KeyInfo get(unsigned unicode) const
UnicodeKeymap(std::string_view keyboardType)
This file implemented 3 utility functions:
const FileContext & systemFileContext()
UnicodeKeymap::KeyInfo KeyInfo
constexpr void fill(ForwardRange &&range, const T &value)
constexpr void sort(RandomAccessRange &&range)
auto * binary_find(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
TemporaryString tmpStrCat(Ts &&... ts)
static constexpr uint8_t CAPS_MASK
static constexpr uint8_t SHIFT_MASK
static constexpr uint8_t GRAPH_MASK
static constexpr uint8_t CODE_MASK
static constexpr uint8_t CTRL_MASK