From f3e27a205efd9d4409ecba0f6cd79652ceef2fd2 Mon Sep 17 00:00:00 2001
From: raffael0 <43984260+raffael0@users.noreply.github.com>
Date: Thu, 16 Jun 2022 12:54:38 +0200
Subject: [PATCH] backlight: auto-detect default card (#2728)
* auto-detect a default backlight
* Implemented suggestions
* added changelog
* Implemented suggestions
* Fix changelog formatting
Co-authored-by: patrick96
---
CHANGELOG.md | 1 +
src/modules/backlight.cpp | 23 +++++++++++++++++++++--
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 09d47585..0f3309e4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `internal/fs`: Use `/` as a fallback if no mountpoints are specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2705`](https://github.com/polybar/polybar/pull/2705))
+- `internal/backlight`: Detect backlight if none specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2728`](https://github.com/polybar/polybar/pull/2728))
### Fixed
- Waiting for double click interval on modules that don't have a double click action ([`#2663`](https://github.com/polybar/polybar/issues/2663), [`#2695`](https://github.com/polybar/polybar/pull/2695))
diff --git a/src/modules/backlight.cpp b/src/modules/backlight.cpp
index 59c3fd5a..4e4c47da 100644
--- a/src/modules/backlight.cpp
+++ b/src/modules/backlight.cpp
@@ -27,9 +27,28 @@ namespace modules {
: inotify_module(bar, move(name_)) {
m_router->register_action(EVENT_DEC, [this]() { action_dec(); });
m_router->register_action(EVENT_INC, [this]() { action_inc(); });
+ auto card = m_conf.get(name(), "card", ""s);
+ if (card.empty()) {
+ vector backlight_card_names = file_util::list_files(string_util::replace(PATH_BACKLIGHT, "%card%", ""));
+ backlight_card_names.erase(std::remove_if(backlight_card_names.begin(), backlight_card_names.end(),
+ [&](const string& card) -> bool {
+ auto dir = string_util::replace(PATH_BACKLIGHT, "%card%", card);
+ return !(file_util::is_file(dir + "/actual_brightness") &&
+ file_util::is_file(dir + "/brightness") &&
+ file_util::is_file(dir + "/max_brightness"));
+ }),
+ backlight_card_names.end());
- auto card = m_conf.get(name(), "card");
-
+ if (backlight_card_names.empty()) {
+ throw module_error("no viable default backlight found");
+ }
+ card = backlight_card_names.at(0);
+ if (backlight_card_names.size() > 1) {
+ m_log.warn("%s: multiple backlights found, using %s", name(), card);
+ } else {
+ m_log.info("%s: no backlight specified, using `%s`", name(), card);
+ }
+ }
// Get flag to check if we should add scroll handlers for changing value
m_scroll = m_conf.get(name(), "enable-scroll", m_scroll);