Initial power console applet.

This commit is contained in:
2026-01-21 02:04:52 -05:00
commit 9de104b32c
5 changed files with 924 additions and 0 deletions

157
power_console_main.cc Normal file
View File

@ -0,0 +1,157 @@
#include "power_console.h"
#include <unistd.h>
#include <iostream>
#include <map>
#include <fstream>
#include <thread>
#include <sstream>
#include <string>
namespace C
{
const bool debug= false;
const bool debugUpdate= false or C::debugUpdate;
// NOTE: This one can be REALLY floody, if the updates are happening on schedule
const bool debugUpdateCalled= false or C::debug;
const double updateTimeout= 0.1;
}
const std::string conservePath= "/sys/devices/pci0000:00/0000:00:1f.0/PNP0C09:00/VPC2004:00/conservation_mode";
const std::map< int, Fl_Color > levelColors=
{
{ 15, FL_DARK_RED },
{ 33, FL_RED },
{ 45, FL_DARK_YELLOW },
{ 66, FL_YELLOW },
{ 75, FL_DARK_GREEN },
{ 90, FL_GREEN },
{ 100, FL_BLUE },
};
const std::map< std::string, Fl_Round_Button ** > powerMode=
{
{ "power-saver", &saver },
{ "balanced", &balance },
{ "performance", &perfButton },
};
std::string battLevelString= "UNKNOWN";
void
scheduledUpdate( void * )
{
// Ensure we get called again, on schedule:
Fl::repeat_timeout( C::updateTimeout, scheduledUpdate, nullptr );
if( C::debugUpdateCalled ) std::cerr << "Update!" << std::endl;
std::ifstream batt{ "/sys/class/power_supply/BAT0/capacity" };
batt >> battLevelString;
std::istringstream iss{ battLevelString };
int battlevel= 2;
iss >> battlevel;
level->value( battlevel );
level->selection_color( levelColors.upper_bound( battlevel )->second );
if( C::debugUpdate ) std::cerr << "Battery state read as: " << battlevel << std::endl;
std::ifstream conserved{ conservePath };
int cons;
conserved >> cons;
if( cons == 1 )
{
conserveButton->value( 1 );
}
else
{
conserveButton->value( 0 );
}
if( C::debugUpdate ) std::cerr << "Conservation state read as: " << cons << std::endl;
std::ifstream acCheck{ "/sys/class/power_supply/ADP0/online" };
int ac= -1;
acCheck >> ac;
acButton->value( ac );
if( C::debugUpdate ) std::cerr << "AC state read as: " << ac << std::endl;
}
// Since this requires launching bg processes, we only do it at startup and then every 30s.
void
profileUpdate( void * )
{
FILE *prof= popen( "powerprofilesctl get", "r" );
char buffer[ 1024 ]= "";
fscanf( prof, "%s", buffer );
pclose( prof );
( *powerMode.at( buffer ) )->setonly();
Fl::repeat_timeout( 30, profileUpdate, nullptr );
}
int
main( const int argcnt, char **argvec )
{
auto w= make_window();
level->minimum( 0 );
level->maximum( 100 );
level->value( 0 );
level->label( battLevelString.c_str() );
w->hotspot( w );
w->show( argcnt, argvec );
conserveButton->callback((Fl_Callback*)consClicked);
Fl::repeat_timeout( C::updateTimeout, scheduledUpdate, nullptr );
Fl::repeat_timeout( 0, profileUpdate, nullptr );
Fl::run();
}
void
consClicked( Fl_Light_Button *, void * )
{
std::cerr << "Conserve!" << std::endl;
std::ifstream oldConserved{ conservePath };
int state;
oldConserved >> state;
const int newState= 1 - state;
conserveButton->value( newState );
std::ofstream conserved{ conservePath };
conserved << newState;
std::cerr << "Tried to set new state to " << newState << std::endl;
}
void
setPowerMode( const std::string &mode )
{
( *powerMode.at( mode ) )->setonly();
std::thread bg{ [mode]{ system( ( "powerprofilesctl set " + mode ).c_str() ); } };
bg.detach();
}
void
saverChosen()
{
setPowerMode( "power-saver" );
}
void
balChosen()
{
setPowerMode( "balanced" );
}
void
perfChosen()
{
setPowerMode( "performance" );
}