-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKbdGuiLayout.h
413 lines (376 loc) · 14.2 KB
/
KbdGuiLayout.h
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*****************************************************************************/
/* KbdGuiLayout.h - GUI layout definition for the keyboard */
/*****************************************************************************/
/*
Copyright (C) 2019 Hermann Seib
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _KbdGuiLayout_h___included_
#define _KbdGuiLayout_h___included_
#include "layout.h"
/*****************************************************************************/
/* MatrixKey : in BlUSB, one key in the matrix is an uint16_t (HID+type) */
/*****************************************************************************/
typedef wxUint16 MatrixKey;
/*****************************************************************************/
/* MacroKey : in BlUSB, one key in the macro is an uint8_t (HID?) */
/*****************************************************************************/
typedef wxUint8 MacroKey;
/*****************************************************************************/
/* KbdMatrix : class definition for a keyboard matrix */
/*****************************************************************************/
class KbdMatrix
{
public:
KbdMatrix(int rows = 0, int cols = 0, MatrixKey const *values = NULL)
: rows(0), cols(0)
{ Resize(rows, cols, values); }
KbdMatrix(KbdMatrix const &org)
: rows(0), cols(0)
{ DoCopy(org); }
virtual ~KbdMatrix() {}
KbdMatrix &operator=(KbdMatrix const &org)
{ return DoCopy(org); }
void Resize(int newrows = 0, int newcols = 0, MatrixKey const *values = NULL)
{
// Theoretically, we could copy the old contents to the new position here
// wxVector<MatrixKey> oldkeys(keys);
// int oldrows = rows, oldcols = cols;
newrows = max(min(newrows, MAXROWS), 0);
newcols = max(min(newcols, MAXCOLS), 0);
if (newrows * newcols &&
newrows * newcols > rows * cols)
keys.resize(newrows * newcols);
rows = newrows;
cols = newcols;
SetKeys(values);
}
int GetRows() const { return rows; }
int GetCols() const { return cols; }
MatrixKey &GetKeys() { return keys[0]; }
int GetKey(int row, int col) const
{ return (row < rows && col < cols) ? keys[row * cols + col] : KB_UNUSED; }
void SetKey(int row, int col, MatrixKey value)
{ if (row < rows && col < cols) keys[row * cols + col] = value; }
void SetKeys(MatrixKey const *values = NULL)
{
if (values)
{
for (int i = 0; i < rows * cols; i++)
keys[i] = values[i];
}
else
{
for (int i = 0; i < rows * cols; i++)
keys[i] = KB_UNUSED;
}
}
protected:
int rows, cols;
wxVector<MatrixKey> keys;
KbdMatrix &DoCopy(KbdMatrix const &org)
{
rows = org.rows;
cols = org.cols;
keys.assign(org.keys.begin(), org.keys.end());
return *this;
}
};
/*****************************************************************************/
/* KbdMacro : class definition for a keyboard macro */
/*****************************************************************************/
// TODO: QUITE a LOT still missing here. Including understanding macros 8-)
class KbdMacro
{
public:
KbdMacro(int mods = 0, int keys = 0, MacroKey const *values = NULL)
: mods(mods)
{ Resize(keys, values); }
void Resize(int newkeys = 0, MacroKey const *values = NULL)
{
// TODO: get that right!
newkeys = max(min(newkeys, 6), 0);
keys.resize(newkeys);
SetKeys(values);
}
void SetKeys(MacroKey const *values = NULL)
{
if (values)
{
for (size_t i = 0; i < keys.size(); i++)
keys[i] = values[i];
}
else
{
for (size_t i = 0; i < keys.size(); i++)
keys[i] = KB_UNUSED;
}
}
protected:
wxUint8 mods;
wxVector<MacroKey> keys;
KbdMacro &DoCopy(KbdMacro const &org)
{
mods = org.mods;
keys.assign(org.keys.begin(), org.keys.end());
return *this;
}
};
/*****************************************************************************/
/* KbdLayout : class definition for a keyboard layout (layers of matrices) */
/*****************************************************************************/
class KbdLayout
{
public:
// TODO: devise macro initialization functionality
KbdLayout(int layers = 0, int rows = 0, int cols = 0, int macros = 0, MatrixKey *values = NULL)
: layers(0), rows(0), cols(0), macros(0)
{
Resize(layers, rows, cols, macros, values);
SetModified(false);
}
KbdLayout(KbdLayout const &org)
{ DoCopy(org); }
virtual ~KbdLayout() {}
KbdLayout &operator=(KbdLayout const &org)
{ return DoCopy(org); }
KbdMatrix &operator[](int n) { return layer[n]; }
bool InsertLayer(int pos, int count = 1)
{
if (count <= 0 || count + layers > NUMLAYERS_MAX)
return false;
for (int i = 0; i < count; i++)
layer.insert(layer.begin() + pos + i, KbdMatrix(rows, cols));
layers += count;
SetModified();
return true;
}
bool RemoveLayer(int pos, int count = 1)
{
if (pos < 0 || count <= 0 || pos >= layers)
return false;
if (pos + count > layers)
count = layers - pos;
layer.erase(layer.begin() + pos, layer.begin() + pos + count - 1);
layers -= count;
SetModified();
return true;
}
bool AddLayer(int count = 1)
{ return InsertLayer(layers, count); }
// TODO: devise macro initialization functionality
void Resize(int layers = 0, int rows = 0, int cols = 0, int macros = 0, MatrixKey *values = NULL)
{
layers = max(min(layers, NUMLAYERS_MAX), 0);
rows = max(min(rows, MAXROWS), 0);
cols = max(min(cols, MAXCOLS), 0);
macros = max(min(macros, NUM_MACROKEYS), 0);
if (this->layers != layers ||
this->rows != rows ||
this->cols != cols)
SetModified();
this->layers = layers;
this->rows = rows;
this->cols = cols;
if (layers > 0 && rows > 0 && cols > 0)
{
for (int i = 0; i < layers; i++)
{
if (i >= (int)layer.size())
layer.push_back(KbdMatrix(rows, cols, values));
else
layer[i].Resize(rows, cols, values);
if (values)
values += (rows * cols);
}
}
}
int GetLayers() const { return layers; }
int GetMaxLayers() const { return (int)layer.size(); }
int GetRows() const { return rows; }
int GetCols() const { return cols; }
int GetMacros() const { return macros; }
MatrixKey &GetKeys(int layernum = 0) { return layer[layernum].GetKeys(); }
int GetKey(int layernum, int row, int col)
{ return layer[layernum].GetKey(row, col); }
void SetKey(int layernum, int row, int col, MatrixKey value)
{
if (layer[layernum].GetKey(row, col) != value)
SetModified();
return layer[layernum].SetKey(row, col, value);
}
KbdMacro &GetMacro(int macronum = 0) { return macro[macronum]; }
// TODO: some sort of macro getter/setter
// import layout in Model M USB transfer format
bool Import(wxUint8 *layout, int bufsize /* in bytes! */ = -1,
int tgtrows = NUMROWS, int tgtcols = NUMCOLS,
wxUint8 *macrobuf = NULL, int macrosize = 0,
int transformat = 0) // 0=V<1.5, 1=V>=1.5
{
int newLayers = layout[0]; // sanitize
newLayers = max(min(newLayers, NUMLAYERS_MAX), 0);
if (bufsize >= 0 &&
bufsize < (int)sizeof(wxUint16) * (1 + newLayers * tgtrows * tgtcols))
return false;
// sanitize incoming macros
int macros = macrobuf ? macrosize / LEN_MACRO : 0;
for (int mac = 0; mac < macros; mac++)
{
wxUint8 *pmac = macrobuf + mac * LEN_MACRO;
/* layout:
pmac[0] = mods
pmac[1] = reserved
pmac[2] .. pmac[7] = macro keys
*/
// sanity check: must not consist of FF only
int zeros = 0, ffs = 0;
for (int i = 0; i < LEN_MACRO; i++)
if (pmac[i] == 0xff)
ffs++;
// reformat uninitialized macro areas
if (ffs == LEN_MACRO)
memset(pmac, KB_UNUSED, LEN_MACRO);
}
Resize(newLayers, tgtrows, tgtcols, macros);
if (transformat == 1)
layout += sizeof(wxUint8);
else
layout += sizeof(wxUint16);
for (int l = 0; l < newLayers; l++)
for (int r = 0; r < tgtrows; r++)
for (int c = 0; c < tgtcols; c++)
{
// Controller sends LSB first, whatever our internal format might be
wxUint16 k = *layout++;
k += (*layout++) << 8;
SetKey(l, r, c, k);
}
// TODO: import the macros!
SetModified(false);
return true;
}
// export layout to Model M USB transfer format
bool Export(wxUint8 *buf, int &bufsize /* in bytes!*/,
int tgtrows = NUMROWS, int tgtcols = NUMCOLS,
int transformat = 1) // 0=V<1.5, 1=V>=1.5
{
if (bufsize < 1 + (int)sizeof(wxUint16) * (layers * tgtrows * tgtcols))
return false;
// restrict to maximum layers for the device!
int ilayers = layers;
if (transformat == 0 && ilayers > NUMLAYERS_MAX_OLD)
ilayers = NUMLAYERS_MAX_OLD;
bufsize = 1 + (int)sizeof(wxUint16) * (ilayers * tgtrows * tgtcols);
*buf++ = ilayers;
for (int l = 0; l < ilayers; l++)
for (int r = 0; r < tgtrows; r++)
for (int c = 0; c < tgtcols; c++)
{
wxUint16 k = GetKey(l, r, c);
// LSB first, whatever our internal format might be
*buf++ = k & 0xff;
*buf++ = k >> 8;
}
// TODO: export the macros!
return true;
}
bool ReadFile(wxString const &filename);
bool WriteFile(wxString const &filename, bool bNative = true,
int tgtrows = NUMROWS, int tgtcols = NUMCOLS);
bool IsModified() { return bModified; }
void SetModified(bool bOn = true) { bModified = bOn; }
protected:
bool bModified;
int layers, rows, cols, macros;
wxVector<KbdMatrix> layer;
wxVector<KbdMacro> macro;
KbdLayout &DoCopy(KbdLayout const &org)
{
layers = org.layers;
rows = org.rows;
cols = org.cols;
macros = org.macros;
layer.assign(org.layer.begin(), org.layer.end());
macro.assign(org.macro.begin(), org.macro.end());
SetModified(false);
return *this;
}
};
/*****************************************************************************/
/* GuiKey : definition for one key */
/*****************************************************************************/
struct GuiKey
{
int row; /* start row for the key */
float height; /* key height */
int hidcode; /* HID code for that key */
int matrixrow, matrixcol; /* matrix position for that key */
/* -1/-1 indicates empty space */
float width1, width2; /* top / bottom with for the key */
/* width2 is mainly for ISO Enter, */
/* which has 2 different sizes */
wxString label[2]; /* 2 labels */
/* 0 : key label text */
/* 1 : for logging etc. */
float startx1, starty1; /* top X/Y position for the key */
float startx2, starty2; /* bottom X/Y position for the key */
// startx2/y2 can be deduced from row/height, but if the height becomes
// non-integral at a later point, having them separately is a good thing.
};
/*****************************************************************************/
/* KbdGui : definition for the whole keyboard GUI */
/*****************************************************************************/
class KbdGui
{
public:
enum
{
KbdDefault = -1,
KbdANSI,
KbdISO,
KbdANSI121,
KbdISO122,
};
KbdGui(int kbdStyle = KbdDefault);
virtual ~KbdGui();
KbdGui(KbdGui const &org) { DoCopy(org); }
KbdGui &operator=(KbdGui const &org) { return DoCopy(org); }
GuiKey &operator[](int index) { return keys[index]; }
static void SetDefault(bool bISO = false, bool b122 = false);
// get the display name
wxString const &GetName() { return layoutName; }
size_t size() const { return keys.size(); }
GuiKey const &GetKey(int index) const { return keys[index]; }
float GetHorizontalUnits() { return unitsH; }
float GetVerticalUnits() { return unitsV; }
void GetMatrixLayout(int &nRows, int &nCols) const
{ nRows = nMaxRow + 1; nCols = nMaxCol + 1; }
int GetMaxRow() { return nMaxRow; }
int GetMaxCol() { return nMaxCol; }
bool ReadLayoutFile(wxString const &filename, wxString *error = NULL);
bool WriteLayoutFile(wxString const &filename);
// convert OS Virtual Key -> HID
static int GetHID(int /* wxKeyCode */ vkey);
// convert scan code -> HID
static int GetHIDFromScancode(int scancode);
protected:
KbdGui &DoCopy(KbdGui const &org);
void CalcLayout();
protected:
static GuiKey *pDefault;
static int nDefault;
static wxString sDefault;
wxString layoutName;
wxVector<GuiKey> keys;
float unitsV, unitsH;
int nMaxRow, nMaxCol;
};
#endif // !defined(_KbdGuiLayout_h___included_)