feat(net): Add {linkSpeed}

pull/2891/head
Piotr Wegrzyn 2024-02-06 16:02:00 +01:00
parent 4c5ff80bbd
commit 8c30f43096
3 changed files with 28 additions and 2 deletions

View File

@ -47,6 +47,8 @@ class Network : public ALabel {
void clearIface(); void clearIface();
bool wildcardMatch(const std::string& pattern, const std::string& text) const; bool wildcardMatch(const std::string& pattern, const std::string& text) const;
std::optional<std::pair<unsigned long long, unsigned long long>> readBandwidthUsage(); std::optional<std::pair<unsigned long long, unsigned long long>> readBandwidthUsage();
uint32_t readLinkSpeed() const;
int ifid_; int ifid_;
sa_family_t family_; sa_family_t family_;
@ -79,6 +81,7 @@ class Network : public ALabel {
uint8_t signal_strength_; uint8_t signal_strength_;
std::string signal_strength_app_; std::string signal_strength_app_;
uint32_t route_priority; uint32_t route_priority;
uint32_t link_speed_;
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_timer_; util::SleeperThread thread_timer_;

View File

@ -163,6 +163,8 @@ Addressed by *network*
*{bandwidthTotalBytes}*: Instant total speed in bytes/seconds. *{bandwidthTotalBytes}*: Instant total speed in bytes/seconds.
*{linkSpeed}*: Ethernet link speed.
*{icon}*: Icon, as defined in *format-icons*. *{icon}*: Icon, as defined in *format-icons*.
# EXAMPLES # EXAMPLES

View File

@ -77,6 +77,22 @@ waybar::modules::Network::readBandwidthUsage() {
return {{receivedBytes, transmittedBytes}}; return {{receivedBytes, transmittedBytes}};
} }
uint32_t waybar::modules::Network::readLinkSpeed() const {
auto path = fmt::format("/sys/class/net/{}/speed", ifname_);
std::ifstream sysfs_speed(path);
if (!sysfs_speed)
return 0;
uint32_t speed;
sysfs_speed>>speed;
if(sysfs_speed.bad()) // read fails on incompatible devices
return 0;
return speed;
}
waybar::modules::Network::Network(const std::string &id, const Json::Value &config) waybar::modules::Network::Network(const std::string &id, const Json::Value &config)
: ALabel(config, "network", id, DEFAULT_FORMAT, 60), : ALabel(config, "network", id, DEFAULT_FORMAT, 60),
ifid_(-1), ifid_(-1),
@ -308,6 +324,8 @@ auto waybar::modules::Network::update() -> void {
bandwidth_up_total_ = up_octets; bandwidth_up_total_ = up_octets;
} }
link_speed_ = readLinkSpeed();
if (!alt_) { if (!alt_) {
auto state = getNetworkState(); auto state = getNetworkState();
if (!state_.empty() && label_.get_style_context()->has_class(state_)) { if (!state_.empty() && label_.get_style_context()->has_class(state_)) {
@ -349,7 +367,8 @@ auto waybar::modules::Network::update() -> void {
fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")), fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")),
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")), fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
fmt::arg("bandwidthTotalBytes", fmt::arg("bandwidthTotalBytes",
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s"))); pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s")));
if (text.compare(label_.get_label()) != 0) { if (text.compare(label_.get_label()) != 0) {
label_.set_markup(text); label_.set_markup(text);
if (text.empty()) { if (text.empty()) {
@ -382,7 +401,8 @@ auto waybar::modules::Network::update() -> void {
fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")), fmt::arg("bandwidthDownBytes", pow_format(bandwidth_down / interval_.count(), "B/s")),
fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")), fmt::arg("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")),
fmt::arg("bandwidthTotalBytes", fmt::arg("bandwidthTotalBytes",
pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s"))); pow_format((bandwidth_up + bandwidth_down) / interval_.count(), "B/s")),
fmt::arg("linkSpeed", pow_format(link_speed_ * 1000000ull, "b/s")));
if (label_.get_tooltip_text() != tooltip_text) { if (label_.get_tooltip_text() != tooltip_text) {
label_.set_tooltip_markup(tooltip_text); label_.set_tooltip_markup(tooltip_text);
} }
@ -415,6 +435,7 @@ void waybar::modules::Network::clearIface() {
signal_strength_dbm_ = 0; signal_strength_dbm_ = 0;
signal_strength_ = 0; signal_strength_ = 0;
signal_strength_app_.clear(); signal_strength_app_.clear();
link_speed_ = 0;
frequency_ = 0.0; frequency_ = 0.0;
} }