Merge pull request #3248 from LukashonakV/ISSUE3223_upower

This commit is contained in:
Alexis Rouillard 2024-05-10 06:50:40 +02:00 committed by GitHub
commit cb2d54a237
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -49,6 +49,7 @@ class UPower final : public AIconLabel {
Glib::ustring label_markup_; Glib::ustring label_markup_;
std::mutex mutex_; std::mutex mutex_;
Glib::RefPtr<Gtk::IconTheme> gtkTheme_; Glib::RefPtr<Gtk::IconTheme> gtkTheme_;
bool sleeping_;
// Technical functions // Technical functions
void addDevice(UpDevice *); void addDevice(UpDevice *);

View File

@ -7,7 +7,7 @@
namespace waybar::modules { namespace waybar::modules {
UPower::UPower(const std::string &id, const Json::Value &config) UPower::UPower(const std::string &id, const Json::Value &config)
: AIconLabel(config, "upower", id, "{percentage}", 0, true, true, true) { : AIconLabel(config, "upower", id, "{percentage}", 0, true, true, true), sleeping_{false} {
box_.set_name(name_); box_.set_name(name_);
box_.set_spacing(0); box_.set_spacing(0);
box_.set_has_tooltip(AModule::tooltipEnabled()); box_.set_has_tooltip(AModule::tooltipEnabled());
@ -185,7 +185,7 @@ days: \"{3}\", strRet: \"{4}\"",
auto UPower::update() -> void { auto UPower::update() -> void {
std::lock_guard<std::mutex> guard{mutex_}; std::lock_guard<std::mutex> guard{mutex_};
// Don't update widget if the UPower service isn't running // Don't update widget if the UPower service isn't running
if (!upRunning_) { if (!upRunning_ || sleeping_) {
if (hideIfEmpty_) box_.hide(); if (hideIfEmpty_) box_.hide();
return; return;
} }
@ -262,9 +262,11 @@ void UPower::prepareForSleep_cb(const Glib::RefPtr<Gio::DBus::Connection> &conne
if (!sleeping.get()) { if (!sleeping.get()) {
resetDevices(); resetDevices();
setDisplayDevice(); setDisplayDevice();
sleeping_ = false;
// Update the widget // Update the widget
dp.emit(); dp.emit();
} } else
sleeping_ = true;
} }
} }
@ -355,6 +357,9 @@ void UPower::setDisplayDevice() {
std::lock_guard<std::mutex> guard{mutex_}; std::lock_guard<std::mutex> guard{mutex_};
if (nativePath_.empty()) { if (nativePath_.empty()) {
// Unref current upDevice
if (upDevice_.upDevice != NULL) g_object_unref(upDevice_.upDevice);
upDevice_.upDevice = up_client_get_display_device(upClient_); upDevice_.upDevice = up_client_get_display_device(upClient_);
getUpDeviceInfo(upDevice_); getUpDeviceInfo(upDevice_);
} else { } else {
@ -367,7 +372,7 @@ void UPower::setDisplayDevice() {
thisPtr->getUpDeviceInfo(upDevice); thisPtr->getUpDeviceInfo(upDevice);
if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) { if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) {
// Unref current upDevice // Unref current upDevice
if (thisPtr->upDevice_.upDevice) g_object_unref(thisPtr->upDevice_.upDevice); if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice);
// Reassign new upDevice // Reassign new upDevice
thisPtr->upDevice_ = upDevice; thisPtr->upDevice_ = upDevice;
} }
@ -375,12 +380,12 @@ void UPower::setDisplayDevice() {
this); this);
} }
if (upDevice_.upDevice) if (upDevice_.upDevice != NULL)
g_signal_connect(upDevice_.upDevice, "notify", G_CALLBACK(deviceNotify_cb), this); g_signal_connect(upDevice_.upDevice, "notify", G_CALLBACK(deviceNotify_cb), this);
} }
void UPower::getUpDeviceInfo(upDevice_output &upDevice_) { void UPower::getUpDeviceInfo(upDevice_output &upDevice_) {
if (upDevice_.upDevice && G_IS_OBJECT(upDevice_.upDevice)) { if (upDevice_.upDevice != NULL && G_IS_OBJECT(upDevice_.upDevice)) {
g_object_get(upDevice_.upDevice, "kind", &upDevice_.kind, "state", &upDevice_.state, g_object_get(upDevice_.upDevice, "kind", &upDevice_.kind, "state", &upDevice_.state,
"percentage", &upDevice_.percentage, "icon-name", &upDevice_.icon_name, "percentage", &upDevice_.percentage, "icon-name", &upDevice_.icon_name,
"time-to-empty", &upDevice_.time_empty, "time-to-full", &upDevice_.time_full, "time-to-empty", &upDevice_.time_empty, "time-to-full", &upDevice_.time_full,
@ -398,7 +403,7 @@ native_path: \"{7}\". model: \"{8}\"",
const Glib::ustring UPower::getText(const upDevice_output &upDevice_, const std::string &format) { const Glib::ustring UPower::getText(const upDevice_output &upDevice_, const std::string &format) {
Glib::ustring ret{""}; Glib::ustring ret{""};
if (upDevice_.upDevice) { if (upDevice_.upDevice != NULL) {
std::string timeStr{""}; std::string timeStr{""};
switch (upDevice_.state) { switch (upDevice_.state) {
case UP_DEVICE_STATE_CHARGING: case UP_DEVICE_STATE_CHARGING: