Rudimentary configuration file system.

This commit is contained in:
2026-01-26 03:05:35 -05:00
parent f56e86f34b
commit 9fca970fc5
2 changed files with 138 additions and 14 deletions

View File

@ -11,6 +11,8 @@
#include <string>
#include <filesystem>
#include "ConfigFile.h"
namespace C
{
const bool debug= false;
@ -20,12 +22,15 @@ namespace C
const bool debugUpdateCalled= false or C::debug;
const double updateTimeout= 0.1;
}
const std::filesystem::path conservePath= "/sys/devices/pci0000:00/0000:00:1f.0/PNP0C09:00/VPC2004:00/conservation_mode";
const std::filesystem::path powerPath= "/sys/class/power_supply/ADP0";
const std::filesystem::path batteryPath= "/sys/class/power_supply/BAT0";
const std::filesystem::path backlightPath= "/sys/class/backlight/intel_backlight";
const std::filesystem::path defaultConservePath= "/sys/devices/pci0000:00/0000:00:1f.0/PNP0C09:00/VPC2004:00/conservation_mode";
const std::filesystem::path defaultPowerPath= "/sys/class/power_supply/ADP0";
const std::filesystem::path defaultBatteryPath= "/sys/class/power_supply/BAT0";
const std::filesystem::path defaultBacklightPath= "/sys/class/backlight/intel_backlight";
const std::filesystem::path path= "/";
}
const std::map< int, Fl_Color > levelColors=
{
@ -45,6 +50,10 @@ const std::map< std::string, Fl_Round_Button ** > powerMode=
{ "performance", &perfButton },
};
using Config::exports::ConfigFile;
std::unique_ptr< ConfigFile > config;
std::string battLevelString= "UNKNOWN";
void
@ -54,7 +63,7 @@ scheduledUpdate( void * )
Fl::repeat_timeout( C::updateTimeout, scheduledUpdate, nullptr );
if( C::debugUpdateCalled ) std::cerr << "Update!" << std::endl;
std::ifstream batt{ batteryPath / "capacity" };
std::ifstream batt{ C::path / config->get( "battery_path" ) / "capacity" };
batt >> battLevelString;
std::istringstream iss{ battLevelString };
int battlevel= 2;
@ -63,7 +72,7 @@ scheduledUpdate( void * )
level->selection_color( levelColors.upper_bound( battlevel )->second );
if( C::debugUpdate ) std::cerr << "Battery state read as: " << battlevel << std::endl;
std::ifstream conserved{ conservePath };
std::ifstream conserved{ C::path / config->get( "conserve_path" ) };
int cons;
conserved >> cons;
if( cons == 1 )
@ -76,20 +85,20 @@ scheduledUpdate( void * )
}
if( C::debugUpdate ) std::cerr << "Conservation state read as: " << cons << std::endl;
std::ifstream acCheck{ powerPath / "online" };
std::ifstream acCheck{ C::path / config->get( "power_path" ) / "online" };
int ac= -1;
acCheck >> ac;
acButton->value( ac );
if( C::debugUpdate ) std::cerr << "AC state read as: " << ac << std::endl;
std::ifstream i_max{ backlightPath / "max_brightness" };
std::ifstream i_max{ C::path / config->get( "backlight_path" ) / "max_brightness" };
int max;
i_max >> max;
std::ifstream inf{ backlightPath / "brightness" };
std::ifstream inf{ C::path / config->get( "backlight_path" ) / "brightness" };
int in;
inf >> in;
@ -113,6 +122,21 @@ profileUpdate( void * )
int
main( const int argcnt, char **argvec )
{
config= std::make_unique< ConfigFile >
(
ConfigFile
{
std::ifstream{ C::path / ::getenv( "HOME" ) / ".power_console.conf" },
{
{ "battery_path", C::defaultBatteryPath.string() },
{ "power_path", C::defaultPowerPath.string() },
{ "backlight_path", C::defaultBacklightPath.string() },
{ "conserve_path", C::defaultConservePath.string() },
}
}
);
auto w= make_window();
level->minimum( 0 );
@ -141,12 +165,12 @@ void
consClicked( Fl_Light_Button *, void * )
{
std::cerr << "Conserve!" << std::endl;
std::ifstream oldConserved{ conservePath };
std::ifstream oldConserved{ C::path / config->get( "conserve_path" ) };
int state;
oldConserved >> state;
const int newState= 1 - state;
conserveButton->value( newState );
std::ofstream conserved{ conservePath };
std::ofstream conserved{ C::path / config->get( "conserve_path" ) };
conserved << newState;
std::cerr << "Tried to set new state to " << newState << std::endl;
}
@ -182,7 +206,7 @@ namespace
void
changeBrightness( int pct )
{
std::ifstream i_max{ backlightPath / "max_brightness" };
std::ifstream i_max{ C::path / config->get( "backlight_path" ) / "max_brightness" };
int max;
i_max >> max;
@ -191,7 +215,7 @@ namespace
int adj= std::clamp( max * pct / 100, 1, max );
std::cerr << "Adjustment computed: " << adj << std::endl;
std::ofstream out{ backlightPath / "brightness" };
std::ofstream out{ C::path / config->get( "backlight_path" ) / "brightness" };
out << adj;
}