Add Net sanity checking

This disables network if the tx_bytes file is not found on Init()
This commit is contained in:
scorpion-26 2023-02-12 20:45:05 +01:00
parent 35b7065879
commit 906a1259c3
3 changed files with 19 additions and 2 deletions

View file

@ -555,7 +555,7 @@ namespace Bar
if (RuntimeConfig::Get().hasBlueZ)
WidgetBluetooth(*right);
#endif
if (Config::Get().networkWidget)
if (Config::Get().networkWidget && RuntimeConfig::Get().hasNet)
WidgetNetwork(*right);
WidgetSensors(*right);

View file

@ -57,5 +57,7 @@ public:
bool hasBlueZ = false;
#endif
bool hasNet = true;
static RuntimeConfig& Get();
};

View file

@ -467,10 +467,23 @@ namespace System
}
#endif
void CheckNetwork()
{
std::ifstream bytes("/sys/class/net/" + Config::Get().networkAdapter + "/statistics/tx_bytes");
if (!bytes.is_open())
{
LOG("Cannot open network device! Disabling Network widget.");
RuntimeConfig::Get().hasNet = false;
}
}
double GetNetworkBpsCommon(double dt, uint64_t& prevBytes, const std::string& deviceFile)
{
if (!RuntimeConfig::Get().hasNet)
{
return 0.f;
}
std::ifstream bytes(deviceFile);
ASSERT(bytes.is_open(), "Couldn't open " << deviceFile);
std::string bytesStr;
std::getline(bytes, bytesStr);
@ -563,6 +576,8 @@ namespace System
#endif
PulseAudio::Init();
CheckNetwork();
}
void FreeResources()
{