2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-04-15 18:59:26 +02:00
#define FF_HOST_MODULE_NAME "Host"
#define FF_HOST_NUM_FORMAT_ARGS 3
2021-05-14 16:03:19 +02:00
static bool hostValueSet ( FFstrbuf * value )
2021-04-23 20:59:13 +02:00
{
2021-06-16 10:50:16 +02:00
ffStrbufTrimRight ( value , '\n' );
2021-05-14 16:03:19 +02:00
ffStrbufTrim ( value , ' ' );
2021-04-23 20:59:13 +02:00
return
value -> length > 0 &&
2021-06-16 10:50:16 +02:00
ffStrbufStartsWithIgnCaseS ( value , "To be filled" ) != true &&
ffStrbufStartsWithIgnCaseS ( value , "To be set" ) != true &&
ffStrbufStartsWithIgnCaseS ( value , "OEM" ) != true &&
ffStrbufStartsWithIgnCaseS ( value , "O.E.M." ) != true &&
2021-04-23 20:59:13 +02:00
ffStrbufIgnCaseCompS ( value , "None" ) != 0 &&
2021-06-16 10:50:16 +02:00
ffStrbufIgnCaseCompS ( value , "System Product" ) != 0 &&
2021-05-14 16:03:19 +02:00
ffStrbufIgnCaseCompS ( value , "System Product Name" ) != 0 &&
2021-06-16 10:50:16 +02:00
ffStrbufIgnCaseCompS ( value , "System Product Version" ) != 0 &&
ffStrbufIgnCaseCompS ( value , "System Name" ) != 0 &&
2021-06-14 23:22:14 +02:00
ffStrbufIgnCaseCompS ( value , "System Version" ) != 0 &&
2021-06-16 10:50:16 +02:00
ffStrbufIgnCaseCompS ( value , "Default string" ) != 0 &&
ffStrbufIgnCaseCompS ( value , "Undefined" ) != 0 &&
ffStrbufIgnCaseCompS ( value , "Not Specified" ) != 0 &&
ffStrbufIgnCaseCompS ( value , "Not Applicable" ) != 0 &&
ffStrbufIgnCaseCompS ( value , "INVALID" ) != 0 &&
2021-06-15 18:12:33 +02:00
ffStrbufIgnCaseCompS ( value , "Type1ProductConfigId" ) != 0 &&
2021-06-16 10:50:16 +02:00
ffStrbufIgnCaseCompS ( value , "All Series" ) != 0
2021-04-23 20:59:13 +02:00
;
}
2021-06-16 10:50:16 +02:00
static bool getHostValueSpecific ( FFstrbuf * basePath , const char * key , FFstrbuf * buffer )
{
uint32_t basePathLength = basePath -> length ;
ffStrbufAppendS ( basePath , "product_" );
ffStrbufAppendS ( basePath , key );
ffGetFileContent ( basePath -> chars , buffer );
ffStrbufSubstrBefore ( basePath , basePathLength );
return hostValueSet ( buffer );
}
static bool getHostValue ( FFstrbuf * devicesPath , FFstrbuf * classPath , const char * key , FFstrbuf * buffer )
{
if ( getHostValueSpecific ( devicesPath , key , buffer ))
return true ;
if ( getHostValueSpecific ( classPath , key , buffer ))
return true ;
return false ;
}
static void destroyStrbufs ( FFstrbuf * devicesPath , FFstrbuf * classPath , FFstrbuf * family , FFstrbuf * name , FFstrbuf * version )
{
ffStrbufDestroy ( devicesPath );
ffStrbufDestroy ( classPath );
ffStrbufDestroy ( family );
ffStrbufDestroy ( name );
ffStrbufDestroy ( version );
}
2021-02-27 18:30:05 +01:00
void ffPrintHost ( FFinstance * instance )
2021-02-18 22:25:36 +01:00
{
2021-04-15 18:59:26 +02:00
if ( ffPrintFromCache ( instance , FF_HOST_MODULE_NAME , & instance -> config . hostKey , & instance -> config . hostFormat , FF_HOST_NUM_FORMAT_ARGS ))
2021-02-22 16:52:51 +01:00
return ;
2021-06-16 10:50:16 +02:00
FFstrbuf devicesPath ;
ffStrbufInitA ( & devicesPath , 128 );
ffStrbufAppendS ( & devicesPath , "/sys/devices/virtual/dmi/id/" );
2021-03-12 12:16:41 +01:00
2021-06-16 10:50:16 +02:00
FFstrbuf classPath ;
ffStrbufInitA ( & classPath , 128 );
ffStrbufAppendS ( & classPath , "/sys/class/dmi/id/" );
2021-03-27 18:14:19 +01:00
2021-06-16 10:50:16 +02:00
FFstrbuf family ;
ffStrbufInit ( & family );
bool familySet = getHostValue ( & devicesPath , & classPath , "family" , & family );
FFstrbuf name ;
ffStrbufInit ( & name );
bool nameSet = getHostValue ( & devicesPath , & classPath , "name" , & name );
if ( name . length == 0 )
{
ffGetFileContent ( "/sys/firmware/devicetree/base/model" , & name );
nameSet = hostValueSet ( & name );
}
if ( name . length == 0 )
{
ffGetFileContent ( "/tmp/sysinfo/model" , & name );
nameSet = hostValueSet ( & name );
}
if ( ffStrbufStartsWithS ( & name , "Standard PC" ))
{
FFstrbuf copy ;
ffStrbufInitCopy ( & copy , & name );
ffStrbufSetS ( & name , "KVM/QEMU " );
ffStrbufAppend ( & name , & copy );
ffStrbufDestroy ( & copy );
}
FFstrbuf version ;
ffStrbufInit ( & version );
bool versionSet = getHostValue ( & devicesPath , & classPath , "version" , & version );
2021-03-12 12:16:41 +01:00
2021-05-14 16:03:19 +02:00
if ( ! familySet && ! nameSet )
2021-04-03 21:51:50 +02:00
{
2021-06-16 10:50:16 +02:00
destroyStrbufs ( & devicesPath , & classPath , & family , & name , & version );
2021-04-23 20:59:13 +02:00
ffPrintError ( instance , FF_HOST_MODULE_NAME , 0 , & instance -> config . hostKey , & instance -> config . hostFormat , FF_HOST_NUM_FORMAT_ARGS , "neither family nor name is set by O.E.M." );
2021-04-03 21:51:50 +02:00
return ;
}
2021-06-16 10:50:16 +02:00
FFstrbuf host ;
ffStrbufInit ( & host );
2021-03-19 20:57:07 +01:00
2021-05-14 16:03:19 +02:00
if ( nameSet )
2021-04-15 18:59:26 +02:00
ffStrbufAppend ( & host , & name );
2021-03-23 19:48:52 +01:00
else
2021-04-15 18:59:26 +02:00
ffStrbufAppend ( & host , & family );
2021-06-16 10:50:16 +02:00
if ( versionSet )
{
2021-04-15 18:59:26 +02:00
ffStrbufAppendC ( & host , ' ' );
ffStrbufAppend ( & host , & version );
2021-03-23 19:48:52 +01:00
}
2021-02-22 16:52:51 +01:00
2021-04-15 18:59:26 +02:00
ffPrintAndSaveToCache ( instance , FF_HOST_MODULE_NAME , & instance -> config . hostKey , & host , & instance -> config . hostFormat , FF_HOST_NUM_FORMAT_ARGS , ( FFformatarg []) {
{ FF_FORMAT_ARG_TYPE_STRBUF , & family },
{ FF_FORMAT_ARG_TYPE_STRBUF , & name },
{ FF_FORMAT_ARG_TYPE_STRBUF , & version }
});
2021-03-21 16:52:17 +01:00
2021-06-16 10:50:16 +02:00
destroyStrbufs ( & devicesPath , & classPath , & family , & name , & version );
2021-03-27 18:14:19 +01:00
}