Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
* Crashed when Lua render fails
* No initial value from PowerSource upon failure, causing Lua
  to fail when rendering power source
  • Loading branch information
2hdddg committed Jan 16, 2024
1 parent 01c8bcd commit 8c5a257
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ void FlexContainer::Draw(cairo_t* cr, int x, int y) const {
void Widget::Compute(const WidgetConfig& config, const std::string& outputName, cairo_t* cr) {
auto item = config.render(outputName);
if (!item) {
spdlog::error("Bad return from widget");
spdlog::error("Bad render return from widget");
m_paddingX = 0;
m_paddingY = 0;
computed.cx = 0;
computed.cy = 0;
return;
}
item->Compute(cr);
Expand All @@ -131,6 +135,10 @@ void Widget::Compute(const WidgetConfig& config, const std::string& outputName,
}

void Widget::Draw(cairo_t* cr, int x, int y) const {
if (!m_renderable) {
// Lua render failed previously
return;
}
cairo_save(cr);
m_renderable->Draw(cr, x + m_paddingX, y + m_paddingY);
cairo_restore(cr);
Expand Down
3 changes: 3 additions & 0 deletions src/PowerSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ std::shared_ptr<PowerSource> PowerSource::Create(std::string_view name, MainLoop
}

bool PowerSource::Initialize() {
// Publish initial value to make sure that something is published
m_scriptContext->Publish(m_name,
PowerState{.IsPluggedIn = false, .IsCharging = false, .Capacity = 0});
// TODO: Probe that these exists
m_ac = "/sys/class/power_supply/AC/online";
// TODO: Always BAT0?
Expand Down
9 changes: 7 additions & 2 deletions src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,19 @@ static std::set<std::string> ParseSources(const sol::table& widgetTable) {
static void ParseWidgetConfig(const sol::table& table, std::vector<WidgetConfig>& widgets) {
WidgetConfig widget;
widget.sources = ParseSources(table);
sol::optional<sol::function> maybeRenderFunction = table["on_render"];
sol::optional<sol::protected_function> maybeRenderFunction = table["on_render"];
if (!maybeRenderFunction) {
// TODO: Log
return;
}
auto renderFunction = *maybeRenderFunction;
widget.render = [renderFunction](auto outputName) {
return FromObject(renderFunction(outputName));
sol::optional<sol::object> result = renderFunction(outputName);
if (!result) {
spdlog::error("Bad return from render function");
return std::unique_ptr<Renderable>(nullptr);
}
return FromObject(*result);
};
sol::optional<sol::function> maybeClickFunction = table["on_click"];
if (maybeClickFunction) {
Expand Down

0 comments on commit 8c5a257

Please sign in to comment.