From 45e6cd5f6d4aa0e25a81697c6a4869f85dbbb6b3 Mon Sep 17 00:00:00 2001 From: jadehawk Date: Fri, 27 Nov 2026 09:33:44 -0500 Subject: [PATCH] feat: expose optional touch swipe origins --- docs/writing-apps.md | 4 +++- freeink/DessenFreeInk.cpp | 25 +++++++++++++++++-------- freeink/DessenFreeInk.h | 5 +++++ freeink/test_host.cpp | 36 ++++++++++++++++++++++++++++++++++++ sdk/dessen_abi.h | 2 +- 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/docs/writing-apps.md b/docs/writing-apps.md index 32f2617..bf0de2c 100644 --- a/docs/writing-apps.md +++ b/docs/writing-apps.md @@ -119,7 +119,9 @@ and a buttons-only one: - `TAP` / `LONG_PRESS` — a touch and a press-and-hold; both carry `x, y` (`DSN_EV_X/Y(e)`). - `SWIPE_UP` / `SWIPE_DOWN` / `SWIPE_LEFT` / `SWIPE_RIGHT` — the four touch - swipes (no coordinates). Use vertical swipes to **page/scroll**. + swipes. A host may provide the touch origin in safe-area coordinates via +`DSN_EV_X/Y(e)`; both zero also represent an absent origin, so apps must +retain a direction-only path. Use vertical swipes to **page/scroll**. A board with up/down keys *and* touch (most of them) delivers both, so these stay distinct: keys move the selection, a swipe pages, a tap opens. Always offer a diff --git a/freeink/DessenFreeInk.cpp b/freeink/DessenFreeInk.cpp index cdca867..487b9da 100644 --- a/freeink/DessenFreeInk.cpp +++ b/freeink/DessenFreeInk.cpp @@ -30,18 +30,24 @@ View viewOf(const DeviceContext& device) { return View{safe.width, safe.height, safe.x, safe.y}; } -uint32_t eventOf(const InputSnapshot& in, View view, FreeInkHost::Swipe swipe, bool sideways) { +uint32_t eventOf(const InputSnapshot& in, View view, FreeInkHost::Swipe swipe, bool sideways, int16_t swipeX, int16_t swipeY) { if (in.confirm) return DSN_EV_CONFIRM; if (in.back) return DSN_EV_BACK; + // Invalid or absent origins retain the direction-only event. The 12-bit + // coordinate fields must not wrap if the panel is unusually large. + const int32_t x = swipeX - view.originX, y = swipeY - view.originY; + const uint32_t origin = x >= 0 && y >= 0 && x < view.width && y < view.height && + x <= 0xfff && y <= 0xfff + ? (static_cast(x) << 8) | (static_cast(y) << 20) : 0; switch (swipe) { - case FreeInkHost::SwipeUp: return DSN_EV_SWIPE_UP; - case FreeInkHost::SwipeDown: return DSN_EV_SWIPE_DOWN; - case FreeInkHost::SwipeLeft: return DSN_EV_SWIPE_LEFT; - case FreeInkHost::SwipeRight: return DSN_EV_SWIPE_RIGHT; + case FreeInkHost::SwipeUp: return DSN_EV_SWIPE_UP | origin; + case FreeInkHost::SwipeDown: return DSN_EV_SWIPE_DOWN | origin; + case FreeInkHost::SwipeLeft: return DSN_EV_SWIPE_LEFT | origin; + case FreeInkHost::SwipeRight: return DSN_EV_SWIPE_RIGHT | origin; default: break; } - if (in.swipeLeft) return DSN_EV_SWIPE_LEFT; - if (in.swipeRight) return DSN_EV_SWIPE_RIGHT; + if (in.swipeLeft) return DSN_EV_SWIPE_LEFT | origin; + if (in.swipeRight) return DSN_EV_SWIPE_RIGHT | origin; const uint32_t key = sideways ? DSN_EV_SIDEWAYS : 0; if (in.prev || in.focusPrev) return DSN_EV_PREV | key; if (in.next || in.focusNext) return DSN_EV_NEXT | key; @@ -316,6 +322,7 @@ uint32_t FreeInkHost::nextRandom() { bool FreeInkHost::load(uint8_t* mem, uint32_t len, uint32_t memCap) { refresh_ = false; clipped_ = clipTouched_ = sideways_ = false; + swipeX_ = swipeY_ = -1; state_ = dvm_load(&vm_, mem, len, memCap) ? State::Running : State::Faulted; return state_ == State::Running; } @@ -329,13 +336,15 @@ State FreeInkHost::step(DrawTarget& target, const DeviceContext& device, const I uint32_t budget) { const bool sideways = sideways_; // about this step's input only, whatever the state sideways_ = false; + const int16_t swipeX = swipeX_, swipeY = swipeY_; + swipeX_ = swipeY_ = -1; if (state_ == State::Exited || state_ == State::Faulted) return state_; if (state_ == State::Busy) { const int code = services_ ? services_->poll() : DSN_ERR_IO; if (code == kPending) return state_; // still working; try again next tick vm_.x[10] = static_cast(code); // result ready: resume the VM below } else if (state_ == State::Waiting) { - const uint32_t ev = eventOf(input, viewOf(device), swipe, sideways); + const uint32_t ev = eventOf(input, viewOf(device), swipe, sideways, swipeX, swipeY); if (!ev) return state_; vm_.x[10] = ev; // wait_event's return value } diff --git a/freeink/DessenFreeInk.h b/freeink/DessenFreeInk.h index e891b88..eb3be55 100644 --- a/freeink/DessenFreeInk.h +++ b/freeink/DessenFreeInk.h @@ -51,6 +51,10 @@ class FreeInkHost { // the step consumes it whatever state the app is in. void markSideways() { sideways_ = true; } + // Optional touch-swipe origin in panel coordinates, consumed on the next + // step just like markSideways(). Direction-only hosts need not set it. + void markSwipeOrigin(int16_t x, int16_t y) { swipeX_ = x; swipeY_ = y; } + // Entropy for the app's random() call, e.g. esp_random. Without one the // app gets a weak fallback sequence, fine for tests and nothing else. void setRandom(uint32_t (*source)()) { random_ = source; } @@ -111,6 +115,7 @@ class FreeInkHost { bool clipped_ = false; bool clipTouched_ = false; bool sideways_ = false; + int16_t swipeX_ = -1, swipeY_ = -1; public: void randomBytesInto(uint8_t* out, uint32_t n); // fills from service or fallback diff --git a/freeink/test_host.cpp b/freeink/test_host.cpp index f722d5e..cb1eb5c 100644 --- a/freeink/test_host.cpp +++ b/freeink/test_host.cpp @@ -350,6 +350,31 @@ int main(int argc, char** argv) { assert(host.exitCode() == (left ? DSN_EV_SWIPE_LEFT : DSN_EV_SWIPE_RIGHT)); } + // Optional origins use the same safe-area coordinate space as taps. The + // metadata is one-shot and an out-of-area origin does not hide the swipe. + for (auto dir : {std::pair{Host::SwipeUp, DSN_EV_SWIPE_UP}, {Host::SwipeDown, DSN_EV_SWIPE_DOWN}, + {Host::SwipeLeft, DSN_EV_SWIPE_LEFT}, {Host::SwipeRight, DSN_EV_SWIPE_RIGHT}}) { + assert(host.load(mem, readApp(argv[1]), sizeof mem)); + while (host.step(target, screen, none) == dessen::State::Running) {} + host.markSwipeOrigin(123, 234); + assert(host.step(target, screen, none, dir.first) == dessen::State::Exited); + const uint32_t event = static_cast(host.exitCode()); + assert(DSN_EV_TYPE(event) == static_cast(dir.second)); + assert(DSN_EV_X(event) == 123 && DSN_EV_Y(event) == 234); + } + assert(host.load(mem, readApp(argv[1]), sizeof mem)); + while (host.step(target, screen, none) == dessen::State::Running) {} + host.markSwipeOrigin(-1, 123); + assert(host.step(target, screen, none, Host::SwipeUp) == dessen::State::Exited); + assert(host.exitCode() == DSN_EV_SWIPE_UP); + + assert(host.load(mem, readApp(argv[1]), sizeof mem)); + while (host.step(target, screen, none) == dessen::State::Running) {} + host.markSwipeOrigin(123, 234); + assert(host.step(target, screen, none) == dessen::State::Waiting); + assert(host.step(target, screen, none, Host::SwipeRight) == dessen::State::Exited); + assert(host.exitCode() == DSN_EV_SWIPE_RIGHT); + // oob: a sandbox escape attempt ends as a fault, not a crash. assert(host.load(mem, readApp(argv[2]), sizeof mem)); assert(host.step(target, screen, none) == dessen::State::Faulted); @@ -454,6 +479,17 @@ int main(int argc, char** argv) { assert(host.step(target, bezel, edge) == dessen::State::Exited); assert(static_cast(host.exitCode()) == (DSN_EV_TAP | 100u << 8 | 100u << 20)); + assert(host.load(mem, readApp(argv[1]), sizeof mem)); + while (host.step(target, bezel, none) == dessen::State::Running) {} + host.markSwipeOrigin(140, 110); + assert(host.step(target, bezel, none, Host::SwipeDown) == dessen::State::Exited); + assert(static_cast(host.exitCode()) == (DSN_EV_SWIPE_DOWN | 100u << 8 | 100u << 20)); + assert(host.load(mem, readApp(argv[1]), sizeof mem)); + while (host.step(target, bezel, none) == dessen::State::Running) {} + host.markSwipeOrigin(5, 5); + assert(host.step(target, bezel, none, Host::SwipeDown) == dessen::State::Exited); + assert(host.exitCode() == DSN_EV_SWIPE_DOWN); + // drawtest again inside the bezel: it checks dsn_device() against dsn_screen(), // and its clip moves inside the bezel with everything else. Without hint // slots from the firmware it reports none (its own stroke is w3). diff --git a/sdk/dessen_abi.h b/sdk/dessen_abi.h index 5f4ff56..bb30270 100644 --- a/sdk/dessen_abi.h +++ b/sdk/dessen_abi.h @@ -74,7 +74,7 @@ typedef struct { #define DSN_EV_NEXT 4 // Down button / focus-next #define DSN_EV_TAP 5 // carries x, y #define DSN_EV_LONG_PRESS 6 // press and hold; carries x, y like a tap -#define DSN_EV_SWIPE_UP 7 // the four directional touch swipes (no coordinates) +#define DSN_EV_SWIPE_UP 7 // touch swipes may carry their safe-area origin x/y #define DSN_EV_SWIPE_DOWN 8 #define DSN_EV_SWIPE_LEFT 9 #define DSN_EV_SWIPE_RIGHT 10