This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeyboard.cpp
365 lines (304 loc) · 8.95 KB
/
keyboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// setting.cpp
#include "keyboard.h"
#include <algorithm>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Key
// add a name or an alias of key
void Key::addName(const tstringi &i_name)
{
m_names.push_back(i_name);
}
// add a scan code
void Key::addScanCode(const ScanCode &i_sc)
{
ASSERT(m_scanCodesSize < MAX_SCAN_CODES_SIZE);
m_scanCodes[m_scanCodesSize ++] = i_sc;
}
// initializer
Key &Key::initialize()
{
m_names.clear();
m_isPressed = false;
m_isPressedOnWin32 = false;
m_isPressedByAssign = false;
m_scanCodesSize = 0;
return *this;
}
// equation by name
bool Key::operator==(const tstringi &i_name) const
{
return std::find(m_names.begin(), m_names.end(), i_name) != m_names.end();
}
// is the scan code of this key ?
bool Key::isSameScanCode(const Key &i_key) const
{
if (m_scanCodesSize != i_key.m_scanCodesSize)
return false;
return isPrefixScanCode(i_key);
}
// is the key's scan code the prefix of this key's scan code ?
bool Key::isPrefixScanCode(const Key &i_key) const
{
for (size_t i = 0; i < i_key.m_scanCodesSize; ++ i)
if (m_scanCodes[i] != i_key.m_scanCodes[i])
return false;
return true;
}
// stream output
tostream &operator<<(tostream &i_ost, const Key &i_mk)
{
return i_ost << i_mk.getName();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Modifier
Modifier::Modifier()
: m_modifiers(0),
m_dontcares(0)
{
ASSERT(Type_end <= (sizeof(MODIFIERS) * 8));
static const Type defaultDontCare[] =
{
Type_Up, Type_Down, Type_Repeat,
Type_ImeLock, Type_ImeComp, Type_NumLock, Type_CapsLock, Type_ScrollLock,
Type_KanaLock,
Type_Maximized, Type_Minimized, Type_MdiMaximized, Type_MdiMinimized,
Type_Lock0, Type_Lock1, Type_Lock2, Type_Lock3, Type_Lock4,
Type_Lock5, Type_Lock6, Type_Lock7, Type_Lock8, Type_Lock9,
};
for (size_t i = 0; i < NUMBER_OF(defaultDontCare); ++ i)
dontcare(defaultDontCare[i]);
}
// add m's modifiers where this dontcare
void Modifier::add(const Modifier &i_m)
{
for (int i = 0; i < Type_end; ++ i)
{
if (isDontcare(static_cast<Modifier::Type>(i)))
if (!i_m.isDontcare(static_cast<Modifier::Type>(i)))
if (i_m.isPressed(static_cast<Modifier::Type>(i)))
press(static_cast<Modifier::Type>(i));
else
release(static_cast<Modifier::Type>(i));
}
}
// stream output
tostream &operator<<(tostream &i_ost, const Modifier &i_m)
{
struct Mods
{
Modifier::Type m_mt;
const _TCHAR *m_symbol;
};
const static Mods mods[] =
{
{ Modifier::Type_Up, _T("U-") }, { Modifier::Type_Down, _T("D-") },
{ Modifier::Type_Shift, _T("S-") }, { Modifier::Type_Alt, _T("A-") },
{ Modifier::Type_Control, _T("C-") }, { Modifier::Type_Windows, _T("W-") },
{ Modifier::Type_Repeat, _T("R-") },
{ Modifier::Type_ImeLock, _T("IL-") },
{ Modifier::Type_ImeComp, _T("IC-") },
{ Modifier::Type_ImeComp, _T("I-") },
{ Modifier::Type_NumLock, _T("NL-") },
{ Modifier::Type_CapsLock, _T("CL-") },
{ Modifier::Type_ScrollLock, _T("SL-") },
{ Modifier::Type_KanaLock, _T("KL-") },
{ Modifier::Type_Maximized, _T("MAX-") },
{ Modifier::Type_Minimized, _T("MIN-") },
{ Modifier::Type_MdiMaximized, _T("MMAX-") },
{ Modifier::Type_MdiMinimized, _T("MMIN-") },
{ Modifier::Type_Mod0, _T("M0-") }, { Modifier::Type_Mod1, _T("M1-") },
{ Modifier::Type_Mod2, _T("M2-") }, { Modifier::Type_Mod3, _T("M3-") },
{ Modifier::Type_Mod4, _T("M4-") }, { Modifier::Type_Mod5, _T("M5-") },
{ Modifier::Type_Mod6, _T("M6-") }, { Modifier::Type_Mod7, _T("M7-") },
{ Modifier::Type_Mod8, _T("M8-") }, { Modifier::Type_Mod9, _T("M9-") },
{ Modifier::Type_Lock0, _T("L0-") }, { Modifier::Type_Lock1, _T("L1-") },
{ Modifier::Type_Lock2, _T("L2-") }, { Modifier::Type_Lock3, _T("L3-") },
{ Modifier::Type_Lock4, _T("L4-") }, { Modifier::Type_Lock5, _T("L5-") },
{ Modifier::Type_Lock6, _T("L6-") }, { Modifier::Type_Lock7, _T("L7-") },
{ Modifier::Type_Lock8, _T("L8-") }, { Modifier::Type_Lock9, _T("L9-") },
};
for (size_t i = 0; i < NUMBER_OF(mods); ++ i)
if (!i_m.isDontcare(mods[i].m_mt) && i_m.isPressed(mods[i].m_mt))
i_ost << mods[i].m_symbol;
#if 0
else if (!i_m.isDontcare(mods[i].m_mt) && i_m.isPressed(mods[i].m_mt))
i_ost << _T("~") << mods[i].m_symbol;
else
i_ost << _T("*") << mods[i].m_symbol;
#endif
return i_ost;
}
/// stream output
tostream &operator<<(tostream &i_ost, Modifier::Type i_type)
{
const _TCHAR *modNames[] =
{
_T("Shift"),
_T("Alt"),
_T("Control"),
_T("Windows"),
_T("Up"),
_T("Down"),
_T("Repeat"),
_T("ImeLock"),
_T("ImeComp"),
_T("NumLock"),
_T("CapsLock"),
_T("ScrollLock"),
_T("KanaLock"),
_T("Maximized"),
_T("Minimized"),
_T("MdiMaximized"),
_T("MdiMinimized"),
_T("Mod0"),
_T("Mod1"),
_T("Mod2"),
_T("Mod3"),
_T("Mod4"),
_T("Mod5"),
_T("Mod6"),
_T("Mod7"),
_T("Mod8"),
_T("Mod9"),
_T("Lock0"),
_T("Lock1"),
_T("Lock2"),
_T("Lock3"),
_T("Lock4"),
_T("Lock5"),
_T("Lock6"),
_T("Lock7"),
_T("Lock8"),
_T("Lock9"),
};
size_t i = static_cast<size_t>(i_type);
if (0 <= i && i < NUMBER_OF(modNames))
i_ost << modNames[i];
return i_ost;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ModifiedKey
// stream output
tostream &operator<<(tostream &i_ost, const ModifiedKey &i_mk)
{
if (i_mk.m_key)
i_ost << i_mk.m_modifier << *i_mk.m_key;
return i_ost;
}
// ハッシュテーブル内の全てのキーコードを列挙する
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Keyboard::KeyIterator
Keyboard::KeyIterator::KeyIterator(Keys *i_hashedKeys, size_t i_hashedKeysSize)
: m_hashedKeys(i_hashedKeys),
m_hashedKeysSize(i_hashedKeysSize),
m_i((*m_hashedKeys).begin())
{
if ((*m_hashedKeys).empty())
next();
}
// 次の "スキャンコード"
void Keyboard::KeyIterator::next()
{
if (m_hashedKeysSize == 0)
return;
++ m_i;
if (m_i == (*m_hashedKeys).end())
{
do
{
-- m_hashedKeysSize;
++ m_hashedKeys;
} while (0 < m_hashedKeysSize && (*m_hashedKeys).empty()); // ハッシュテーブルの空要素を飛ばす
if (0 < m_hashedKeysSize)
m_i = (*m_hashedKeys).begin();
}
}
// 現在へのスキャンコードを取得
Key *Keyboard::KeyIterator::operator *()
{
if (m_hashedKeysSize == 0)
return NULL;
return &*m_i;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Keyboard
// 入力されたキーから始まるscan codeのKeyのリストを取得
Keyboard::Keys &Keyboard::getKeys(const Key &i_key)
{
ASSERT(1 <= i_key.getScanCodesSize());
return m_hashedKeys[i_key.getScanCodes()->m_scan % HASHED_KEYS_SIZE];
}
// add a key
void Keyboard::addKey(const Key &i_key)
{
getKeys(i_key).push_front(i_key);
}
// add a key name alias
void Keyboard::addAlias(const tstringi &i_aliasName, Key *i_key)
{
m_aliases.insert(Aliases::value_type(i_aliasName, i_key));
}
// add substitute
void Keyboard::addSubstitute(const ModifiedKey &i_mkeyFrom,
const ModifiedKey &i_mkeyTo)
{
m_substitutes.push_front(Substitute(i_mkeyFrom, i_mkeyTo));
}
// add a modifier key
void Keyboard::addModifier(Modifier::Type i_mt, Key *i_key)
{
ASSERT((int)i_mt < (int)Modifier::Type_BASIC);
if (std::find(m_mods[i_mt].begin(), m_mods[i_mt].end(), i_key)
!= m_mods[i_mt].end())
return; // already added
m_mods[i_mt].push_back(i_key);
}
// search a key
Key *Keyboard::searchKey(const Key &i_key)
{
Keys &keys = getKeys(i_key);
for (Keys::iterator i = keys.begin(); i != keys.end(); ++ i)
if ((*i).isSameScanCode(i_key))
return &*i;
return NULL;
}
// search a key (of which the key's scan code is the prefix)
Key *Keyboard::searchPrefixKey(const Key &i_key)
{
Keys &keys = getKeys(i_key);
for (Keys::iterator i = keys.begin(); i != keys.end(); ++ i)
if ((*i).isPrefixScanCode(i_key))
return &*i;
return NULL;
}
// search a key by name
Key *Keyboard::searchKey(const tstringi &i_name)
{
Aliases::iterator i = m_aliases.find(i_name);
if (i != m_aliases.end())
return (*i).second;
return searchKeyByNonAliasName(i_name);
}
// search a key by non-alias name
Key *Keyboard::searchKeyByNonAliasName(const tstringi &i_name)
{
for (int j = 0; j < HASHED_KEYS_SIZE; ++ j)
{
Keys &keys = m_hashedKeys[j];
Keys::iterator i = std::find(keys.begin(), keys.end(), i_name);
if (i != keys.end())
return &*i;
}
return NULL;
}
/// search a substitute
ModifiedKey Keyboard::searchSubstitute(const ModifiedKey &i_mkey)
{
for (Substitutes::const_iterator
i = m_substitutes.begin(); i != m_substitutes.end(); ++ i)
if (i->m_mkeyFrom.m_key == i_mkey.m_key &&
i->m_mkeyFrom.m_modifier.doesMatch(i_mkey.m_modifier))
return i->m_mkeyTo;
return ModifiedKey(); // not found (.m_mkey is NULL)
}