2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-04-24 13:05:43 +02:00
#include <unistd.h>
2021-03-03 21:03:12 +01:00
#include <dirent.h>
2021-04-15 18:59:26 +02:00
#define FF_BATTERY_MODULE_NAME "Battery"
#define FF_BATTERY_NUM_FORMAT_ARGS 5
2021-04-03 21:51:50 +02:00
2021-04-24 13:05:43 +02:00
static void printBattery ( FFinstance * instance , FFstrbuf * dir , uint32_t index )
2021-02-18 22:25:36 +01:00
{
2021-04-24 13:05:43 +02:00
uint32_t dirLength = dir -> length ;
2021-03-21 16:52:17 +01:00
FF_STRBUF_CREATE ( manufactor );
2021-04-24 13:05:43 +02:00
ffStrbufAppendS ( dir , "/manufacturer" );
ffGetFileContent ( dir -> chars , & manufactor );
ffStrbufSubstrBefore ( dir , dirLength );
2021-03-03 21:03:12 +01:00
2021-03-21 16:52:17 +01:00
FF_STRBUF_CREATE ( model );
2021-04-24 13:05:43 +02:00
ffStrbufAppendS ( dir , "/model_name" );
ffGetFileContent ( dir -> chars , & model );
ffStrbufSubstrBefore ( dir , dirLength );
2021-03-03 21:03:12 +01:00
2021-03-21 16:52:17 +01:00
FF_STRBUF_CREATE ( technology );
2021-04-24 13:05:43 +02:00
ffStrbufAppendS ( dir , "/technology" );
ffGetFileContent ( dir -> chars , & technology );
ffStrbufSubstrBefore ( dir , dirLength );
2021-02-18 22:25:36 +01:00
2021-03-21 16:52:17 +01:00
FF_STRBUF_CREATE ( capacity );
2021-04-24 13:05:43 +02:00
ffStrbufAppendS ( dir , "/capacity" );
ffGetFileContent ( dir -> chars , & capacity );
ffStrbufSubstrBefore ( dir , dirLength );
2021-02-18 22:25:36 +01:00
2021-03-21 16:52:17 +01:00
FF_STRBUF_CREATE ( status );
2021-04-24 13:05:43 +02:00
ffStrbufAppendS ( dir , "/status" );
ffGetFileContent ( dir -> chars , & status );
ffStrbufSubstrBefore ( dir , dirLength );
2021-03-03 21:03:12 +01:00
2021-05-05 21:29:09 +02:00
if ( ffStrbufIgnCaseCompS ( & status , "Unknown" ) == 0 )
2021-05-05 19:52:50 +02:00
ffStrbufClear ( & status );
2021-05-30 21:35:04 +02:00
if ( capacity . length == 0 && status . length == 0 )
2021-04-15 18:59:26 +02:00
{
2021-04-24 13:05:43 +02:00
ffPrintError ( instance , FF_BATTERY_MODULE_NAME , index , & instance -> config . batteryKey , & instance -> config . batteryFormat , FF_BATTERY_NUM_FORMAT_ARGS , "No file in %s could be read or all battery options are disabled" , dir -> chars );
2021-04-03 21:51:50 +02:00
return ;
2021-04-03 11:39:59 +02:00
}
2021-03-01 19:53:20 +01:00
2021-03-23 19:48:52 +01:00
if ( instance -> config . batteryFormat . length == 0 )
2021-03-04 20:49:12 +01:00
{
2021-03-27 15:39:56 +01:00
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , FF_BATTERY_MODULE_NAME , index , & instance -> config . batteryKey );
2021-03-27 15:39:56 +01:00
2021-05-30 21:37:27 +02:00
bool showStatus = status . length > 0 && ffStrbufIgnCaseCompS ( & status , "Full" ) != 0 ;
2021-03-23 19:48:52 +01:00
if ( capacity . length > 0 )
2021-03-19 20:57:07 +01:00
{
2021-05-30 21:35:04 +02:00
ffStrbufWriteTo ( & capacity , stdout );
putchar ( '%' );
2021-03-27 18:14:19 +01:00
2021-05-30 21:37:27 +02:00
if ( showStatus )
2021-06-14 23:22:14 +02:00
fputs ( "; " , stdout );
2021-03-19 20:57:07 +01:00
}
2021-05-30 21:35:04 +02:00
2021-05-30 21:37:27 +02:00
if ( showStatus )
2021-05-30 21:35:04 +02:00
ffStrbufWriteTo ( & status , stdout );
putchar ( '\n' );
2021-02-19 16:23:41 +01:00
}
2021-03-01 19:53:20 +01:00
else
2021-02-19 16:23:41 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , FF_BATTERY_MODULE_NAME , index , & instance -> config . batteryKey , & instance -> config . batteryFormat , NULL , FF_BATTERY_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_STRBUF , & manufactor },
{ FF_FORMAT_ARG_TYPE_STRBUF , & model },
{ FF_FORMAT_ARG_TYPE_STRBUF , & technology },
{ FF_FORMAT_ARG_TYPE_STRBUF , & capacity },
{ FF_FORMAT_ARG_TYPE_STRBUF , & status }
});
2021-02-19 16:23:41 +01:00
}
2021-03-21 16:52:17 +01:00
ffStrbufDestroy ( & manufactor );
ffStrbufDestroy ( & model );
ffStrbufDestroy ( & technology );
ffStrbufDestroy ( & capacity );
ffStrbufDestroy ( & status );
2021-03-03 21:03:12 +01:00
}
2021-02-18 22:25:36 +01:00
2021-03-03 21:03:12 +01:00
void ffPrintBattery ( FFinstance * instance )
{
2021-04-24 13:05:43 +02:00
FFstrbuf baseDir ;
ffStrbufInitA ( & baseDir , 64 );
if ( instance -> config . batteryDir . length > 0 )
2021-04-24 14:37:40 +02:00
{
2021-04-24 13:05:43 +02:00
ffStrbufAppend ( & baseDir , & instance -> config . batteryDir );
2021-04-24 14:37:40 +02:00
if ( baseDir . length == 0 )
{
ffPrintError ( instance , FF_BATTERY_MODULE_NAME , 0 , & instance -> config . batteryKey , & instance -> config . batteryFormat , FF_BATTERY_NUM_FORMAT_ARGS , "custom battery dir is an empty string" );
ffStrbufDestroy ( & baseDir );
return ;
}
if ( baseDir . chars [ baseDir . length - 1 ] != '/' )
ffStrbufAppendC ( & baseDir , '/' );
}
2021-04-24 13:05:43 +02:00
else
ffStrbufAppendS ( & baseDir , "/sys/class/power_supply/" );
2021-04-24 14:37:40 +02:00
2021-04-24 13:05:43 +02:00
uint32_t baseDirLength = baseDir . length ;
2021-04-15 18:59:26 +02:00
2021-04-24 13:05:43 +02:00
DIR * dirp = opendir ( baseDir . chars );
if ( dirp == NULL )
2021-03-03 21:03:12 +01:00
{
2021-04-24 13:05:43 +02:00
ffPrintError ( instance , FF_BATTERY_MODULE_NAME , 0 , & instance -> config . batteryKey , & instance -> config . batteryFormat , FF_BATTERY_NUM_FORMAT_ARGS , "opendir( \" %s \" ) == NULL" , baseDir . chars );
ffStrbufDestroy ( & baseDir );
return ;
}
FFlist dirs ;
ffListInitA ( & dirs , sizeof ( FFstrbuf ), 4 );
2021-03-03 21:03:12 +01:00
2021-04-24 13:05:43 +02:00
struct dirent * entry ;
while (( entry = readdir ( dirp )) != NULL )
{
ffStrbufAppendS ( & baseDir , entry -> d_name );
ffStrbufAppendS ( & baseDir , "/capacity" );
if ( access ( baseDir . chars , F_OK ) == 0 )
2021-03-03 21:03:12 +01:00
{
2021-04-24 13:05:43 +02:00
FFstrbuf * name = ffListAdd ( & dirs );
ffStrbufInit ( name );
ffStrbufSetS ( name , entry -> d_name );
2021-03-03 21:03:12 +01:00
}
2021-04-24 13:05:43 +02:00
ffStrbufSubstrBefore ( & baseDir , baseDirLength );
2021-03-03 21:03:12 +01:00
}
2021-04-03 11:39:59 +02:00
2021-04-24 13:05:43 +02:00
if ( dirs . length == 0 )
2021-04-03 11:39:59 +02:00
{
2021-04-24 13:17:30 +02:00
ffPrintError ( instance , FF_BATTERY_MODULE_NAME , 0 , & instance -> config . batteryKey , & instance -> config . batteryFormat , FF_BATTERY_NUM_FORMAT_ARGS , "%s doesn't contain any battery folder" , baseDir . chars );
2021-04-24 13:05:43 +02:00
ffListDestroy ( & dirs );
ffStrbufDestroy ( & baseDir );
2021-04-15 18:59:26 +02:00
return ;
}
2021-04-03 11:39:59 +02:00
2021-04-24 13:05:43 +02:00
for ( uint32_t i = 0 ; i < dirs . length ; i ++ )
2021-04-15 18:59:26 +02:00
{
2021-04-24 13:05:43 +02:00
FFstrbuf * name = ffListGet ( & dirs , i );
ffStrbufAppend ( & baseDir , name );
2021-04-24 14:25:27 +02:00
printBattery ( instance , & baseDir , dirs . length == 1 ? 0 : i + 1 );
2021-04-24 13:05:43 +02:00
ffStrbufSubstrBefore ( & baseDir , baseDirLength );
ffStrbufDestroy ( name );
2021-04-03 11:39:59 +02:00
}
2021-04-24 13:05:43 +02:00
ffListDestroy ( & dirs );
ffStrbufDestroy ( & baseDir );
2021-03-27 18:14:19 +01:00
}