From 8c5a257a2a148ff07f27107d02463c2c393ac803 Mon Sep 17 00:00:00 2001 From: Peter Wilhelmsson <2hdddg@gmail.com> Date: Tue, 16 Jan 2024 18:25:20 +0100 Subject: [PATCH] Bug fixes * Crashed when Lua render fails * No initial value from PowerSource upon failure, causing Lua to fail when rendering power source --- src/Draw.cpp | 10 +++++++++- src/PowerSource.cpp | 3 +++ src/ScriptContext.cpp | 9 +++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Draw.cpp b/src/Draw.cpp index ed36822..8cabc71 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -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); @@ -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); diff --git a/src/PowerSource.cpp b/src/PowerSource.cpp index 70ad76c..86f14fe 100644 --- a/src/PowerSource.cpp +++ b/src/PowerSource.cpp @@ -29,6 +29,9 @@ std::shared_ptr 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? diff --git a/src/ScriptContext.cpp b/src/ScriptContext.cpp index 0518a07..3977fee 100644 --- a/src/ScriptContext.cpp +++ b/src/ScriptContext.cpp @@ -111,14 +111,19 @@ static std::set ParseSources(const sol::table& widgetTable) { static void ParseWidgetConfig(const sol::table& table, std::vector& widgets) { WidgetConfig widget; widget.sources = ParseSources(table); - sol::optional maybeRenderFunction = table["on_render"]; + sol::optional maybeRenderFunction = table["on_render"]; if (!maybeRenderFunction) { // TODO: Log return; } auto renderFunction = *maybeRenderFunction; widget.render = [renderFunction](auto outputName) { - return FromObject(renderFunction(outputName)); + sol::optional result = renderFunction(outputName); + if (!result) { + spdlog::error("Bad return from render function"); + return std::unique_ptr(nullptr); + } + return FromObject(*result); }; sol::optional maybeClickFunction = table["on_click"]; if (maybeClickFunction) {