From 1af7191740a330c03caf9fc266ba8830bdc0cc99 Mon Sep 17 00:00:00 2001 From: Bao Trinh Date: Thu, 21 Apr 2022 21:48:40 -0500 Subject: [PATCH 1/2] Backlight: avoid crash on getting brightness fails Reading brightness value for backlight device can fail intermittently (particularly when using ddcci-driver-linux). Handle this more gracefully rather than crashing --- src/modules/backlight.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index a93934b..d982ee6 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -214,12 +214,10 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last, strncmp(name, "amdgpu_bl", 9) == 0 ? "brightness" : "actual_brightness"; const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr); - check_nn(actual); - const int actual_int = std::stoi(actual); + const int actual_int = actual == nullptr ? 0 : std::stoi(actual); const char *max = udev_device_get_sysattr_value(dev, "max_brightness"); - check_nn(max); - const int max_int = std::stoi(max); + const int max_int = max == nullptr ? 0 : std::stoi(max); auto found = std::find_if(first, last, [name](const auto &device) { return device.name() == name; }); From 96746142d23da07e985420811f182f2c05ea9ba6 Mon Sep 17 00:00:00 2001 From: Bao Trinh Date: Thu, 21 Apr 2022 22:19:05 -0500 Subject: [PATCH 2/2] Backlight: don't reset value when failing to read Avoids the brightness percentage resetting to 0 on intermittent failures --- src/modules/backlight.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp index d982ee6..4b34824 100644 --- a/src/modules/backlight.cpp +++ b/src/modules/backlight.cpp @@ -214,17 +214,20 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last, strncmp(name, "amdgpu_bl", 9) == 0 ? "brightness" : "actual_brightness"; const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr); - const int actual_int = actual == nullptr ? 0 : std::stoi(actual); - const char *max = udev_device_get_sysattr_value(dev, "max_brightness"); - const int max_int = max == nullptr ? 0 : std::stoi(max); auto found = std::find_if(first, last, [name](const auto &device) { return device.name() == name; }); if (found != last) { - found->set_actual(actual_int); - found->set_max(max_int); + if (actual != nullptr) { + found->set_actual(std::stoi(actual)); + } + if (max != nullptr) { + found->set_max(std::stoi(max)); + } } else { + const int actual_int = actual == nullptr ? 0 : std::stoi(actual); + const int max_int = max == nullptr ? 0 : std::stoi(max); *inserter = BacklightDev{name, actual_int, max_int}; ++inserter; }