Merge pull request #41 from jpirnay/fix-powerleak

fix: Fix power button leak on startup
This commit is contained in:
jpirnay
2026-04-08 10:48:50 +02:00
committed by GitHub
7 changed files with 57 additions and 0 deletions
@@ -48,6 +48,10 @@ int clampPercent(int percent) {
void EpubReaderActivity::onEnter() {
Activity::onEnter();
// Drop any input events that arrived from the activity that launched us (e.g. a wake-up power
// button hold) before they reach detectPageTurn() — see ReaderUtils::InputDrainGuard.
inputDrainGuard.arm();
if (!epub) {
return;
}
@@ -118,6 +122,10 @@ void EpubReaderActivity::loop() {
return;
}
if (inputDrainGuard.shouldDrain(mappedInput)) {
return;
}
if (automaticPageTurnActive) {
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm) ||
mappedInput.wasReleased(MappedInputManager::Button::Back)) {
@@ -6,6 +6,7 @@
#include <optional>
#include "EpubReaderMenuActivity.h"
#include "ReaderUtils.h"
#include "activities/Activity.h"
class EpubReaderActivity final : public Activity {
@@ -45,6 +46,7 @@ class EpubReaderActivity final : public Activity {
uint16_t pendingParagraphIndex = 0;
bool pendingScreenshot = false;
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
ReaderUtils::InputDrainGuard inputDrainGuard;
bool automaticPageTurnActive = false;
std::string deferredSyncEpubPath;
// -1 means use global SETTINGS value.
+27
View File
@@ -29,6 +29,33 @@ inline void applyOrientation(GfxRenderer& renderer, const uint8_t orientation) {
}
}
// Suppresses input processing on activity entry until the user has released all buttons and a
// clean frame (no pending press/release events) has been observed. Without this, the power-button
// hold used to wake the device leaks into detectPageTurn() and triggers a page turn or, with
// longPressChapterSkip enabled, a chapter skip (the wake-hold easily exceeds skipChapterMs).
// Each reader holds an instance, calls arm() in onEnter(), and calls shouldDrain() at the top
// of loop() — returning early when it returns true.
struct InputDrainGuard {
bool active = false;
void arm() { active = true; }
bool shouldDrain(const MappedInputManager& input) {
if (!active) {
return false;
}
using B = MappedInputManager::Button;
const bool anyHeld = input.isPressed(B::Back) || input.isPressed(B::Confirm) || input.isPressed(B::Left) ||
input.isPressed(B::Right) || input.isPressed(B::Up) || input.isPressed(B::Down) ||
input.isPressed(B::Power) || input.isPressed(B::PageBack) || input.isPressed(B::PageForward);
if (anyHeld || input.wasAnyPressed() || input.wasAnyReleased()) {
return true;
}
active = false;
return false;
}
};
struct PageTurnResult {
bool prev;
bool next;
@@ -84,6 +84,10 @@ size_t parseAndWrapLines(const uint8_t* buffer, size_t chunkSize, size_t fileOff
void TxtReaderActivity::onEnter() {
Activity::onEnter();
// See ReaderUtils::InputDrainGuard — prevents wake-up power-button hold from leaking into
// the first detectPageTurn() call as a page turn or chapter skip.
inputDrainGuard.arm();
if (!txt) {
return;
}
@@ -117,6 +121,10 @@ void TxtReaderActivity::onExit() {
}
void TxtReaderActivity::loop() {
if (inputDrainGuard.shouldDrain(mappedInput)) {
return;
}
// Long press BACK (1s+) goes to home screen
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= ReaderUtils::GO_HOME_MS) {
ReaderUtils::enforceExitFullRefresh(renderer);
@@ -5,6 +5,7 @@
#include <vector>
#include "CrossPointSettings.h"
#include "ReaderUtils.h"
#include "activities/Activity.h"
class TxtReaderActivity final : public Activity {
@@ -13,6 +14,7 @@ class TxtReaderActivity final : public Activity {
int currentPage = 0;
int totalPages = 1;
int pagesUntilFullRefresh = 0;
ReaderUtils::InputDrainGuard inputDrainGuard;
// Streaming text reader - stores file offsets for each page
std::vector<size_t> pageOffsets; // File offset for start of each page
@@ -29,6 +29,10 @@ constexpr unsigned long goHomeMs = 1000;
void XtcReaderActivity::onEnter() {
Activity::onEnter();
// See ReaderUtils::InputDrainGuard — prevents wake-up power-button hold from leaking into
// the first detectPageTurn() call as a page turn or chapter skip.
inputDrainGuard.arm();
if (!xtc) {
return;
}
@@ -58,6 +62,10 @@ void XtcReaderActivity::onExit() {
}
void XtcReaderActivity::loop() {
if (inputDrainGuard.shouldDrain(mappedInput)) {
return;
}
// Enter chapter selection activity
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (xtc && xtc->hasChapters() && !xtc->getChapters().empty()) {
@@ -9,6 +9,7 @@
#include <Xtc.h>
#include "ReaderUtils.h"
#include "activities/Activity.h"
class XtcReaderActivity final : public Activity {
@@ -16,6 +17,7 @@ class XtcReaderActivity final : public Activity {
uint32_t currentPage = 0;
int pagesUntilFullRefresh = 0;
ReaderUtils::InputDrainGuard inputDrainGuard;
void renderPage();
void saveProgress() const;