2021-04-03 23:10:14 +02:00
#include "fastfetch.h"
2021-04-26 17:27:12 +02:00
#include <string.h>
2021-04-03 23:10:14 +02:00
2021-04-15 18:59:26 +02:00
#define FF_WMTHEME_MODULE_NAME "WM Theme"
#define FF_WMTHEME_NUM_FORMAT_ARGS 1
2021-04-03 23:10:14 +02:00
static void printWMTheme ( FFinstance * instance , const char * theme )
{
if ( instance -> config . wmThemeFormat . length == 0 )
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey );
2021-04-03 23:10:14 +02:00
puts ( theme );
}
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey , & instance -> config . wmThemeFormat , NULL , FF_WMTHEME_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_STRING , theme }
});
2021-04-03 23:10:14 +02:00
}
}
static void printKWin ( FFinstance * instance )
{
char theme [ 256 ];
theme [ 0 ] = '\0' ;
ffParsePropFileHome ( instance , ".config/kwinrc" , "theme=%s" , theme );
if ( theme [ 0 ] == '\0' )
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey , & instance -> config . wmThemeFormat , FF_WMTHEME_NUM_FORMAT_ARGS , "Couldn't find \" theme= \" in \" .config/kwinrc \" " );
2021-04-03 23:10:14 +02:00
return ;
}
printWMTheme ( instance , theme );
}
2021-04-26 17:27:12 +02:00
static void printOpenbox ( FFinstance * instance )
2021-04-25 21:18:56 +02:00
{
2021-04-26 17:27:12 +02:00
FFstrbuf absolutePath , theme ;
2021-04-25 21:18:56 +02:00
ffStrbufInitA ( & absolutePath , 64 );
2021-04-26 17:27:12 +02:00
ffStrbufInitA ( & theme , 256 );
2021-04-25 21:18:56 +02:00
ffStrbufAppendS ( & absolutePath , instance -> state . passwd -> pw_dir );
2021-04-26 17:27:12 +02:00
ffStrbufAppendC ( & absolutePath , '/' );
const char * deName = ffGetSessionDesktop ();
if ( strcmp ( deName , ( const char * ) "LXQt" ) == 0 )
ffStrbufAppendS ( & absolutePath , ".config/openbox/lxqt-rc.xml" );
else if ( strcmp ( deName , ( const char * ) "LXDE" ) == 0 )
ffStrbufAppendS ( & absolutePath , ".config/openbox/lxde-rc.xml" );
else
ffStrbufAppendS ( & absolutePath , ".config/openbox/rc.xml" );
2021-04-25 21:18:56 +02:00
char * line = NULL ;
size_t len = 0 ;
FILE * file = fopen ( absolutePath . chars , "r" );
if ( file == NULL )
return ; // handle errors in higher functions
2021-04-26 17:27:12 +02:00
while ( getline ( & line , & len , file ) != - 1 )
2021-04-25 21:18:56 +02:00
{
2021-04-26 17:27:12 +02:00
if ( strstr ( line , "<theme>" ) != 0 )
2021-04-25 21:18:56 +02:00
break ;
}
2021-04-26 17:27:12 +02:00
while ( getline ( & line , & len , file ) != - 1 )
2021-04-25 21:18:56 +02:00
{
2021-04-26 17:27:12 +02:00
if ( strstr ( line , "<name>" ) != 0 )
2021-04-25 21:18:56 +02:00
{
const char * delStrs [] = { "<name>" , "</name>" };
2021-04-26 17:27:12 +02:00
ffStrbufAppendS ( & theme , line );
ffStrbufRemoveStringsA ( & theme , 2 , delStrs );
ffStrbufTrimRight ( & theme , '\n' );
ffStrbufTrim ( & theme , ' ' );
2021-04-25 21:18:56 +02:00
}
2021-04-26 17:27:12 +02:00
else if ( strstr ( line , "</theme>" ) != 0 ) // sanity check
2021-04-25 21:18:56 +02:00
break ;
break ;
}
fclose ( file );
2021-04-26 17:27:12 +02:00
if ( line != NULL ) {
2021-04-25 21:18:56 +02:00
free ( line );
2021-04-26 17:27:12 +02:00
}
if ( theme . length == 0 )
2021-04-25 21:18:56 +02:00
{
2021-04-26 17:27:12 +02:00
ffPrintError ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey , & instance -> config . wmThemeFormat , FF_WMTHEME_NUM_FORMAT_ARGS , "Couldn't find theme name in \" %s \" " , absolutePath . chars );
ffStrbufDestroy ( & absolutePath );
ffStrbufDestroy ( & theme );
2021-04-25 21:18:56 +02:00
return ;
}
2021-04-26 17:27:12 +02:00
printWMTheme ( instance , theme . chars );
2021-04-25 21:18:56 +02:00
2021-04-26 17:27:12 +02:00
ffStrbufDestroy ( & absolutePath );
ffStrbufDestroy ( & theme );
}
2021-04-25 21:18:56 +02:00
2021-04-03 23:10:14 +02:00
void ffPrintWMTheme ( FFinstance * instance )
{
2021-04-22 21:41:42 +02:00
const FFWMResult * result = ffCalculateWM ( instance );
2021-04-03 23:10:14 +02:00
2021-04-22 21:41:42 +02:00
if ( result -> prettyName . length == 0 )
2021-04-03 23:10:14 +02:00
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey , & instance -> config . wmThemeFormat , FF_WMTHEME_NUM_FORMAT_ARGS , "WM Theme needs sucessfull WM detection" );
2021-04-03 23:10:14 +02:00
return ;
}
2021-04-22 21:41:42 +02:00
if ( ffStrbufIgnCaseCompS ( & result -> prettyName , "KWin" ) == 0 )
2021-04-03 23:10:14 +02:00
printKWin ( instance );
2021-04-25 21:18:56 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> prettyName , "Openbox" ) == 0 )
printOpenbox ( instance );
2021-04-03 23:10:14 +02:00
else
2021-04-22 21:41:42 +02:00
ffPrintError ( instance , FF_WMTHEME_MODULE_NAME , 0 , & instance -> config . wmThemeKey , & instance -> config . wmThemeFormat , FF_WMTHEME_NUM_FORMAT_ARGS , "Unknown WM: %s" , result -> prettyName . chars );
2021-04-03 23:10:14 +02:00
}