Merge pull request #3267 from luttermann/upower_model

Add config option to select UPower device based on device model.
This commit is contained in:
Alexis Rouillard 2024-07-01 09:22:59 +02:00 committed by GitHub
commit 9997155617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 7 deletions

View File

@ -45,6 +45,7 @@ class UPower final : public AIconLabel {
// Technical variables // Technical variables
std::string nativePath_; std::string nativePath_;
std::string model_;
std::string lastStatus_; std::string lastStatus_;
Glib::ustring label_markup_; Glib::ustring label_markup_;
std::mutex mutex_; std::mutex mutex_;

View File

@ -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 ++ The battery to monitor. Refer to the https://upower.freedesktop.org/docs/UpDevice.html#UpDevice--native-path ++
Can be obtained using `upower --dump` 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*: ++ *icon-size*: ++
typeof: integer ++ typeof: integer ++
default: 20 ++ default: 20 ++

View File

@ -29,6 +29,8 @@ UPower::UPower(const std::string &id, const Json::Value &config)
if (!showIcon_) box_.remove(image_); if (!showIcon_) box_.remove(image_);
// Device user wants // Device user wants
if (config_["native-path"].isString()) nativePath_ = config_["native-path"].asString(); 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 // Hide If Empty
if (config_["hide-if-empty"].isBool()) hideIfEmpty_ = config_["hide-if-empty"].asBool(); if (config_["hide-if-empty"].isBool()) hideIfEmpty_ = config_["hide-if-empty"].asBool();
@ -356,7 +358,7 @@ void UPower::resetDevices() {
void UPower::setDisplayDevice() { void UPower::setDisplayDevice() {
std::lock_guard<std::mutex> guard{mutex_}; std::lock_guard<std::mutex> guard{mutex_};
if (nativePath_.empty()) { if (nativePath_.empty() && model_.empty()) {
// Unref current upDevice // Unref current upDevice
if (upDevice_.upDevice != NULL) g_object_unref(upDevice_.upDevice); if (upDevice_.upDevice != NULL) g_object_unref(upDevice_.upDevice);
@ -370,13 +372,22 @@ void UPower::setDisplayDevice() {
auto thisPtr{static_cast<UPower *>(user_data)}; auto thisPtr{static_cast<UPower *>(user_data)};
upDevice.upDevice = static_cast<UpDevice *>(data); upDevice.upDevice = static_cast<UpDevice *>(data);
thisPtr->getUpDeviceInfo(upDevice); thisPtr->getUpDeviceInfo(upDevice);
if (upDevice.nativePath == nullptr) return; upDevice_output displayDevice{NULL};
if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) { if (!thisPtr->nativePath_.empty()) {
// Unref current upDevice if (upDevice.nativePath == nullptr) return;
if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) {
// Reassign new upDevice displayDevice = upDevice;
thisPtr->upDevice_ = 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); this);
} }