Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/writing-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions freeink/DessenFreeInk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(x) << 8) | (static_cast<uint32_t>(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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<uint32_t>(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
}
Expand Down
5 changes: 5 additions & 0 deletions freeink/DessenFreeInk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions freeink/test_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(host.exitCode());
assert(DSN_EV_TYPE(event) == static_cast<uint32_t>(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);
Expand Down Expand Up @@ -454,6 +479,17 @@ int main(int argc, char** argv) {
assert(host.step(target, bezel, edge) == dessen::State::Exited);
assert(static_cast<uint32_t>(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<uint32_t>(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).
Expand Down
2 changes: 1 addition & 1 deletion sdk/dessen_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down