diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 47701b4e..c25aaa65 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -47,6 +47,8 @@ class Network : public ALabel { void clearIface(); bool wildcardMatch(const std::string& pattern, const std::string& text) const; std::optional> readBandwidthUsage(); + uint32_t readLinkSpeed() const; + int ifid_; sa_family_t family_; @@ -79,6 +81,7 @@ class Network : public ALabel { uint8_t signal_strength_; std::string signal_strength_app_; uint32_t route_priority; + uint32_t link_speed_; util::SleeperThread thread_; util::SleeperThread thread_timer_; diff --git a/man/waybar-network.5.scd b/man/waybar-network.5.scd index 08c86d3d..2d15b0c1 100644 --- a/man/waybar-network.5.scd +++ b/man/waybar-network.5.scd @@ -163,6 +163,8 @@ Addressed by *network* *{bandwidthTotalBytes}*: Instant total speed in bytes/seconds. +*{linkSpeed}*: Ethernet link speed. + *{icon}*: Icon, as defined in *format-icons*. # EXAMPLES diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 654afbe8..e095055d 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -77,6 +77,22 @@ waybar::modules::Network::readBandwidthUsage() { 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) : ALabel(config, "network", id, DEFAULT_FORMAT, 60), ifid_(-1), @@ -308,6 +324,8 @@ auto waybar::modules::Network::update() -> void { bandwidth_up_total_ = up_octets; } + link_speed_ = readLinkSpeed(); + if (!alt_) { auto state = getNetworkState(); 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("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")), 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) { label_.set_markup(text); 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("bandwidthUpBytes", pow_format(bandwidth_up / interval_.count(), "B/s")), 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) { label_.set_tooltip_markup(tooltip_text); } @@ -415,6 +435,7 @@ void waybar::modules::Network::clearIface() { signal_strength_dbm_ = 0; signal_strength_ = 0; signal_strength_app_.clear(); + link_speed_ = 0; frequency_ = 0.0; }