diff --git a/CHANGELOG.md b/CHANGELOG.md index 0426b63d..1e1a03d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `DEBUG_SHADED` cmake variable and its associated functionality. ### Added +- `internal/network`: New token `%mac%` + ([2569](https://github.com/polybar/polybar/pull/2569)) - Polybar can now read config files from stdin: `polybar -c /dev/stdin`. - `custom/script`: Implement `env-*` config option. ([2090](https://github.com/polybar/polybar/issues/2090)) diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index 4785db78..05a13036 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -68,6 +68,7 @@ namespace net { struct link_status { string ip; string ip6; + string mac; link_activity previous{}; link_activity current{}; }; @@ -86,6 +87,7 @@ namespace net { string ip() const; string ip6() const; + string mac() const; string downspeed(int minwidth = 3, const string& unit = "B/s") const; string upspeed(int minwidth = 3, const string& unit = "B/s") const; void set_unknown_up(bool unknown = true); diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp index cc351f21..1fc5f182 100644 --- a/src/adapters/net.cpp +++ b/src/adapters/net.cpp @@ -31,6 +31,7 @@ namespace net { }; static const string NO_IP = string("N/A"); + static const string NO_MAC = string("N/A"); static const string NET_PATH = "/sys/class/net/"; static const string VIRTUAL_PATH = "/sys/devices/virtual/"; @@ -150,6 +151,11 @@ namespace net { return false; } + m_status.mac = string_util::trim(file_util::contents(NET_PATH + m_interface + "/address"), isspace); + if (m_status.mac == "") { + m_status.mac = NO_MAC; + } + for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_addr == nullptr) { continue; @@ -228,6 +234,12 @@ namespace net { string network::ip() const { return m_status.ip; } + /** + * Get interface mac address + */ + string network::mac() const { + return m_status.mac; + } /** * Get interface ipv6 address diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 0eac9f76..78199b89 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -154,6 +154,7 @@ namespace modules { label->reset_tokens(); label->replace_token("%ifname%", m_interface); label->replace_token("%local_ip%", network->ip()); + label->replace_token("%mac%", network->mac()); label->replace_token("%local_ip6%", network->ip6()); label->replace_token("%upspeed%", upspeed); label->replace_token("%downspeed%", downspeed);