|
3 | 3 | #include <libsystem.hpp>
|
4 | 4 | #include <standby.hpp>
|
5 | 5 |
|
6 |
| -// namespace app { |
7 |
| -// std::vector<App> appList; |
8 |
| -// |
9 |
| -// bool request; |
10 |
| -// AppRequest requestingApp; |
11 |
| -// |
12 |
| -// void init() { |
13 |
| -// std::vector<std::string> dirs = storage::Path(APP_DIR).listdir(); |
14 |
| -// |
15 |
| -// storage::FileStream stream((storage::Path(PERMS_DIR) / "auth.list").str(), storage::READ); |
16 |
| -// const std::string allowedFiles = stream.read(); |
17 |
| -// stream.close(); |
18 |
| -// |
19 |
| -// for (const auto& dir: dirs) { |
20 |
| -// std::cout << (storage::Path(APP_DIR) / dir).str() << std::endl; |
21 |
| -// if (allowedFiles.find((storage::Path(APP_DIR) / dir).str()) != std::string::npos) { |
22 |
| -// appList.push_back({ |
23 |
| -// dir, storage::Path(APP_DIR) / dir / "app.lua", storage::Path(PERMS_DIR) / (dir + ".json"), true |
24 |
| -// }); |
25 |
| -// } else { |
26 |
| -// appList.push_back({ |
27 |
| -// dir, storage::Path(APP_DIR) / dir / "app.lua", storage::Path(APP_DIR) / dir / "manifest.json", false |
28 |
| -// }); |
29 |
| -// } |
30 |
| -// } |
31 |
| -// } |
32 |
| -// |
33 |
| -// App getApp(const std::string& appName) { |
34 |
| -// for (auto &app: appList) { |
35 |
| -// if (app.name == appName) { |
36 |
| -// return app; |
37 |
| -// } |
38 |
| -// } |
39 |
| -// |
40 |
| -// return {"", storage::Path(), storage::Path(), false}; |
41 |
| -// } |
42 |
| -// |
43 |
| -// bool askPerm(App &app) { |
44 |
| -// gui::elements::Window win; |
45 |
| -// auto *label = new Label(0, 0, 320, 400); |
46 |
| -// |
47 |
| -// storage::FileStream stream(app.manifest.str(), storage::READ); |
48 |
| -// std::string data = stream.read(); |
49 |
| -// stream.close(); |
50 |
| -// |
51 |
| -// label->setText(data); |
52 |
| -// win.addChild(label); |
53 |
| -// |
54 |
| -// auto *btn = new Button(35, 420, 250, 38); |
55 |
| -// btn->setText("Accepter"); |
56 |
| -// win.addChild(btn); |
57 |
| -// |
58 |
| -// // TODO: Add "Cancel" button |
59 |
| -// |
60 |
| -// while (true) { |
61 |
| -// win.updateAll(); |
62 |
| -// |
63 |
| -// if (btn->isTouched()) { |
64 |
| -// storage::FileStream streamP((storage::Path(PERMS_DIR) / "auth.list").str(), storage::APPEND); |
65 |
| -// streamP.write(app.path.str() + "\n"); |
66 |
| -// streamP.close(); |
67 |
| -// |
68 |
| -// app.manifest = storage::Path(PERMS_DIR) / (app.name + ".json"); |
69 |
| -// app.auth = true; |
70 |
| -// |
71 |
| -// storage::FileStream newPermCopy(app.manifest.str(), storage::WRITE); |
72 |
| -// newPermCopy.write(data); |
73 |
| -// newPermCopy.close(); |
74 |
| -// |
75 |
| -// return true; |
76 |
| -// } |
77 |
| -// } |
78 |
| -// } |
79 |
| -// |
80 |
| -// void runApp(const storage::Path& path) { |
81 |
| -// for (auto &app: appList) { |
82 |
| -// if (app.path.str() == path.str()) { |
83 |
| -// if (app.auth) { |
84 |
| -// std::cout << "Succes: running app" << std::endl; |
85 |
| -// LuaFile luaApp(path, app.manifest); |
86 |
| -// luaApp.run(); |
87 |
| -// } else { |
88 |
| -// std::cout << "Asking for permissions" << std::endl; |
89 |
| -// |
90 |
| -// if (askPerm(app)) { |
91 |
| -// LuaFile luaApp(path, app.manifest); |
92 |
| -// luaApp.run(); |
93 |
| -// } |
94 |
| -// } |
95 |
| -// |
96 |
| -// return; |
97 |
| -// } |
98 |
| -// } |
99 |
| -// |
100 |
| -// std::cout << "Error: no such app" << std::endl; |
101 |
| -// } |
102 |
| -// |
103 |
| -// void runApp(const AppRequest& app) { |
104 |
| -// LuaFile luaApp(app.app.path, app.app.manifest); |
105 |
| -// luaApp.run(app.parameters); |
106 |
| -// } |
107 |
| -// }; |
108 |
| - |
109 | 6 | namespace AppManager {
|
110 | 7 | App::App(const std::string& name, const storage::Path& path, const storage::Path& manifest, const bool auth)
|
111 | 8 | : name(name), fullName(name), path(path), manifest(manifest),
|
@@ -377,69 +274,48 @@ namespace AppManager {
|
377 | 274 | }
|
378 | 275 |
|
379 | 276 | void loop() {
|
380 |
| - threadsync.lock(); |
381 |
| - // Implementation for the main loop |
382 |
| - if (!appStack.empty() && hardware::getHomeButton()) |
383 |
| - // if the home button is pressed, remove the app from the stack, and kill it if it's not running in the background |
384 |
| - { |
385 |
| - while (hardware::getHomeButton()) {} |
386 |
| - |
387 |
| - if (!appStack.empty()) { |
388 |
| - if (appStack.back()->background == false) { |
389 |
| - const auto app = appStack.back(); |
390 |
| - int count = 0; // TODO: Use ? |
| 277 | + updateForeground(); |
| 278 | + updateBackground(); |
| 279 | + } |
391 | 280 |
|
392 |
| - for (const auto& it : appStack) { |
393 |
| - if (it == app) { |
394 |
| - count++; |
395 |
| - } |
396 |
| - } |
| 281 | + void updateForeground() { |
| 282 | + threadsync.lock(); |
397 | 283 |
|
| 284 | + // Run tick on every app |
| 285 | + for (const auto& app: appList) { |
| 286 | + if(app->background == false) // app is not in background |
| 287 | + { |
| 288 | + if (app->isRunning()) { // app is running |
| 289 | + app->luaInstance->loop(); |
| 290 | + } 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 |
| 291 | + { |
398 | 292 | app->kill();
|
399 |
| - } else { |
400 |
| - std::cerr << "Error: app is in background" << std::endl; |
401 | 293 | }
|
402 |
| - |
403 |
| - appStack.pop_back(); |
404 |
| - } |
405 |
| - } |
406 |
| - |
407 |
| - for (const auto& app: appList) { |
408 |
| - if (app->isRunning()) { |
409 |
| - app->luaInstance->loop(); |
410 |
| - } else if (app->luaInstance != nullptr) { |
411 |
| - app->kill(); |
412 | 294 | }
|
413 | 295 | }
|
414 | 296 |
|
| 297 | + // Update foreground app GUI |
415 | 298 | if (!appStack.empty()) {
|
416 |
| - appStack.back()->luaInstance->lua_gui.update(); |
| 299 | + const App* app = appStack.back(); |
| 300 | + |
| 301 | + if (app->luaInstance != nullptr) { |
| 302 | + app->luaInstance->lua_gui.update(); |
| 303 | + } |
417 | 304 | }
|
418 | 305 |
|
419 | 306 | threadsync.unlock();
|
420 |
| - |
421 |
| - StandbyMode::wait(); |
422 | 307 | }
|
423 | 308 |
|
424 |
| - void update() { |
| 309 | + void updateBackground() { |
425 | 310 | threadsync.lock();
|
426 | 311 |
|
427 | 312 | // Run tick on every app
|
428 | 313 | for (const auto& app: appList) {
|
429 |
| - if (app->isRunning()) { |
430 |
| - app->luaInstance->loop(); |
431 |
| - } else if (app->luaInstance != nullptr) { |
432 |
| - app->kill(); |
433 |
| - } |
434 |
| - } |
435 |
| - |
436 |
| - // Update foreground app GUI |
437 |
| - // TODO : What happens if a background app is on top of a foreground app on the stack ? |
438 |
| - if (!appStack.empty()) { |
439 |
| - const App* app = appStack.back(); |
440 |
| - |
441 |
| - if (app->luaInstance != nullptr) { |
442 |
| - app->luaInstance->lua_gui.update(); |
| 314 | + if(app->background == true) // app is in background |
| 315 | + { |
| 316 | + if (app->isRunning()) { // app is running |
| 317 | + app->luaInstance->loop(); |
| 318 | + } |
443 | 319 | }
|
444 | 320 | }
|
445 | 321 |
|
|
0 commit comments