feat(Battery): plugged status

This commit is contained in:
Alex 2019-05-14 15:43:57 +02:00
parent d209d350fe
commit 5b3402e110
3 changed files with 30 additions and 15 deletions

View File

@ -30,10 +30,10 @@ class Battery : public ALabel {
private: private:
static inline const fs::path data_dir_ = "/sys/class/power_supply/"; static inline const fs::path data_dir_ = "/sys/class/power_supply/";
void getBatteries(); void getBatteries();
void worker(); void worker();
const std::string getAdapterStatus(uint8_t capacity) const; const std::string getAdapterStatus(uint8_t capacity, uint32_t current_now) const;
const std::tuple<uint8_t, std::string> getInfos() const; const std::tuple<uint8_t, uint32_t, std::string> getInfos() const;
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_timer_; util::SleeperThread thread_timer_;

View File

@ -94,6 +94,8 @@
"critical": 15 "critical": 15
}, },
"format": "{capacity}% {icon}", "format": "{capacity}% {icon}",
"format-charging": "{capacity}% ",
"format-plugged": "{capacity}% ",
// "format-good": "", // An empty format will hide the module // "format-good": "", // An empty format will hide the module
// "format-full": "", // "format-full": "",
"format-icons": ["", "", "", "", ""] "format-icons": ["", "", "", "", ""]
@ -104,14 +106,14 @@
"network": { "network": {
// "interface": "wlp2s0", // (Optional) To force the use of this interface // "interface": "wlp2s0", // (Optional) To force the use of this interface
"format-wifi": "{essid} ({signalStrength}%) ", "format-wifi": "{essid} ({signalStrength}%) ",
"format-ethernet": "{ifname}: {ipaddr}/{cidr} ", "format-ethernet": "{ifname}: {ipaddr}/{cidr} ",
"format-disconnected": "Disconnected ⚠" "format-disconnected": "Disconnected ⚠"
}, },
"pulseaudio": { "pulseaudio": {
//"scroll-step": 1, //"scroll-step": 1,
"format": "{volume}% {icon}", "format": "{volume}% {icon}",
"format-bluetooth": "{volume}% {icon}", "format-bluetooth": "{volume}% {icon}",
"format-muted": "", "format-muted": "",
"format-icons": { "format-icons": {
"headphones": "", "headphones": "",
"handsfree": "", "handsfree": "",
@ -119,7 +121,7 @@
"phone": "", "phone": "",
"portable": "", "portable": "",
"car": "", "car": "",
"default": ["", ""] "default": ["", "", ""]
}, },
"on-click": "pavucontrol" "on-click": "pavucontrol"
}, },

View File

@ -75,44 +75,55 @@ void waybar::modules::Battery::getBatteries() {
} }
} }
const std::tuple<uint8_t, std::string> waybar::modules::Battery::getInfos() const { const std::tuple<uint8_t, uint32_t, std::string> waybar::modules::Battery::getInfos() const {
try { try {
uint16_t total = 0; uint16_t total = 0;
uint32_t total_current = 0;
std::string status = "Unknown"; std::string status = "Unknown";
for (auto const& bat : batteries_) { for (auto const& bat : batteries_) {
uint16_t capacity; uint16_t capacity;
uint32_t current_now;
std::string _status; std::string _status;
std::ifstream(bat / "capacity") >> capacity; std::ifstream(bat / "capacity") >> capacity;
std::ifstream(bat / "status") >> _status; std::ifstream(bat / "status") >> _status;
std::ifstream(bat / "current_now") >> current_now;
if (_status != "Unknown") { if (_status != "Unknown") {
status = _status; status = _status;
} }
total += capacity; total += capacity;
total_current += current_now;
} }
uint16_t capacity = total / batteries_.size(); 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) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; 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()) { if (!adapter_.empty()) {
bool online; bool online;
std::ifstream(adapter_ / "online") >> online; std::ifstream(adapter_ / "online") >> online;
if (capacity == 100) { if (capacity == 100) {
return "Full"; return "Full";
} }
return online ? "Charging" : "Discharging"; if (online) {
return current_now == 0 ? "Charging" : "Plugged";
}
return "Discharging";
} }
return "Unknown"; return "Unknown";
} }
auto waybar::modules::Battery::update() -> void { auto waybar::modules::Battery::update() -> void {
auto [capacity, status] = getInfos(); auto [capacity, current_now, status] = getInfos();
if (status == "Unknown") { if (status == "Unknown") {
status = getAdapterStatus(capacity); status = getAdapterStatus(capacity, current_now);
} }
if (tooltipEnabled()) { if (tooltipEnabled()) {
label_.set_tooltip_text(status); label_.set_tooltip_text(status);
@ -120,7 +131,9 @@ auto waybar::modules::Battery::update() -> void {
std::transform(status.begin(), status.end(), status.begin(), ::tolower); std::transform(status.begin(), status.end(), status.begin(), ::tolower);
auto format = format_; auto format = format_;
auto state = getState(capacity, true); 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); label_.get_style_context()->add_class(status);
old_status_ = status; old_status_ = status;
if (!state.empty() && config_["format-" + status + "-" + state].isString()) { if (!state.empty() && config_["format-" + status + "-" + state].isString()) {