Skip to content

Commit 35e082b

Browse files
Added Lua support for FileConfig, added OOBE app and some new things.
1 parent 1d367b0 commit 35e082b

12 files changed

+217
-20
lines changed

lib/lua/src/lua_file.cpp

+52-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <fstream>
1515
#include <iostream>
16+
#include <lua_system.hpp>
1617

1718

1819
/*
@@ -486,7 +487,7 @@ void LuaFile::load()
486487
"getTextWidth", &LuaLabel::getTextWidth,
487488
"setVerticalAlignment", &LuaLabel::setVerticalAlignment,
488489
"setHorizontalAlignment", &LuaLabel::setHorizontalAlignment,
489-
"setTextColor", &LuaLabel::setTextColor,
490+
"setTextColor", sol::overload(&LuaLabel::setTextColor, &LuaLabel::setTextColorRGB),
490491
sol::base_classes, sol::bases<LuaWidget>());
491492

492493
lua.new_usertype<LuaInput>("LuaInput",
@@ -648,6 +649,43 @@ void LuaFile::load()
648649
lua["gsm"] = luaGSM;
649650
}
650651

652+
/*
653+
* System.
654+
*
655+
* @todo Add permission.
656+
*/
657+
{
658+
// TODO: Move this from this scope to the "global lua" scope.
659+
auto paxo = lua["paxo"].get_or_create<sol::table>(sol::new_table());
660+
661+
auto system = paxo["system"].get_or_create<sol::table>(sol::new_table());
662+
auto systemConfig = system["config"].get_or_create<sol::table>(sol::new_table());
663+
664+
// paxo.system.config.get()
665+
systemConfig.set_function("get", sol::overload(
666+
&paxolua::system::config::getBool,
667+
// &paxolua::system::config::getInt,
668+
&paxolua::system::config::getFloat,
669+
&paxolua::system::config::getString
670+
));
671+
672+
// paxo.system.config.set()
673+
systemConfig.set_function("set", sol::overload(
674+
&paxolua::system::config::setBool,
675+
// &paxolua::system::config::setInt,
676+
&paxolua::system::config::setFloat,
677+
&paxolua::system::config::setString
678+
));
679+
680+
systemConfig.set_function("write", &paxolua::system::config::write);
681+
682+
auto app = paxo["app"].get_or_create<sol::table>(sol::new_table());
683+
684+
app.set_function("quit", [&]() {
685+
m_commandQueue.push(QUIT);
686+
});
687+
}
688+
651689
{ // load events
652690
sol::table luaEvents = lua.create_table();
653691

@@ -729,8 +767,19 @@ void LuaFile::stop(std::vector<std::string> arg)
729767
lua["quit"](arg);
730768
}
731769

732-
void LuaFile::loop()
733-
{
770+
void LuaFile::loop() {
771+
// Process commands
772+
while (!m_commandQueue.empty()) {
773+
switch (m_commandQueue.front()) {
774+
case QUIT:
775+
// Quit lua app OUTSIDE of lua
776+
AppManager::quitApp();
777+
break;
778+
}
779+
780+
m_commandQueue.pop();
781+
}
782+
734783
//lua_gui.update(); // add App Priority To Acces Gui
735784
lua_time.update();
736785
}

lib/lua/src/lua_file.hpp

+8
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 <queue>
1011

1112
#include "lua_gui.hpp"
1213
#include "lua_hardware.hpp"
@@ -93,6 +94,13 @@ class LuaFile {
9394
//LuaNetwork lua_network;
9495

9596
AppManager::App* app; // using raw pointer, because this class will NEVER call the deleter
97+
98+
private:
99+
enum Command {
100+
QUIT
101+
};
102+
103+
std::queue<Command> m_commandQueue;
96104
};
97105

98106
#endif

lib/lua/src/lua_label.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
#include "lua_label.hpp"
22

3+
#include <libsystem.hpp>
4+
35
LuaLabel::LuaLabel(LuaWidget* parent, int x, int y, int width, int height)
46
{
57
widget = new Label(x, y, width, height);
68
init(widget, parent);
79
}
10+
11+
void LuaLabel::setTextColor(const color_t color) const {
12+
libsystem::log("Color: " + std::to_string(color));
13+
14+
widget->setTextColor(color);
15+
}
16+
17+
void LuaLabel::setTextColorRGB(const uint8_t r, const uint8_t g, const uint8_t b) const {
18+
libsystem::log("Color RGB: " + std::to_string(r) + ", " + std::to_string(g) + ", " + std::to_string(b));
19+
20+
widget->setTextColor(graphics::packRGB565(r, g, b));
21+
}
22+

lib/lua/src/lua_label.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class LuaLabel : public LuaWidget
1919
void setVerticalAlignment(Label::Alignement alignment){ widget->setVerticalAlignment(alignment); }
2020
void setHorizontalAlignment(Label::Alignement alignment){ widget->setHorizontalAlignment(alignment); }
2121

22-
void setTextColor(color_t color){ widget->setTextColor(color); }
22+
void setTextColor(color_t color) const;
23+
void setTextColorRGB(uint8_t r, uint8_t g, uint8_t b) const;
2324

2425
Label* widget = nullptr;
2526
};

lib/lua/src/lua_system.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Created by Charles on 11/09/2024.
3+
//
4+
5+
#include "lua_system.hpp"
6+
7+
#include <libsystem.hpp>
8+
9+
bool paxolua::system::config::getBool(const std::string &key) {
10+
libsystem::log("getBool: " + key);
11+
return libsystem::getSystemConfig().get<bool>(key);
12+
}
13+
14+
int paxolua::system::config::getInt(const std::string &key) {
15+
libsystem::log("getInt: " + key);
16+
return libsystem::getSystemConfig().get<int>(key);
17+
}
18+
19+
float paxolua::system::config::getFloat(const std::string &key) {
20+
libsystem::log("getFloat: " + key);
21+
return libsystem::getSystemConfig().get<float>(key);
22+
}
23+
24+
std::string paxolua::system::config::getString(const std::string &key) {
25+
libsystem::log("getString: " + key);
26+
return libsystem::getSystemConfig().get<std::string>(key);
27+
}
28+
29+
void paxolua::system::config::setBool(const std::string &key, const bool value) {
30+
libsystem::log("setBool: " + key + ", " + std::to_string(value));
31+
libsystem::getSystemConfig().set<bool>(key, value);
32+
}
33+
34+
void paxolua::system::config::setInt(const std::string &key, const int value) {
35+
libsystem::log("setInt: " + key + ", " + std::to_string(value));
36+
libsystem::getSystemConfig().set<int>(key, value);
37+
}
38+
39+
void paxolua::system::config::setFloat(const std::string &key, const float value) {
40+
libsystem::log("setFloat: " + key + ", " + std::to_string(value));
41+
libsystem::getSystemConfig().set<float>(key, value);
42+
}
43+
44+
void paxolua::system::config::setString(const std::string &key, const std::string &value) {
45+
libsystem::log("setString: " + key + ", " + value);
46+
libsystem::getSystemConfig().set<std::string>(key, value);
47+
}
48+
49+
void paxolua::system::config::write() {
50+
libsystem::log("write");
51+
libsystem::getSystemConfig().write();
52+
}

lib/lua/src/lua_system.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by Charles on 11/09/2024.
3+
//
4+
5+
#ifndef LUA_SYSTEM_HPP
6+
#define LUA_SYSTEM_HPP
7+
8+
#include <string>
9+
10+
namespace paxolua::system::config {
11+
bool getBool(const std::string &key);
12+
int getInt(const std::string &key);
13+
float getFloat(const std::string &key);
14+
std::string getString(const std::string &key);
15+
16+
void setBool(const std::string &key, bool value);
17+
void setInt(const std::string &key, int value);
18+
void setFloat(const std::string &key, float value);
19+
void setString(const std::string &key, const std::string &value);
20+
21+
// TODO: Make "write()" auto.
22+
void write();
23+
}
24+
25+
26+
#endif //LUA_SYSTEM_HPP

lib/system/FileConfig.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -151,35 +151,24 @@ namespace libsystem {
151151
value = static_cast<int>(readUint32());
152152
break;
153153
case FLOAT: {
154-
log("Found float !");
155-
156154
uint32_t intValue = readUint32();
157-
158-
log("Float int: " + std::to_string(intValue));
159-
160155
float floatValue = *reinterpret_cast<float *>(&intValue);
161156

162-
log("Float float: " + std::to_string(floatValue));
163-
164157
value = floatValue;
165158

166159
break;
167160
}
168161
case DOUBLE: {
169-
log("Found double !");
170-
171162
uint64_t intValue = readUint64();
172-
173-
log("Double int: " + std::to_string(intValue));
174-
175163
double doubleValue = *reinterpret_cast<double *>(&intValue);
176164

177-
log("Double double: " + std::to_string(doubleValue));
178-
179165
value = doubleValue;
180166

181167
break;
182168
}
169+
case BOOL:
170+
value = static_cast<bool>(readUint8());
171+
break;
183172
default:;
184173
}
185174

@@ -461,6 +450,14 @@ namespace libsystem {
461450

462451
break;
463452
}
453+
case BOOL: {
454+
const bool v = std::get<bool>(m_value);
455+
456+
// ReSharper disable once CppRedundantCastExpression
457+
fileStream->write(static_cast<char>(v));
458+
459+
break;
460+
}
464461
default:
465462
fileStream->write(0x00);
466463
}

lib/system/FileConfig.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ namespace libsystem {
4242
std::string,
4343
int,
4444
float,
45-
double
45+
double,
46+
bool
4647
> file_config_types_t;
4748

4849
public:
@@ -107,7 +108,8 @@ namespace libsystem {
107108
STRING,
108109
INT,
109110
FLOAT,
110-
DOUBLE
111+
DOUBLE,
112+
BOOL
111113
};
112114

113115
enum VersionFlag {

src/main.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ void mainLoop(void* data) {
5757

5858
GuiManager& guiManager = GuiManager::getInstance();
5959

60+
// ReSharper disable once CppTooWideScopeInitStatement
61+
const libsystem::FileConfig systemConfig = libsystem::getSystemConfig();
62+
63+
// Check if OOBE app need to be launched
64+
if (!systemConfig.has("oobe") || !systemConfig.get<bool>("oobe")) {
65+
// Launch OOBE app
66+
const std::shared_ptr<AppManager::App> oobeApp = AppManager::get(".oobe");
67+
68+
try {
69+
oobeApp->run(false);
70+
} catch (std::runtime_error& e) {
71+
std::cerr << "Lua error: " << e.what() << std::endl;
72+
guiManager.showErrorMessage(e.what());
73+
//AppManager::appList[i].kill();
74+
}
75+
}
76+
6077
// Main loop
6178
while (true) {
6279
// Update inputs

storage/sys_apps/.oobe/app.lua

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function run(arg)
2+
win = gui:window()
3+
4+
title = gui:label(win, 0, 160, 320, 40)
5+
title:setText("Paxo")
6+
title:setTextColor(58, 186, 153)
7+
title:setHorizontalAlignment(CENTER_ALIGNMENT)
8+
title:setFontSize(42)
9+
10+
title = gui:label(win, 0, 210, 320, 40)
11+
title:setText("Running on PaxOS 9")
12+
title:setTextColor(100, 100, 100)
13+
title:setHorizontalAlignment(CENTER_ALIGNMENT)
14+
title:setFontSize(20)
15+
16+
button = gui:button(win, 100, 380, 120, 40)
17+
button:setIcon("sys_apps/.oobe/resources/images/arrow_forward_ios_64px.png")
18+
button:setText("Welcome")
19+
button:onClick(function ()
20+
paxo.system.config.set("oobe", true)
21+
paxo.system.config.write()
22+
23+
paxo.app.quit()
24+
end)
25+
26+
gui:setWindow(win)
27+
end

storage/sys_apps/.oobe/manifest.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"access": ["files", "files_root", "gsm", "gui", "hardware", "time", "web", "web_paxo"]
3+
}
Loading

0 commit comments

Comments
 (0)