fix: ActivityManager tweaks (#1220)
## Summary **What is the goal of this PR?** Small tweaks to #1016: - Only Activity and ActivityManager can access activityResultHandler and activityResult - `[[maybe_unused]]` in RenderLock constructor - Only ActivityManager and RenderLock can access renderingMutex - Missing renderUpdate after failed wifi selection - Standardize on activities calling finish instead of activityManager.popActivity - Hold RenderLock while mutating state in EpubReaderActivity result handlers --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**NO**_
This commit is contained in:
@@ -13,15 +13,17 @@
|
|||||||
#include "RenderLock.h"
|
#include "RenderLock.h"
|
||||||
|
|
||||||
class Activity {
|
class Activity {
|
||||||
|
friend class ActivityManager;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string name;
|
std::string name;
|
||||||
GfxRenderer& renderer;
|
GfxRenderer& renderer;
|
||||||
MappedInputManager& mappedInput;
|
MappedInputManager& mappedInput;
|
||||||
|
|
||||||
public:
|
|
||||||
ActivityResultHandler resultHandler;
|
ActivityResultHandler resultHandler;
|
||||||
ActivityResult result;
|
ActivityResult result;
|
||||||
|
|
||||||
|
public:
|
||||||
explicit Activity(std::string name, GfxRenderer& renderer, MappedInputManager& mappedInput)
|
explicit Activity(std::string name, GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
: name(std::move(name)), renderer(renderer), mappedInput(mappedInput) {}
|
: name(std::move(name)), renderer(renderer), mappedInput(mappedInput) {}
|
||||||
virtual ~Activity() = default;
|
virtual ~Activity() = default;
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ RenderLock::RenderLock() {
|
|||||||
isLocked = true;
|
isLocked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderLock::RenderLock(Activity& /* unused */) {
|
RenderLock::RenderLock([[maybe_unused]] Activity&) {
|
||||||
xSemaphoreTake(activityManager.renderingMutex, portMAX_DELAY);
|
xSemaphoreTake(activityManager.renderingMutex, portMAX_DELAY);
|
||||||
isLocked = true;
|
isLocked = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@
|
|||||||
|
|
||||||
#include "GfxRenderer.h"
|
#include "GfxRenderer.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "RenderLock.h"
|
|
||||||
|
|
||||||
class Activity; // forward declaration
|
class Activity; // forward declaration
|
||||||
class RenderLock; // forward declaration
|
class RenderLock; // forward declaration
|
||||||
@@ -31,6 +30,8 @@ class RenderLock; // forward declaration
|
|||||||
* - onActivityResult is implemented via a callback instead of a separate method, for simplicity
|
* - onActivityResult is implemented via a callback instead of a separate method, for simplicity
|
||||||
*/
|
*/
|
||||||
class ActivityManager {
|
class ActivityManager {
|
||||||
|
friend class RenderLock;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GfxRenderer& renderer;
|
GfxRenderer& renderer;
|
||||||
MappedInputManager& mappedInput;
|
MappedInputManager& mappedInput;
|
||||||
@@ -49,6 +50,10 @@ class ActivityManager {
|
|||||||
static void renderTaskTrampoline(void* param);
|
static void renderTaskTrampoline(void* param);
|
||||||
[[noreturn]] virtual void renderTaskLoop();
|
[[noreturn]] virtual void renderTaskLoop();
|
||||||
|
|
||||||
|
// Mutex to protect rendering operations from race conditions
|
||||||
|
// Must only be used via RenderLock
|
||||||
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
|
|
||||||
// Whether to trigger a render after the current loop()
|
// Whether to trigger a render after the current loop()
|
||||||
// This variable must only be set by the main loop, to avoid race conditions
|
// This variable must only be set by the main loop, to avoid race conditions
|
||||||
bool requestedUpdate = false;
|
bool requestedUpdate = false;
|
||||||
@@ -61,10 +66,6 @@ class ActivityManager {
|
|||||||
}
|
}
|
||||||
~ActivityManager() { assert(false); /* should never be called */ };
|
~ActivityManager() { assert(false); /* should never be called */ };
|
||||||
|
|
||||||
// Mutex to protect rendering operations from race conditions
|
|
||||||
// Must only be used via RenderLock
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
|
|||||||
@@ -383,5 +383,6 @@ void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
|||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
state = BrowserState::ERROR;
|
state = BrowserState::ERROR;
|
||||||
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
||||||
|
requestUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ void CalibreConnectActivity::onExit() {
|
|||||||
|
|
||||||
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ void CalibreConnectActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (exitRequested) {
|
if (exitRequested) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -320,6 +320,7 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
std::make_unique<EpubReaderChapterSelectionActivity>(renderer, mappedInput, epub, path, spineIdx),
|
std::make_unique<EpubReaderChapterSelectionActivity>(renderer, mappedInput, epub, path, spineIdx),
|
||||||
[this](const ActivityResult& result) {
|
[this](const ActivityResult& result) {
|
||||||
if (!result.isCancelled && currentSpineIndex != std::get<ChapterResult>(result.data).spineIndex) {
|
if (!result.isCancelled && currentSpineIndex != std::get<ChapterResult>(result.data).spineIndex) {
|
||||||
|
RenderLock lock(*this);
|
||||||
currentSpineIndex = std::get<ChapterResult>(result.data).spineIndex;
|
currentSpineIndex = std::get<ChapterResult>(result.data).spineIndex;
|
||||||
nextPageNumber = 0;
|
nextPageNumber = 0;
|
||||||
section.reset();
|
section.reset();
|
||||||
@@ -421,6 +422,7 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
if (!result.isCancelled) {
|
if (!result.isCancelled) {
|
||||||
const auto& sync = std::get<SyncResult>(result.data);
|
const auto& sync = std::get<SyncResult>(result.data);
|
||||||
if (currentSpineIndex != sync.spineIndex || (section && section->currentPage != sync.page)) {
|
if (currentSpineIndex != sync.spineIndex || (section && section->currentPage != sync.page)) {
|
||||||
|
RenderLock lock(*this);
|
||||||
currentSpineIndex = sync.spineIndex;
|
currentSpineIndex = sync.spineIndex;
|
||||||
nextPageNumber = sync.page;
|
nextPageNumber = sync.page;
|
||||||
section.reset();
|
section.reset();
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ void CalibreSettingsActivity::onExit() { Activity::onExit(); }
|
|||||||
|
|
||||||
void CalibreSettingsActivity::loop() {
|
void CalibreSettingsActivity::loop() {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ void KOReaderSettingsActivity::onExit() { Activity::onExit(); }
|
|||||||
|
|
||||||
void KOReaderSettingsActivity::loop() {
|
void KOReaderSettingsActivity::loop() {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_ERR("OTA", "WiFi connection failed, exiting");
|
LOG_ERR("OTA", "WiFi connection failed, exiting");
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +172,7 @@ void OtaUpdateActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -180,14 +180,14 @@ void OtaUpdateActivity::loop() {
|
|||||||
|
|
||||||
if (state == FAILED) {
|
if (state == FAILED) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state == NO_UPDATE) {
|
if (state == NO_UPDATE) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
activityManager.popActivity();
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user