-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout.h
204 lines (185 loc) · 6.07 KB
/
layout.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
#ifndef LAYOUT_H
#define LAYOUT_H
#include <cstring>
#include "events.h"
#include "lighter.h"
#include "matrix.h"
#include "translator.h"
static const int kMaxLayers = 10;
static const int kMaxTaps = 10;
// Tapping on a modifier (including fn) results in a regular key.
struct Tap {
unsigned char modifier;
unsigned char key;
};
template <int R, int C>
struct Layer {
const char* name;
int layer_id;
int fn_layer_id;
const char* translator;
unsigned char keys[R][C];
Tap taps[kMaxTaps];
int freq[R][C];
};
template <int R, int C>
class Layout {
public:
Layout(int num_layers, const Layer<R, C>* layers,
int num_translators, Translator* translators[], Lighter* lighter = nullptr)
: num_layers_(num_layers),
num_translators_(num_translators),
translators_(translators),
lighter_(lighter) {
for (int i = 0; i < num_layers_; ++i) layers_[i] = &layers[i];
cur_layer_ = FindLayer(l0);
}
Events Interpret(int num_entries, const Entry* entries) {
events_.ClearTapping();
UpdateFnState(num_entries, entries);
const auto* layer = fn_pressed_ ? FindLayer(cur_layer_->fn_layer_id)
: cur_layer_;
for (int i = 0; i < num_entries; ++i) Interpret(entries[i], layer);
return events_;
}
Translator* translator() {
for (int i = 0; i < num_translators_; ++i) {
if (translators_[i] &&
strcmp(cur_layer_->translator, translators_[i]->name()) == 0)
return translators_[i];
}
return nullptr;
}
private:
void UpdateFnState(int num_entries, const Entry* entries) {
for (int i = 0; i < num_entries; ++i) {
int key = key_map[cur_layer_->keys[entries[i].row][entries[i].col]];
if (key == lfn || key == rfn) {
fn_pressed_ = entries[i].pressed;
if (IsDualRole(key)) {
if (entries[i].pressed) {
tapping_modifier_ = key;
if (events_.HasAnyKey()) CheckTapping(key);
} else {
CheckTapping(key);
}
}
break;
}
if (key == fnl && entries[i].pressed) {
if (cur_layer_->fn_layer_id != cur_layer_->layer_id) {
const auto* fn_layer = FindLayer(cur_layer_->fn_layer_id);
base_layer_ = cur_layer_;
cur_layer_ = fn_layer;
} else {
cur_layer_ = base_layer_;
}
LightForLayer(cur_layer_);
break;
}
}
}
bool IsDualRole(int key) const {
if (!IsModifier(key) && key != lfn && key != rfn) return false;
const auto& taps = cur_layer_->taps;
for (int i = 0; i < kMaxTaps; ++i) {
if (taps[i].modifier == 0) return false;
if (key_map[taps[i].modifier] == key) return true;
}
return false;
}
void CheckTapping(int modifier, const Layer<R,C>* layer = nullptr) {
if (tapping_modifier_ != modifier) return;
const auto& taps = layer ? layer->taps : cur_layer_->taps;
for (int i = 0; i < kMaxTaps; ++i) {
if (taps[i].modifier == 0) return;
if (key_map[taps[i].modifier] == modifier) {
events_.AddTappedKey(key_map[taps[i].key]);
tapping_modifier_ = 0;
return;
}
}
}
void Interpret(const Entry& entry, const Layer<R,C>* layer) {
if (entry.pressed) {
++const_cast<Layer<R,C>*>(layer)->freq[entry.row][entry.col];
// Clear the shifted modifier as soon as the next key's pressed.
if (shifted_key_) {
events_.modifiers &= ~MODIFIERKEY_SHIFT;
shifted_key_ = false;
}
}
int key = key_map[layer->keys[entry.row][entry.col]];
if (!key || key == fnl || key == lfn || key == rfn) return;
if (l0 <= key && key < l0 + kMaxLayers) {
events_.RemoveReleasedKey(entry.row, entry.col);
auto new_layer = FindLayer(key);
if (new_layer != cur_layer_) {
cur_layer_ = new_layer;
fn_pressed_ = false;
tapping_modifier_ = 0;
events_.Reset();
LightForLayer(cur_layer_);
}
return;
}
if (mlc <= key && key <= msd) {
tapping_modifier_ = 0;
events_.buttons[key - mlc] = entry.pressed;
return;
}
// TODO: Handle releasing a modifier that doesn't exist in fn layer.
if (IsDualRole(key)) {
if (entry.pressed) {
tapping_modifier_ = key;
if (events_.HasAnyKey()) CheckTapping(key, layer);
else events_.modifiers |= key;
} else {
events_.modifiers &= ~key;
CheckTapping(key, layer);
}
} else if (IsModifier(key)) {
if (entry.pressed) events_.modifiers |= key;
else events_.modifiers &= ~key;
} else {
tapping_modifier_ = 0;
if (entry.pressed) {
events_.AddPressedKey(key > 0 ? key : -key, entry.row, entry.col);
if (key < 0) {
events_.modifiers |= MODIFIERKEY_SHIFT;
shifted_key_ = true;
}
} else {
events_.RemoveReleasedKey(entry.row, entry.col);
}
}
}
bool IsModifier(int key) const {
return MODIFIERKEY_CTRL <= key && key <= MODIFIERKEY_RIGHT_GUI;
}
const Layer<R,C>* FindLayer(int layer_id) const {
for (int i = 0; i < num_layers_; ++i) {
if (layers_[i]->layer_id == layer_id) return layers_[i];
}
return nullptr;
}
void LightForLayer(const Layer<R,C>* layer) const {
if (!lighter_) return;
int index = layer->layer_id - l0;
for (int bit = 0; bit < lighter_->num_leds(); ++bit, index >>= 1) {
lighter_->Led(bit, index & 1);
}
}
const int num_layers_ = 0;
const Layer<R,C>* layers_[kMaxLayers];
const Layer<R,C>* cur_layer_ = nullptr;
const Layer<R,C>* base_layer_ = nullptr;
const int num_translators_ = 0;
Translator** const translators_ = nullptr;
Lighter* const lighter_ = nullptr;
bool fn_pressed_ = false;
bool shifted_key_ = false;
int tapping_modifier_ = 0;
Events events_;
};
#endif