Add package widget

Polling is done via user script and a pipe. Since it is only run every
5 minutes by default, this should be fine

Partially implements https://github.com/scorpion-26/gBar/issues/14
This commit is contained in:
scorpion-26 2023-05-03 18:37:36 +02:00
parent 3517fa5de8
commit 299b497748
10 changed files with 128 additions and 1 deletions

View file

@ -87,6 +87,7 @@ Bar:
- GPU stats (Nvidia/AMD only): Utilisation, temperature, VRAM - GPU stats (Nvidia/AMD only): Utilisation, temperature, VRAM
- Disk: Free/Total - Disk: Free/Total
- Network: Current upload and download speed - Network: Current upload and download speed
- Update checking (Non-Arch systems need to be configured manually)
Bluetooth: Bluetooth:
- Scanning of nearby bluetooth devices - Scanning of nearby bluetooth devices

View file

@ -101,6 +101,12 @@ highlight {
background-color: #bd93f9; background-color: #bd93f9;
} }
.package-outofdate {
font-size: 24px;
margin-right: -5px;
color: #ff5555;
}
.bt-num { .bt-num {
font-size: 16px; font-size: 16px;
color: #1793D1; color: #1793D1;

View file

@ -1 +1 @@
{"version":3,"sourceRoot":"","sources":["style.scss"],"names":[],"mappings":"AAmBA;EACI;EACA;;;AASJ;EACI,kBA7BC;EA8BD;;;AAGJ;EACI;EACA;;;AAGJ;EACI,WAxBO;;;AA2BX;EACI;EAEA;EAEA,OA5CO;;;AA8CX;EACI;EAGA,OAlDO;;;AAoDX;EACI;EAGA,OAxDO;;;AA2DX;EACI;EAGA,OAzDE;;;AA+DN;EACI;;;AAIJ;EACI;EACA;EACA;EACA,kBA/EO;EAgFP;EACA;EACA;;;AAGJ;EAEI;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;;;AAGJ;EACI;EACA,OArGK;EAsGL;;;AAIA;EACI,kBA/GG;;AAkHP;EACI;;AAGJ;EACI,kBAnHC;;;AAuHT;EACI;EACA,OAvHK;EAwHL;;;AAIA;EACI,kBAnIG;;AAsIP;EACI;;AAGJ;EACI,kBArIC;;;AAyIT;EACI,WAnIO;EAoIP,OAtIK;EAuIL;;;AAEJ;EACI;EACA,OA3IK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I,kBAtVC;EAuVD;;;AAEJ;EACI;EACA;EACA;EACA;EACA,OAjVK;;;AAmVT;EACI;EACA;;;AAEJ;EAgBI;EACA;EACA;EACA;EACA;EAEA;EACA;EAEA;;AAxBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA;;AAEJ;EACI,OAzWF;;;AAuXN;EACI,OAxXE;EAyXF,kBAhYO;EAiYP;EACH;EACA;;;AAED;EAaI,OA1YK;EA2YL,kBAnZO;EAoZP;EACA;EACH;EACG;;AAjBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA","file":"style.css"} {"version":3,"sourceRoot":"","sources":["style.scss"],"names":[],"mappings":"AAmBA;EACI;EACA;;;AASJ;EACI,kBA7BC;EA8BD;;;AAGJ;EACI;EACA;;;AAGJ;EACI,WAxBO;;;AA2BX;EACI;EAEA;EAEA,OA5CO;;;AA8CX;EACI;EAGA,OAlDO;;;AAoDX;EACI;EAGA,OAxDO;;;AA2DX;EACI;EAGA,OAzDE;;;AA+DN;EACI;;;AAIJ;EACI;EACA;EACA;EACA,kBA/EO;EAgFP;EACA;EACA;;;AAGJ;EAEI;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGJ;EACI;EACA;EACA;EACA;;;AAGJ;EACI;EACA,OArGK;EAsGL;;;AAIA;EACI,kBA/GG;;AAkHP;EACI;;AAGJ;EACI,kBAnHC;;;AAuHT;EACI;EACA,OAvHK;EAwHL;;;AAIA;EACI,kBAnIG;;AAsIP;EACI;;AAGJ;EACI,kBArIC;;;AAyIT;EACI;EACA;EACA,OA3IE;;;AA8IN;EACI,WAzIO;EA0IP,OA5IK;EA6IL;;;AAEJ;EACI;EACA,OAjJK;EAkJL;;;AAEJ;EACI;EACA,OAtJK;EAuJL;;;AAEJ;EACI;EACA,OA3JK;EA4JL;;;AAGJ;EACI,OArKK;EAsKL,kBA5KO;EA6KP,WAhKO;;;AAkKX;EACI,OA1KK;EA2KL;EACA,WArKO;;;AAwKX;EACI,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EACA;EACA;;AAEJ;EACI,OA/WF;;;AA6XN;EACI,OA9XE;EA+XF,kBAtYO;EAuYP;EACH;EACA;;;AAED;EAaI,OAhZK;EAiZL,kBAzZO;EA0ZP;EACA;EACH;EACG;;AAjBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA","file":"style.css"}

View file

@ -145,6 +145,12 @@ highlight {
} }
} }
.package-outofdate {
font-size: 24px;
margin-right: -5px;
color: $red;
}
.bt-num { .bt-num {
font-size: $textsize; font-size: $textsize;
color: $btblue; color: $btblue;

View file

@ -52,6 +52,14 @@ AudioRevealer: false
# Sets the rate of change of the slider on each scroll. In Percent # Sets the rate of change of the slider on each scroll. In Percent
AudioScrollSpeed: 5 AudioScrollSpeed: 5
# Command that is run to check if there are out-of-date packages.
# The script should return *ONLY* a number. If it doesn't output a number, updates are no longer checked.
# Default value is applicable for Arch Linux. (See src/Config.h for explanation on the default command)
CheckPackagesCommand: pac="$(checkupdates)"; if [ $? -eq 127 ] ; then exit 127; fi; echo $pac | wc -l
# How often to check for updates. In seconds
CheckUpdateInterval: 300
# Limits the range of the audio slider. Only works for audio output. # Limits the range of the audio slider. Only works for audio output.
# Slider "empty" is AudioMinVolume, Slider "full" is AudioMaxVolume # Slider "empty" is AudioMinVolume, Slider "full" is AudioMaxVolume
# AudioMinVolume: 30 # Audio can't get below 30% # AudioMinVolume: 30 # Audio can't get below 30%

View file

@ -3,6 +3,7 @@
#include "System.h" #include "System.h"
#include "Common.h" #include "Common.h"
#include "Config.h" #include "Config.h"
#include <mutex>
namespace Bar namespace Bar
{ {
@ -134,6 +135,30 @@ namespace Bar
} }
#endif #endif
static std::mutex packageTextLock;
static TimerResult UpdatePackages(Text& text)
{
System::GetOutdatedPackagesAsync(
[&](uint32_t numOutdatedPackages)
{
packageTextLock.lock();
if (numOutdatedPackages)
{
text.SetText("󰏔 ");
text.SetClass("package-outofdate");
text.SetTooltip("Updates available! (" + std::to_string(numOutdatedPackages) + " packages)");
}
else
{
text.SetText("");
text.SetClass("package-empty");
text.SetTooltip("");
}
packageTextLock.unlock();
});
return TimerResult::Ok;
}
void OnChangeVolumeSink(Slider&, double value) void OnChangeVolumeSink(Slider&, double value)
{ {
System::SetVolumeSink(value); System::SetVolumeSink(value);
@ -385,6 +410,16 @@ namespace Bar
parent.AddTimer<Widget>(DynCtx::UpdateAudio, DynCtx::updateTimeFast); parent.AddTimer<Widget>(DynCtx::UpdateAudio, DynCtx::updateTimeFast);
} }
void WidgetPackages(Widget& parent)
{
auto text = Widget::Create<Text>();
text->SetText("");
text->SetClass("package-empty");
text->AddTimer<Text>(DynCtx::UpdatePackages, 1000 * Config::Get().checkUpdateInterval,
TimerDispatchBehaviour::ImmediateDispatch);
parent.AddChild(std::move(text));
}
#ifdef WITH_BLUEZ #ifdef WITH_BLUEZ
void WidgetBluetooth(Widget& parent) void WidgetBluetooth(Widget& parent)
{ {
@ -609,6 +644,9 @@ namespace Bar
right->SetSpacing({8, false}); right->SetSpacing({8, false});
right->SetHorizontalTransform({-1, true, Alignment::Right}); right->SetHorizontalTransform({-1, true, Alignment::Right});
{ {
// Needs to be rightmost, since an empty package string introduces additional padding
WidgetPackages(*right);
WidgetAudio(*right); WidgetAudio(*right);
#ifdef WITH_BLUEZ #ifdef WITH_BLUEZ

View file

@ -133,6 +133,7 @@ void Config::Load()
AddConfigVar("ExitCommand", config.exitCommand, lineView, foundProperty); AddConfigVar("ExitCommand", config.exitCommand, lineView, foundProperty);
AddConfigVar("BatteryFolder", config.batteryFolder, lineView, foundProperty); AddConfigVar("BatteryFolder", config.batteryFolder, lineView, foundProperty);
AddConfigVar("DefaultWorkspaceSymbol", config.defaultWorkspaceSymbol, lineView, foundProperty); AddConfigVar("DefaultWorkspaceSymbol", config.defaultWorkspaceSymbol, lineView, foundProperty);
AddConfigVar("CheckPackagesCommand", config.checkPackagesCommand, lineView, foundProperty);
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)
{ {
// Subtract 1 to index from 1 to 9 rather than 0 to 8 // Subtract 1 to index from 1 to 9 rather than 0 to 8
@ -152,6 +153,8 @@ void Config::Load()
AddConfigVar("MinDownloadBytes", config.minDownloadBytes, lineView, foundProperty); AddConfigVar("MinDownloadBytes", config.minDownloadBytes, lineView, foundProperty);
AddConfigVar("MaxDownloadBytes", config.maxDownloadBytes, lineView, foundProperty); AddConfigVar("MaxDownloadBytes", config.maxDownloadBytes, lineView, foundProperty);
AddConfigVar("CheckUpdateInterval", config.checkUpdateInterval, lineView, foundProperty);
AddConfigVar("AudioScrollSpeed", config.audioScrollSpeed, lineView, foundProperty); AddConfigVar("AudioScrollSpeed", config.audioScrollSpeed, lineView, foundProperty);
AddConfigVar("AudioMinVolume", config.audioMinVolume, lineView, foundProperty); AddConfigVar("AudioMinVolume", config.audioMinVolume, lineView, foundProperty);

View file

@ -14,6 +14,11 @@ public:
std::vector<std::string> workspaceSymbols = std::vector<std::string>(9, ""); std::vector<std::string> workspaceSymbols = std::vector<std::string>(9, "");
std::string defaultWorkspaceSymbol = ""; std::string defaultWorkspaceSymbol = "";
// Script that returns how many packages are out-of-date. The script should only print a number!
// The default script runs checkupdates, and forcefully exits when checkupdates is not found, so gBar can disable the package widget.
// "checkupdates | wc -l" would always return 0 on stdout, which gBar accepts
std::string checkPackagesCommand = "pac=\"$(checkupdates)\"; if [ $? -eq 127 ] ; then exit 127; fi; echo $pac | wc -l";
bool centerTime = true; bool centerTime = true;
bool audioRevealer = false; bool audioRevealer = false;
bool audioInput = false; bool audioInput = false;
@ -30,6 +35,8 @@ public:
uint32_t audioScrollSpeed = 5; // 5% each scroll uint32_t audioScrollSpeed = 5; // 5% each scroll
uint32_t checkUpdateInterval = 5 * 60; // Interval to run the "checkPackagesCommand". In seconds
// Only affects outputs (i.e.: speakers, not microphones). This remaps the range of the volume; In percent // Only affects outputs (i.e.: speakers, not microphones). This remaps the range of the volume; In percent
double audioMinVolume = 0.f; // Map the minimum volume to this value double audioMinVolume = 0.f; // Map the minimum volume to this value
double audioMaxVolume = 100.f; // Map the maximum volume to this value double audioMaxVolume = 100.f; // Map the maximum volume to this value
@ -67,5 +74,7 @@ public:
bool hasNet = true; bool hasNet = true;
bool hasPackagesScript = true;
static RuntimeConfig& Get(); static RuntimeConfig& Get();
}; };

View file

@ -8,6 +8,7 @@
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <mutex>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -534,6 +535,59 @@ namespace System
return GetNetworkBpsCommon(dt, prevDownloadBytes, "/sys/class/net/" + Config::Get().networkAdapter + "/statistics/rx_bytes"); return GetNetworkBpsCommon(dt, prevDownloadBytes, "/sys/class/net/" + Config::Get().networkAdapter + "/statistics/rx_bytes");
} }
void GetOutdatedPackagesAsync(std::function<void(uint32_t)>&& returnVal)
{
static std::mutex configMutex;
configMutex.lock();
if (!RuntimeConfig::Get().hasPackagesScript)
{
configMutex.unlock();
return; // Don't bother
}
configMutex.unlock();
std::thread(
[](std::function<void(uint32_t)> returnVal)
{
// We need a pipe, since there is no "libpacman". This should only be called every so often anyways
std::string number;
char buf[2056];
FILE* pipe = popen(Config::Get().checkPackagesCommand.c_str(), "r"); // Redirect stderr
ASSERT(pipe, "GetOutdatedPackages: Couldn't open pipe");
while (fgets(buf, sizeof(buf), pipe) != 0)
{
number.append(buf);
}
ASSERT(feof(pipe), "GetOutdatedPackages: Cannot read to eof!");
int exitCode = pclose(pipe) / 256;
if (exitCode == 127)
{
configMutex.lock();
// Invalid script
LOG("GetOutdatedPackages: Invalid command. Disabling package widget!");
RuntimeConfig::Get().hasPackagesScript = false;
configMutex.unlock();
return;
}
try
{
returnVal(std::stoul(buf));
}
catch (std::invalid_argument)
{
configMutex.lock();
LOG("GetOutdatedPackages: Invalid output of the package script. Disabling package widget!");
RuntimeConfig::Get().hasPackagesScript = false;
configMutex.unlock();
return;
}
},
std::move(returnVal))
.detach();
}
std::string GetTime() std::string GetTime()
{ {
time_t stdTime = time(NULL); time_t stdTime = time(NULL);

View file

@ -105,6 +105,8 @@ namespace System
// Bytes per second download. dx is time since last call. Will always return 0 on first run // Bytes per second download. dx is time since last call. Will always return 0 on first run
double GetNetworkBpsDownload(double dt); double GetNetworkBpsDownload(double dt);
void GetOutdatedPackagesAsync(std::function<void(uint32_t)>&& returnVal);
std::string GetTime(); std::string GetTime();
void Shutdown(); void Shutdown();