From 5b3402e110eca2bb3f99c6c81fa78106f7a51d29 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 14 May 2019 15:43:57 +0200 Subject: [PATCH] feat(Battery): plugged status --- include/modules/battery.hpp | 8 ++++---- resources/config | 8 +++++--- src/modules/battery.cpp | 29 +++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 9787a8dc..5f2290ee 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -30,10 +30,10 @@ class Battery : public ALabel { private: static inline const fs::path data_dir_ = "/sys/class/power_supply/"; - void getBatteries(); - void worker(); - const std::string getAdapterStatus(uint8_t capacity) const; - const std::tuple getInfos() const; + void getBatteries(); + void worker(); + const std::string getAdapterStatus(uint8_t capacity, uint32_t current_now) const; + const std::tuple getInfos() const; util::SleeperThread thread_; util::SleeperThread thread_timer_; diff --git a/resources/config b/resources/config index d3f5fbae..430535fa 100644 --- a/resources/config +++ b/resources/config @@ -94,6 +94,8 @@ "critical": 15 }, "format": "{capacity}% {icon}", + "format-charging": "{capacity}% ", + "format-plugged": "{capacity}% ", // "format-good": "", // An empty format will hide the module // "format-full": "", "format-icons": ["", "", "", "", ""] @@ -104,14 +106,14 @@ "network": { // "interface": "wlp2s0", // (Optional) To force the use of this interface "format-wifi": "{essid} ({signalStrength}%) ", - "format-ethernet": "{ifname}: {ipaddr}/{cidr} ", + "format-ethernet": "{ifname}: {ipaddr}/{cidr} ", "format-disconnected": "Disconnected ⚠" }, "pulseaudio": { //"scroll-step": 1, "format": "{volume}% {icon}", "format-bluetooth": "{volume}% {icon}", - "format-muted": "", + "format-muted": "", "format-icons": { "headphones": "", "handsfree": "", @@ -119,7 +121,7 @@ "phone": "", "portable": "", "car": "", - "default": ["", ""] + "default": ["", "", ""] }, "on-click": "pavucontrol" }, diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 5f97bb26..c0610c31 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -75,44 +75,55 @@ void waybar::modules::Battery::getBatteries() { } } -const std::tuple waybar::modules::Battery::getInfos() const { +const std::tuple waybar::modules::Battery::getInfos() const { try { uint16_t total = 0; + uint32_t total_current = 0; std::string status = "Unknown"; for (auto const& bat : batteries_) { uint16_t capacity; + uint32_t current_now; std::string _status; std::ifstream(bat / "capacity") >> capacity; std::ifstream(bat / "status") >> _status; + std::ifstream(bat / "current_now") >> current_now; if (_status != "Unknown") { status = _status; } total += capacity; + total_current += current_now; } uint16_t capacity = total / batteries_.size(); - return {capacity, status}; + if (status == "Charging" && total_current != 0) { + status == "Plugged"; + } + return {capacity, total_current, status}; } catch (const std::exception& e) { std::cerr << e.what() << std::endl; - return {0, "Unknown"}; + return {0, 0, "Unknown"}; } } -const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity) const { +const std::string waybar::modules::Battery::getAdapterStatus(uint8_t capacity, + uint32_t current_now) const { if (!adapter_.empty()) { bool online; std::ifstream(adapter_ / "online") >> online; if (capacity == 100) { return "Full"; } - return online ? "Charging" : "Discharging"; + if (online) { + return current_now == 0 ? "Charging" : "Plugged"; + } + return "Discharging"; } return "Unknown"; } auto waybar::modules::Battery::update() -> void { - auto [capacity, status] = getInfos(); + auto [capacity, current_now, status] = getInfos(); if (status == "Unknown") { - status = getAdapterStatus(capacity); + status = getAdapterStatus(capacity, current_now); } if (tooltipEnabled()) { label_.set_tooltip_text(status); @@ -120,7 +131,9 @@ auto waybar::modules::Battery::update() -> void { std::transform(status.begin(), status.end(), status.begin(), ::tolower); auto format = format_; auto state = getState(capacity, true); - label_.get_style_context()->remove_class(old_status_); + if (!old_status_.empty()) { + label_.get_style_context()->remove_class(old_status_); + } label_.get_style_context()->add_class(status); old_status_ = status; if (!state.empty() && config_["format-" + status + "-" + state].isString()) {