-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathMappedInputManager.cpp
More file actions
368 lines (326 loc) · 13.8 KB
/
Copy pathMappedInputManager.cpp
File metadata and controls
368 lines (326 loc) · 13.8 KB
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
#include "MappedInputManager.h"
#include <BoardConfig.h>
#include <FreeInkUICore.h>
#include <GfxRenderer.h>
#include <algorithm>
#include <cstdlib>
#include "CrossPointSettings.h"
#include "components/UITheme.h"
namespace fui = freeink::ui;
bool MappedInputManager::isNavDirectionSwapped() const {
// Key the swap on the orientation the screen is *actually* rendered at, not the persisted reader
// setting. The reader (and its modal menus) render rotated, so navigation/labels flip there; the
// home and settings UI render in portrait, so they never flip even when a rotated reader is configured.
const auto orientation = renderer.getOrientation();
return SETTINGS.frontButtonFollowOrientation &&
(orientation == GfxRenderer::PortraitInverted || orientation == GfxRenderer::LandscapeCounterClockwise);
}
MappedInputManager::Button MappedInputManager::mapScreenDirection(const Button button) const {
// Rows follow GfxRenderer::Orientation's declared order.
static constexpr Button directions[][4] = {
{Button::Left, Button::Right, Button::Up, Button::Down},
{Button::Down, Button::Up, Button::Left, Button::Right},
{Button::Right, Button::Left, Button::Down, Button::Up},
{Button::Up, Button::Down, Button::Right, Button::Left},
};
uint8_t direction = 0;
switch (button) {
case Button::ScreenLeft:
direction = 0;
break;
case Button::ScreenRight:
direction = 1;
break;
case Button::ScreenUp:
direction = 2;
break;
case Button::ScreenDown:
direction = 3;
break;
default:
return button;
}
const uint8_t orientation =
SETTINGS.frontButtonFollowOrientation ? static_cast<uint8_t>(renderer.getOrientation()) : 0;
return directions[orientation][direction];
}
bool MappedInputManager::mapButton(const Button button, bool (HalGPIO::*fn)(uint8_t) const) const {
const auto sideLayout = SETTINGS.sideButtonLayout;
switch (button) {
case Button::Back:
// Logical Back maps to user-configured front button.
return (gpio.*fn)(SETTINGS.frontButtonBack);
case Button::Confirm:
// Logical Confirm maps to user-configured front button.
return (gpio.*fn)(SETTINGS.frontButtonConfirm);
case Button::Left:
// Logical Left maps to user-configured front button.
return (gpio.*fn)(SETTINGS.frontButtonLeft);
case Button::Right:
// Logical Right maps to user-configured front button.
return (gpio.*fn)(SETTINGS.frontButtonRight);
case Button::Up:
// Side buttons remain fixed for Up/Down.
return (gpio.*fn)(HalGPIO::BTN_UP);
case Button::Down:
// Side buttons remain fixed for Up/Down.
return (gpio.*fn)(HalGPIO::BTN_DOWN);
case Button::Power:
// Power button bypasses remapping.
return (gpio.*fn)(HalGPIO::BTN_POWER);
case Button::PageBack:
// Reader page navigation uses side buttons and can be swapped via settings.
switch (sideLayout) {
case CrossPointSettings::PREV_NEXT:
return (gpio.*fn)(HalGPIO::BTN_UP);
case CrossPointSettings::NEXT_PREV:
return (gpio.*fn)(HalGPIO::BTN_DOWN);
case CrossPointSettings::SIDE_BUTTONS_DISABLED:
default:
return false;
}
case Button::PageForward:
// Reader page navigation uses side buttons and can be swapped via settings.
switch (sideLayout) {
case CrossPointSettings::PREV_NEXT:
return (gpio.*fn)(HalGPIO::BTN_DOWN);
case CrossPointSettings::NEXT_PREV:
return (gpio.*fn)(HalGPIO::BTN_UP);
case CrossPointSettings::SIDE_BUTTONS_DISABLED:
default:
return false;
}
case Button::NavNext:
// Logical "next item" navigation: side Down + front Right, with the control axis flipped in
// INVERTED / LANDSCAPE_CCW (frontButtonFollowOrientation) so it matches the rotated hint labels.
return isNavDirectionSwapped() ? (mapButton(Button::Up, fn) || mapButton(Button::Left, fn))
: (mapButton(Button::Down, fn) || mapButton(Button::Right, fn));
case Button::NavPrevious:
// Logical "previous item" navigation: side Up + front Left, axis-flipped in the same orientations.
return isNavDirectionSwapped() ? (mapButton(Button::Down, fn) || mapButton(Button::Right, fn))
: (mapButton(Button::Up, fn) || mapButton(Button::Left, fn));
case Button::ScreenLeft:
case Button::ScreenRight:
case Button::ScreenUp:
case Button::ScreenDown:
return mapButton(mapScreenDirection(button), fn);
}
return false;
}
namespace {
constexpr unsigned long TOUCH_DOWN_SELECT_DELAY_MS = 90;
constexpr unsigned long TOUCH_HELD_OVERRIDE_WINDOW_MS = 250;
} // namespace
bool MappedInputManager::hasTouch() const { return gpio.hasTouch(); }
void MappedInputManager::rememberTouchHeldTime() const {
touchHeldOverrideValid = true;
touchHeldOverrideMs = gpio.lastTouchHeldMs();
touchHeldOverrideAt = millis();
}
bool MappedInputManager::wasScreenTapped(int& x, int& y) const {
float nx = 0.0f;
float ny = 0.0f;
if (!gpio.wasTouchTap(nx, ny)) return false;
renderer.tapToLogical(nx, ny, x, y);
rememberTouchHeldTime();
return true;
}
bool MappedInputManager::wasScreenTouchDown(int& x, int& y) const {
float nx = 0.0f;
float ny = 0.0f;
unsigned long heldMs = 0;
if (!gpio.isTouchTapCandidate(nx, ny, heldMs)) return false;
if (heldMs < TOUCH_DOWN_SELECT_DELAY_MS) return false;
renderer.tapToLogical(nx, ny, x, y);
return true;
}
bool MappedInputManager::wasScreenLongPress(int& x, int& y) const {
float nx = 0.0f;
float ny = 0.0f;
if (!gpio.wasTouchLongPress(nx, ny)) return false;
// Consuming the long-press implies acting on it: suppress the rest of the
// contact so the finger lift can't also tap whatever the action opened.
gpio.suppressTouchContact();
renderer.tapToLogical(nx, ny, x, y);
return true;
}
bool MappedInputManager::isScreenTouchHeld(int& x, int& y) const {
// Live contact position while the finger is down (no tap-slop gate) — drag tracking.
float nx = 0.0f;
float ny = 0.0f;
if (!gpio.isTouchHeldAt(nx, ny)) return false;
renderer.tapToLogical(nx, ny, x, y);
return true;
}
bool MappedInputManager::wasScreenTouchReleased() const { return gpio.wasTouchReleased(); }
bool MappedInputManager::wasTapInRect(const int x, const int y, const int width, const int height) const {
int tx = 0;
int ty = 0;
return wasScreenTapped(tx, ty) && tx >= x && tx < x + width && ty >= y && ty < y + height;
}
MappedInputManager::RowTouch MappedInputManager::rowTouch(int& row, const int top, const int rowStep,
const int rowCount, const int xStart, const int xEnd,
const int rowHeight) const {
if (rowStep <= 0 || rowCount <= 0) return RowTouch::None;
const auto hit = [&](const int x, const int y) {
if (x < xStart || x >= xEnd || y < top) return false;
const int r = (y - top) / rowStep;
if (r >= rowCount) return false;
if (rowHeight > 0 && (y - top) % rowStep >= rowHeight) return false;
row = r;
return true;
};
int x = 0;
int y = 0;
if (wasScreenTouchDown(x, y) && hit(x, y)) return RowTouch::Down;
if (wasScreenTapped(x, y) && hit(x, y)) return RowTouch::Tap;
return RowTouch::None;
}
MappedInputManager::RowTouch MappedInputManager::colTouch(int& col, const int left, const int colStep,
const int colCount, const int yStart, const int yEnd,
const int colWidth) const {
if (colStep <= 0 || colCount <= 0) return RowTouch::None;
const auto hit = [&](const int x, const int y) {
if (y < yStart || y >= yEnd || x < left) return false;
const int c = (x - left) / colStep;
if (c >= colCount) return false;
if (colWidth > 0 && (x - left) % colStep >= colWidth) return false;
col = c;
return true;
};
int x = 0;
int y = 0;
if (wasScreenTouchDown(x, y) && hit(x, y)) return RowTouch::Down;
if (wasScreenTapped(x, y) && hit(x, y)) return RowTouch::Tap;
return RowTouch::None;
}
bool MappedInputManager::decodeSwipe(int& sx, int& sy, int& ex, int& ey) const {
float nxs = 0.0f;
float nys = 0.0f;
float nxe = 0.0f;
float nye = 0.0f;
if (!gpio.wasSwipe(nxs, nys, nxe, nye)) return false;
renderer.tapToLogical(nxs, nys, sx, sy);
renderer.tapToLogical(nxe, nye, ex, ey);
return true;
}
MappedInputManager::SwipeDir MappedInputManager::wasSwipe() const {
int sx = 0;
int sy = 0;
int ex = 0;
int ey = 0;
if (!decodeSwipe(sx, sy, ex, ey)) return SwipeDir::None;
switch (fui::swipeDirection(sx, sy, ex, ey)) {
case fui::SwipeDir::Left:
return SwipeDir::Left;
case fui::SwipeDir::Right:
return SwipeDir::Right;
case fui::SwipeDir::Up:
return SwipeDir::Up;
case fui::SwipeDir::Down:
return SwipeDir::Down;
default:
return SwipeDir::None;
}
}
// Edge classification (which swipe counts as an edge gesture) lives in the
// SDK; only the MEANING of each edge — back, menu, home, light panel, and the
// home-key remap — is decided here.
bool MappedInputManager::wasEdgeSwipe(const freeink::ui::ScreenEdge edge) const {
int sx = 0;
int sy = 0;
int ex = 0;
int ey = 0;
if (!decodeSwipe(sx, sy, ex, ey)) return false;
const bool hit = fui::edgeSwipe(edge, sx, sy, ex, ey, renderer.getScreenWidth(), renderer.getScreenHeight());
if (hit) rememberTouchHeldTime();
return hit;
}
bool MappedInputManager::wasBackGesture() const {
// Back = left-to-right swipe starting near the left edge. Edge-anchored so that
// mid-screen horizontal swipes stay available to activities that consume
// SwipeDir::Left/Right (e.g. percent selection, image viewer).
return wasEdgeSwipe(fui::ScreenEdge::Left);
}
bool MappedInputManager::wasTopEdgeDownSwipe() const { return wasEdgeSwipe(fui::ScreenEdge::Top); }
bool MappedInputManager::wasBottomEdgeUpSwipe() const { return wasEdgeSwipe(fui::ScreenEdge::Bottom); }
bool MappedInputManager::wasMenuGesture() const { return wasTopEdgeDownSwipe(); }
bool MappedInputManager::wasHomeGesture() const { return wasBottomEdgeUpSwipe(); }
bool MappedInputManager::wasPressed(const Button button) const {
if (button == Button::Back && wasBackGesture()) return true;
return mapButton(button, &HalGPIO::wasPressed);
}
bool MappedInputManager::wasReleased(const Button button) const {
if (button == Button::Back && wasBackGesture()) return true;
return mapButton(button, &HalGPIO::wasReleased);
}
bool MappedInputManager::isPressed(const Button button) const { return mapButton(button, &HalGPIO::isPressed); }
bool MappedInputManager::wasAnyPressed() const { return gpio.wasAnyPressed(); }
bool MappedInputManager::wasAnyReleased() const { return gpio.wasAnyReleased(); }
unsigned long MappedInputManager::getHeldTime() const {
if (!gpio.wasAnyPressed() && !gpio.wasAnyReleased() && touchHeldOverrideValid &&
millis() - touchHeldOverrideAt <= TOUCH_HELD_OVERRIDE_WINDOW_MS) {
return touchHeldOverrideMs;
}
touchHeldOverrideValid = false;
return gpio.getHeldTime();
}
MappedInputManager::Labels MappedInputManager::mapLabels(const char* back, const char* confirm, const char* previous,
const char* next) const {
// Swap previous/next labels to match the page turn direction swap in INVERTED and LANDSCAPE_CCW.
const bool swapLabels = isNavDirectionSwapped();
const char* leftLabel = swapLabels ? next : previous;
const char* rightLabel = swapLabels ? previous : next;
return mapFrontLabels(back, confirm, leftLabel, rightLabel);
}
MappedInputManager::Labels MappedInputManager::mapDirectionalLabels(const char* back, const char* confirm,
const char* left, const char* right, const char* up,
const char* down) const {
const auto labelForButton = [&](const Button rawButton) {
if (mapScreenDirection(Button::ScreenLeft) == rawButton) return left;
if (mapScreenDirection(Button::ScreenRight) == rawButton) return right;
if (mapScreenDirection(Button::ScreenUp) == rawButton) return up;
if (mapScreenDirection(Button::ScreenDown) == rawButton) return down;
return "";
};
return mapFrontLabels(back, confirm, labelForButton(Button::Left), labelForButton(Button::Right));
}
MappedInputManager::Labels MappedInputManager::mapFrontLabels(const char* back, const char* confirm, const char* left,
const char* right) const {
// Build the label order based on the configured hardware mapping.
auto labelForHardware = [&](uint8_t hw) -> const char* {
// Compare against configured logical roles and return the matching label.
if (hw == SETTINGS.frontButtonBack) {
return back;
}
if (hw == SETTINGS.frontButtonConfirm) {
return confirm;
}
if (hw == SETTINGS.frontButtonLeft) {
return left;
}
if (hw == SETTINGS.frontButtonRight) {
return right;
}
return "";
};
return {labelForHardware(HalGPIO::BTN_BACK), labelForHardware(HalGPIO::BTN_CONFIRM),
labelForHardware(HalGPIO::BTN_LEFT), labelForHardware(HalGPIO::BTN_RIGHT)};
}
int MappedInputManager::getPressedFrontButton() const {
// Scan the raw front buttons in hardware order.
// This bypasses remapping so the remap activity can capture physical presses.
if (gpio.wasPressed(HalGPIO::BTN_BACK)) {
return HalGPIO::BTN_BACK;
}
if (gpio.wasPressed(HalGPIO::BTN_CONFIRM)) {
return HalGPIO::BTN_CONFIRM;
}
if (gpio.wasPressed(HalGPIO::BTN_LEFT)) {
return HalGPIO::BTN_LEFT;
}
if (gpio.wasPressed(HalGPIO::BTN_RIGHT)) {
return HalGPIO::BTN_RIGHT;
}
return -1;
}