Skip to content

Commit 2219312

Browse files
committed
fix crash del from itself, added local event handler in lua apps
1 parent 6bc9980 commit 2219312

File tree

4 files changed

+17
-72
lines changed

4 files changed

+17
-72
lines changed

lib/lua/src/lua_events.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ LuaTimeInterval::LuaTimeInterval(LuaFile* lua, sol::protected_function func, uin
8484
this->lua = lua;
8585
this->func = func;
8686
this->interval = interval;
87-
this->id = eventHandlerApp.setInterval(std::function<void(void)>(std::bind(&LuaTimeInterval::call, this)), interval);
87+
this->id = lua->eventHandler.setInterval(std::function<void(void)>(std::bind(&LuaTimeInterval::call, this)), interval);
8888
}
8989

9090

@@ -93,8 +93,8 @@ uint32_t LuaTimeEvent::addEventListener(LuaFile* lua, sol::protected_function co
9393
this->condition = condition;
9494
this->callback = callback;
9595
// this->interval = interval;
96-
// this->id = eventHandlerApp.setInterval(std::function<void(void)>(std::bind(&LuaTimeInterval::call, this)),);
97-
return 0; //eventHandlerApp.addEventListener( (Function *) condition, (Function *) callback);
96+
// this->id = eventHandler.setInterval(std::function<void(void)>(std::bind(&LuaTimeInterval::call, this)),);
97+
return 0; //eventHandler.addEventListener( (Function *) condition, (Function *) callback);
9898
}
9999

100100

@@ -123,15 +123,15 @@ void LuaTimeInterval::call()
123123

124124
LuaTimeInterval::~LuaTimeInterval()
125125
{
126-
eventHandlerApp.removeInterval(id);
126+
lua->eventHandler.removeInterval(id);
127127
}
128128

129129
LuaTimeTimeout::LuaTimeTimeout(LuaFile* lua, sol::protected_function func, uint32_t timeout)
130130
{
131131
this->lua = lua;
132132
this->func = func;
133133
this->timeout = timeout;
134-
this->id = eventHandlerApp.setTimeout(new Callback<>(std::function<void(void)>(std::bind(&LuaTimeTimeout::call, this))), timeout);
134+
this->id = lua->eventHandler.setTimeout(new Callback<>(std::function<void(void)>(std::bind(&LuaTimeTimeout::call, this))), timeout);
135135
}
136136

137137
int LuaTimeTimeout::getId()
@@ -158,12 +158,12 @@ void LuaTimeTimeout::call()
158158
LuaTimeTimeout::~LuaTimeTimeout()
159159
{
160160
if(!done)
161-
eventHandlerApp.removeTimeout(id);
161+
lua->eventHandler.removeTimeout(id);
162162
}
163163

164164
void LuaTime::update()
165165
{
166-
eventHandlerApp.update();
166+
lua->eventHandler.update();
167167

168168
for (int it = 0; it < timeouts.size(); it++)
169169
{

lib/lua/src/lua_file.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <filestream.hpp>
88
#include <path.hpp>
99
#include <hardware.hpp>
10+
#include <threads.hpp>
1011

1112
#include "lua_gui.hpp"
1213
#include "lua_hardware.hpp"
@@ -86,6 +87,7 @@ class LuaFile {
8687
storage::Path filename;
8788
Window* current_root;
8889

90+
EventHandler eventHandler;
8991
LuaHardware lua_hardware;
9092
LuaGui lua_gui;
9193
LuaStorage lua_storage;

lib/lua/src/lua_gui.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ LuaWindow* LuaGui::window()
141141

142142
void LuaGui::del(LuaWidget* widget)
143143
{
144-
delete widget;
145-
if(mainWindow == widget)
146-
mainWindow = nullptr;
147-
widget = nullptr;
144+
// prevent a widget to remove itself during its execution
145+
lua->eventHandler.setTimeout(new Callback<>(std::function<void(void)>([&]()
146+
{
147+
delete widget;
148+
if(mainWindow == widget)
149+
mainWindow = nullptr;
150+
widget = nullptr;
151+
})), 0); // run it as soon as possible
148152
}
149153

150154
void LuaGui::update()

src/main.cpp

-61
Original file line numberDiff line numberDiff line change
@@ -141,67 +141,6 @@ void mainLoop(void* data) {
141141

142142
StandbyMode::wait();
143143
}
144-
/*
145-
// Main loop
146-
while (true) {
147-
// Update inputs
148-
hardware::input::update();
149-
std::cout << "Update inputs" << std::endl;
150-
151-
// Update running apps
152-
AppManager::update();
153-
154-
// Don't show anything
155-
if (libsystem::getDeviceMode() == libsystem::SLEEP) {
156-
if (getButtonDown(hardware::input::HOME)) {
157-
setDeviceMode(libsystem::NORMAL);
158-
}
159-
160-
continue;
161-
}
162-
163-
if (AppManager::isAnyVisibleApp()) {
164-
if (getButtonDown(hardware::input::HOME)) {
165-
AppManager::quitApp();
166-
}
167-
} else {
168-
// If home button pressed on the launcher
169-
// Put the device in sleep
170-
if (getButtonDown(hardware::input::HOME)) {
171-
// Free the launcher resources
172-
applications::launcher::free();
173-
174-
setDeviceMode(libsystem::SLEEP);
175-
continue;
176-
}
177-
178-
std::cout << "Update launcher" << std::endl;
179-
180-
// Update, show and allocate launcher
181-
applications::launcher::update();
182-
183-
// Icons interactions
184-
if (applications::launcher::iconTouched()) {
185-
const std::shared_ptr<AppManager::App> app = applications::launcher::getApp();
186-
187-
// Free the launcher resources
188-
applications::launcher::free();
189-
190-
// Launch the app
191-
try {
192-
app->run(false);
193-
} catch (std::runtime_error& e) {
194-
std::cerr << "Erreur: " << e.what() << std::endl;
195-
// Affichage du msg d'erreur
196-
guiManager.showErrorMessage(e.what());
197-
// on kill l'application ?!?
198-
//AppManager::appList[i].kill();
199-
}
200-
}
201-
}
202-
203-
AppManager::loop();
204-
}*/
205144
}
206145

207146
void setup()

0 commit comments

Comments
 (0)