Skip to content

Commit

Permalink
Fix: mise a jour des apps premierplan/arrièreplan
Browse files Browse the repository at this point in the history
  • Loading branch information
paxo-rch committed Oct 28, 2024
1 parent eb4624d commit 4bf55d7
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 46 deletions.
56 changes: 32 additions & 24 deletions lib/applications/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace AppManager {
: name(name), fullName(name), path(path), manifest(manifest),
auth(auth),
luaInstance(nullptr), app_state(NOT_RUNNING), background(false) {
std::cout << "App: \"" << name << "\" \"" << fullName << "\"" << std::endl;
}

void App::run(const bool background, const std::vector<std::string>& parameters) {
void App::run(const std::vector<std::string>& parameters) {
if (!auth) {
throw libsystem::exceptions::RuntimeError("App is not authorized to run");
}
Expand Down Expand Up @@ -43,7 +42,7 @@ namespace AppManager {
}

bool App::isRunning() const {
return app_state == RUNNING || app_state == RUNNING_BACKGROUND;
return this->luaInstance != nullptr;
}

bool App::isLoaded() const {
Expand All @@ -62,6 +61,14 @@ namespace AppManager {
luaInstance->stop();
luaInstance.reset(); // delete luaInstance

for (auto it = appStack.begin(); it != appStack.end();) {
if (*it == this) {
it = appStack.erase(it);
} else {
++it;
}
}

app_state = NOT_RUNNING;

std::cout << "App killed" << std::endl;
Expand Down Expand Up @@ -174,7 +181,7 @@ namespace AppManager {
void askGui(const LuaFile* lua) {
App* app = lua->app;

if (lua->lua_gui.mainWindow == nullptr) {
/*if (lua->lua_gui.mainWindow == nullptr) {
for (auto it = appStack.begin(); it != appStack.end(); ++it) {
if (*it == app) {
app->app_state = App::AppState::NOT_RUNNING;
Expand All @@ -184,7 +191,7 @@ namespace AppManager {
}
return;
}
}*/

// if (appStack.empty() || appStack.back() != app) {
// appStack.push_back(app);
Expand All @@ -198,11 +205,11 @@ namespace AppManager {
std::string allowedFiles = stream.read();
stream.close();

libsystem::log("auth.list : " + allowedFiles);
//libsystem::log("auth.list : " + allowedFiles);

for (auto dir: dirs) {
auto appPath = storage::Path(directory) / dir;
libsystem::log("Loading app at \"" + appPath.str() + "\".");
//libsystem::log("Loading app at \"" + appPath.str() + "\".");

auto manifestPath = storage::Path(directory) / dir / "manifest.json";

Expand All @@ -211,7 +218,7 @@ namespace AppManager {
manifestStream.close();

if (!nlohmann::json::accept(manifestContent)) {
std::cout << "Error: invalid manifest at \"" << manifestPath.str() << "\"" << std::endl;
std::cerr << "Error: invalid manifest at \"" << manifestPath.str() << "\"" << std::endl;
continue;
}

Expand Down Expand Up @@ -242,7 +249,8 @@ namespace AppManager {
);
}

app->fullName = prefix.size() ? (prefix + "." + dir) : (dir);
app->fullName = prefix.size() ? (prefix + dir) : (dir);
libsystem::log("Loading app \"" + app->fullName + "\".");

if (!dir.empty() && dir[0] == '.') {
app->visible = false;
Expand All @@ -260,12 +268,6 @@ namespace AppManager {
std::cout << "loading app in the background" << std::endl;
}

if (manifest["autorun"].is_boolean()) {
if (manifest["autorun"] && app->background) {
app.get()->run(true);
}
}

if(manifest["subdir"].is_string()) // todo, restrict only for subdirs
{
if((app.get()->path / "../" / manifest["subdir"]).exists())
Expand All @@ -275,8 +277,14 @@ namespace AppManager {
}

// Add app to list
libsystem::log("Loaded app : " + app->toString() + ".");
//libsystem::log("Loaded app : " + app->toString() + ".");
appList.push_back(app);

if (manifest["autorun"].is_boolean()) {
if (manifest["autorun"] && app->background) {
app.get()->run();
}
}
}
}

Expand All @@ -297,22 +305,25 @@ namespace AppManager {
for (const auto& app: appList) {
if(app->background == false) // app is not in background
{
if (app->isRunning()) { // app is running
app->luaInstance->loop();
} else if (std::find(appStack.begin(), appStack.end(), app.get()) != appStack.end()) // if app is no longer in the stack (no gui is running) -> kill it
if (app->isRunning())
{
app->kill();
app->luaInstance->loop();
}
}
}

// Update foreground app GUI
if (!appStack.empty()) {
const App* app = appStack.back();
App* app = appStack.back();

if (app->luaInstance != nullptr) {
app->luaInstance->lua_gui.update();
}

if(app->luaInstance->lua_gui.mainWindow == nullptr) // app has no GUI
{
app->kill();
}
}

threadsync.unlock();
Expand Down Expand Up @@ -348,9 +359,6 @@ namespace AppManager {

// Kill the app
app->kill();

// Remove app from stack
appStack.pop_back();
}

bool isAnyVisibleApp() {
Expand Down
3 changes: 1 addition & 2 deletions lib/applications/src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ namespace AppManager
App(const std::string& name, const storage::Path& path, const storage::Path& manifest, bool auth);

/**
* @param background Run in background
* @param parameters List of parameters to send to the lua run function of the app
*/
void run(bool background, const std::vector<std::string> &parameters = {});
void run(const std::vector<std::string> &parameters = {});

/**
* @brief Wake up the app (if it was sleeping)
Expand Down
6 changes: 4 additions & 2 deletions lib/gui/src/ElementBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ bool gui::ElementBase::update()
if (widgetPressed != nullptr && widgetPressed != this)
return false;

uint16_t resolution = 0;

bool isScreenTouched = graphics::isTouched();
bool isWidgetTouched = isScreenTouched && (getAbsoluteX()-10 < touchX && touchX < getAbsoluteX() + getWidth() +10 &&
getAbsoluteY()-10 < touchY && touchY < getAbsoluteY() + getHeight() +10);
bool isWidgetTouched = isScreenTouched && (getAbsoluteX()-resolution < touchX && touchX < getAbsoluteX() + getWidth() +resolution &&
getAbsoluteY()-resolution < touchY && touchY < getAbsoluteY() + getHeight() +resolution);

bool returnValue = false;

Expand Down
4 changes: 2 additions & 2 deletions lib/lua/src/lua_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ void LuaFile::load()
lua.set_function("launch", sol::overload([&](std::string name, std::vector<std::string> arg)
{
try{
AppManager::get(name)->run(false, arg);
AppManager::get(name)->run(arg);
}
catch(std::runtime_error &e) {
std::cerr << "Erreur: " << e.what() << std::endl;
Expand All @@ -633,7 +633,7 @@ void LuaFile::load()
{
try
{
AppManager::get(name)->run(false, {});
AppManager::get(name)->run({});
}
catch (std::runtime_error &e)
{
Expand Down
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ void mainLoop(void* data) {
try {
const std::shared_ptr<AppManager::App> oobeApp = AppManager::get(".oobe");

oobeApp->run(false);
oobeApp->run();
} catch (std::runtime_error& e) {
std::cerr << "Lua error: " << e.what() << std::endl;
guiManager.showErrorMessage(e.what());
//std::cerr << "Lua error: " << e.what() << std::endl;
//guiManager.showErrorMessage(e.what());
//AppManager::appList[i].kill();
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ void mainLoop(void* data) {

// Launch the app
try {
app->run(app->background);
app->run();
} catch (std::runtime_error& e) {
std::cerr << "Erreur: " << e.what() << std::endl;
// Affichage du msg d'erreur
Expand Down Expand Up @@ -241,7 +241,7 @@ void setup()
// gestion des appels entrants
GSM::ExternalEvents::onIncommingCall = []()
{
eventHandlerApp.setTimeout(new Callback<>([](){AppManager::get(".receivecall")->run(false);}), 0);
eventHandlerApp.setTimeout(new Callback<>([](){AppManager::get(".receivecall")->run();}), 0);
};

// Gestion de la réception d'un message
Expand Down
20 changes: 11 additions & 9 deletions storage/sys_apps/testbc/app.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
function run()
print("run baground testbc app, no displayed: show conter to 10 and run settings app")
print(time)
local counter = 0
-- local counter = 0

local function printCounter()
counter = counter + 1
print("Counter: " .. counter .. " sec")
if(counter == 10) then
launch("settings")
end
end
-- local function printCounter()
-- counter = counter + 1
-- print("Counter: " .. counter .. " sec")
-- if(counter == 10) then
-- launch("settings")
-- end
-- end

time:setInterval(printCounter, 1000)
-- time:setInterval(printCounter, 1000)

launch("testbc.sub1")
end
2 changes: 1 addition & 1 deletion storage/sys_apps/testbc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"files",
"time"
],
"background": true,
"background": false,
"subdir": "sub"
}
2 changes: 1 addition & 1 deletion storage/sys_apps/testbc/sub/.sub1/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"time"
],
"background": true,
"autorun": true
"autorun": false
}

0 comments on commit 4bf55d7

Please sign in to comment.