From 6413f25b8d3c274f1238090268c7242bc724586a Mon Sep 17 00:00:00 2001 From: Lasse Luttermann Date: Tue, 14 May 2024 10:13:22 +0200 Subject: [PATCH 1/3] Add config option to select UPower device based on device model. --- include/modules/upower.hpp | 1 + man/waybar-upower.5.scd | 6 ++++++ src/modules/upower.cpp | 23 +++++++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/modules/upower.hpp b/include/modules/upower.hpp index d3499232..60a276db 100644 --- a/include/modules/upower.hpp +++ b/include/modules/upower.hpp @@ -45,6 +45,7 @@ class UPower final : public AIconLabel { // Technical variables std::string nativePath_; + std::string model_; std::string lastStatus_; Glib::ustring label_markup_; std::mutex mutex_; diff --git a/man/waybar-upower.5.scd b/man/waybar-upower.5.scd index 5e2a8eb8..2ae5f17e 100644 --- a/man/waybar-upower.5.scd +++ b/man/waybar-upower.5.scd @@ -17,6 +17,12 @@ compatible devices in the tooltip. The battery to monitor. Refer to the https://upower.freedesktop.org/docs/UpDevice.html#UpDevice--native-path ++ Can be obtained using `upower --dump` +*model*: ++ + typeof: string ++ + default: ++ + The battery to monitor, based on the model. (this option is ignored if *native-path* is given). ++ + Can be obtained using `upower --dump` + *icon-size*: ++ typeof: integer ++ default: 20 ++ diff --git a/src/modules/upower.cpp b/src/modules/upower.cpp index fbbd6c4d..b13dcc44 100644 --- a/src/modules/upower.cpp +++ b/src/modules/upower.cpp @@ -29,6 +29,8 @@ UPower::UPower(const std::string &id, const Json::Value &config) if (!showIcon_) box_.remove(image_); // Device user wants if (config_["native-path"].isString()) nativePath_ = config_["native-path"].asString(); + // Device model user wants + if (config_["model"].isString()) model_ = config_["model"].asString(); // Hide If Empty if (config_["hide-if-empty"].isBool()) hideIfEmpty_ = config_["hide-if-empty"].asBool(); @@ -356,13 +358,13 @@ void UPower::resetDevices() { void UPower::setDisplayDevice() { std::lock_guard guard{mutex_}; - if (nativePath_.empty()) { + if (nativePath_.empty() && model_.empty()) { // Unref current upDevice if (upDevice_.upDevice != NULL) g_object_unref(upDevice_.upDevice); upDevice_.upDevice = up_client_get_display_device(upClient_); getUpDeviceInfo(upDevice_); - } else { + } else if (!nativePath_.empty()) { g_ptr_array_foreach( up_client_get_devices2(upClient_), [](gpointer data, gpointer user_data) { @@ -379,6 +381,23 @@ void UPower::setDisplayDevice() { } }, this); + } else { // if `nativePath_` is empty, but `model_` is not. + g_ptr_array_foreach( + up_client_get_devices2(upClient_), + [](gpointer data, gpointer user_data) { + upDevice_output upDevice; + auto thisPtr {static_cast(user_data)}; + upDevice.upDevice = static_cast(data); + thisPtr->getUpDeviceInfo(upDevice); + if (upDevice.model == nullptr) return; + if (0 == std::strcmp(upDevice.model, thisPtr->model_.c_str())) { + // Unref current upDevice + if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); + // Reassign new upDevice + thisPtr->upDevice_ = upDevice; + } + }, + this); } if (upDevice_.upDevice != NULL) From 28ef5b7db26c3d17347fde18e05c49ea3fd3b54c Mon Sep 17 00:00:00 2001 From: Lasse Luttermann Date: Tue, 14 May 2024 10:21:24 +0200 Subject: [PATCH 2/3] Fix formatting --- src/modules/upower.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/upower.cpp b/src/modules/upower.cpp index b13dcc44..6a3b9091 100644 --- a/src/modules/upower.cpp +++ b/src/modules/upower.cpp @@ -381,12 +381,12 @@ void UPower::setDisplayDevice() { } }, this); - } else { // if `nativePath_` is empty, but `model_` is not. + } else { // if `nativePath_` is empty, but `model_` is not. g_ptr_array_foreach( up_client_get_devices2(upClient_), [](gpointer data, gpointer user_data) { upDevice_output upDevice; - auto thisPtr {static_cast(user_data)}; + auto thisPtr{static_cast(user_data)}; upDevice.upDevice = static_cast(data); thisPtr->getUpDeviceInfo(upDevice); if (upDevice.model == nullptr) return; From d2a719d67c5427dffc3e431c41b96b60399576e6 Mon Sep 17 00:00:00 2001 From: Lasse Luttermann Date: Thu, 16 May 2024 12:37:53 +0200 Subject: [PATCH 3/3] Redo to minimize code duplication. --- src/modules/upower.cpp | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/modules/upower.cpp b/src/modules/upower.cpp index 6a3b9091..552495f8 100644 --- a/src/modules/upower.cpp +++ b/src/modules/upower.cpp @@ -364,7 +364,7 @@ void UPower::setDisplayDevice() { upDevice_.upDevice = up_client_get_display_device(upClient_); getUpDeviceInfo(upDevice_); - } else if (!nativePath_.empty()) { + } else { g_ptr_array_foreach( up_client_get_devices2(upClient_), [](gpointer data, gpointer user_data) { @@ -372,30 +372,22 @@ void UPower::setDisplayDevice() { auto thisPtr{static_cast(user_data)}; upDevice.upDevice = static_cast(data); thisPtr->getUpDeviceInfo(upDevice); - if (upDevice.nativePath == nullptr) return; - if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) { - // Unref current upDevice - if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); - // Reassign new upDevice - thisPtr->upDevice_ = upDevice; - } - }, - this); - } else { // if `nativePath_` is empty, but `model_` is not. - g_ptr_array_foreach( - up_client_get_devices2(upClient_), - [](gpointer data, gpointer user_data) { - upDevice_output upDevice; - auto thisPtr{static_cast(user_data)}; - upDevice.upDevice = static_cast(data); - thisPtr->getUpDeviceInfo(upDevice); - if (upDevice.model == nullptr) return; - if (0 == std::strcmp(upDevice.model, thisPtr->model_.c_str())) { - // Unref current upDevice - if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); - // Reassign new upDevice - thisPtr->upDevice_ = upDevice; + upDevice_output displayDevice{NULL}; + if (!thisPtr->nativePath_.empty()) { + if (upDevice.nativePath == nullptr) return; + if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) { + displayDevice = upDevice; + } + } else { + if (upDevice.model == nullptr) return; + if (0 == std::strcmp(upDevice.model, thisPtr->model_.c_str())) { + displayDevice = upDevice; + } } + // Unref current upDevice + if (displayDevice.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); + // Reassign new upDevice + thisPtr->upDevice_ = displayDevice; }, this); }