2021-03-11 17:21:18 +01:00
#include "fastfetch.h"
2021-04-15 18:59:26 +02:00
#define FF_TERMFONT_MODULE_NAME "Terminal Font"
2021-05-23 19:40:34 +02:00
#define FF_TERMFONT_NUM_FORMAT_ARGS 5
2021-04-15 18:59:26 +02:00
2021-05-23 19:40:34 +02:00
static void printTerminalFont ( FFinstance * instance , const char * raw , FFfont * font )
2021-03-19 20:57:07 +01:00
{
2021-05-23 19:40:34 +02:00
if ( font -> pretty . length == 0 )
{
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Terminal font is an empty value" );
2021-05-23 19:40:34 +02:00
return ;
}
2021-04-05 19:25:34 +02:00
2022-06-17 18:15:36 +02:00
if ( instance -> config . terminalFont . outputFormat . length == 0 )
2021-03-19 20:57:07 +01:00
{
2022-06-17 18:15:36 +02:00
ffPrintLogoAndKey ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont . key );
2021-05-23 19:40:34 +02:00
ffStrbufPutTo ( & font -> pretty , stdout );
2021-03-19 20:57:07 +01:00
}
else
{
2022-06-17 18:15:36 +02:00
ffPrintFormat ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , FF_TERMFONT_NUM_FORMAT_ARGS , ( FFformatarg []){
2021-05-23 19:40:34 +02:00
{ FF_FORMAT_ARG_TYPE_STRING , raw },
{ FF_FORMAT_ARG_TYPE_STRBUF , & font -> name },
{ FF_FORMAT_ARG_TYPE_STRBUF , & font -> size },
{ FF_FORMAT_ARG_TYPE_LIST , & font -> styles },
{ FF_FORMAT_ARG_TYPE_STRBUF , & font -> pretty }
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
}
2021-05-20 20:06:08 +02:00
static const char * getSystemMonospaceFont ( FFinstance * instance )
{
2022-01-02 16:55:26 +01:00
const FFDisplayServerResult * wmde = ffConnectDisplayServer ( instance );
2021-05-20 20:06:08 +02:00
if ( ffStrbufIgnCaseCompS ( & wmde -> dePrettyName , "Cinnamon" ) == 0 )
{
const char * systemMonospaceFont = ffSettingsGet ( instance , "/org/cinnamon/desktop/interface/monospace-font-name" , "org.cinnamon.desktop.interface" , NULL , "monospace-font-name" , FF_VARIANT_TYPE_STRING ). strValue ;
if ( systemMonospaceFont != NULL )
return systemMonospaceFont ;
}
else if ( ffStrbufIgnCaseCompS ( & wmde -> dePrettyName , "Mate" ) == 0 )
{
const char * systemMonospaceFont = ffSettingsGet ( instance , "/org/mate/interface/monospace-font-name" , "org.mate.interface" , NULL , "monospace-font-name" , FF_VARIANT_TYPE_STRING ). strValue ;
if ( systemMonospaceFont != NULL )
return systemMonospaceFont ;
}
return ffSettingsGet ( instance , "/org/gnome/desktop/interface/monospace-font-name" , "org.gnome.desktop.interface" , NULL , "monospace-font-name" , FF_VARIANT_TYPE_STRING ). strValue ;
}
2021-05-13 12:27:44 +02:00
static void printTerminalFontFromConfigFile ( FFinstance * instance , const char * configFile , const char * start )
2021-05-09 12:30:10 +02:00
{
2021-05-23 19:40:34 +02:00
FFstrbuf fontName ;
ffStrbufInit ( & fontName );
ffParsePropFileConfig ( instance , configFile , start , & fontName );
if ( fontName . length == 0 )
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't find terminal font in \" $XDG_CONFIG_HOME/%s \" " , configFile );
2021-05-13 12:27:44 +02:00
else
2021-05-23 19:40:34 +02:00
{
FFfont font ;
ffFontInitPango ( & font , fontName . chars );
printTerminalFont ( instance , fontName . chars , & font );
ffFontDestroy ( & font );
}
2021-05-09 12:30:10 +02:00
2021-05-23 19:40:34 +02:00
ffStrbufDestroy ( & fontName );
2021-05-09 12:30:10 +02:00
}
2021-05-20 20:06:08 +02:00
static void printTerminalFontFromGSettings ( FFinstance * instance , char * profilePath , char * profileList , char * profile )
{
const char * defaultProfile = ffSettingsGetGSettings ( instance , profileList , NULL , "default" , FF_VARIANT_TYPE_STRING ). strValue ;
if ( defaultProfile == NULL )
{
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't get \" default \" profile from gsettings" );
2021-05-20 20:06:08 +02:00
return ;
}
FFstrbuf path ;
ffStrbufInitA ( & path , 128 );
ffStrbufAppendS ( & path , profilePath );
ffStrbufAppendS ( & path , defaultProfile );
ffStrbufAppendC ( & path , '/' );
const char * fontName ;
if ( ! ffSettingsGetGSettings ( instance , profile , path . chars , "use-system-font" , FF_VARIANT_TYPE_BOOL ). boolValue ) // custom font
{
fontName = ffSettingsGetGSettings ( instance , profile , path . chars , "font" , FF_VARIANT_TYPE_STRING ). strValue ;
if ( fontName == NULL )
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't get terminal font from GSettings (%s::%s::font)" , profile , path . chars );
2021-05-20 20:06:08 +02:00
}
else // system font
{
fontName = getSystemMonospaceFont ( instance );
if ( fontName == NULL )
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Could't get system monospace font name from GSettings / DConf" );
2021-05-20 20:06:08 +02:00
}
ffStrbufDestroy ( & path );
if ( fontName == NULL )
return ;
2021-05-23 19:40:34 +02:00
FFfont font ;
ffFontInitPango ( & font , fontName );
printTerminalFont ( instance , fontName , & font );
ffFontDestroy ( & font );
2021-05-20 20:06:08 +02:00
}
2021-03-11 17:21:18 +01:00
static void printKonsole ( FFinstance * instance )
{
2021-05-13 12:27:44 +02:00
FFstrbuf profile ;
ffStrbufInit ( & profile );
2021-05-15 14:58:43 +02:00
ffParsePropFileConfig ( instance , "konsolerc" , "DefaultProfile =" , & profile );
2021-05-13 12:27:44 +02:00
if ( profile . length == 0 )
2021-03-11 17:21:18 +01:00
{
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't find \" DefaultProfile=%[^ \\ n] \" in \" .config/konsolerc \" " );
2021-05-13 12:27:44 +02:00
ffStrbufDestroy ( & profile );
2021-03-11 17:21:18 +01:00
return ;
}
2021-05-13 12:27:44 +02:00
FFstrbuf profilePath ;
ffStrbufInitA ( & profilePath , 64 );
ffStrbufAppendS ( & profilePath , ".local/share/konsole/" );
ffStrbufAppend ( & profilePath , & profile );
2021-03-11 17:21:18 +01:00
2021-05-23 19:40:34 +02:00
FFstrbuf fontName ;
ffStrbufInit ( & fontName );
ffParsePropFileHome ( instance , profilePath . chars , "Font =" , & fontName );
2021-03-11 17:21:18 +01:00
2021-05-23 19:40:34 +02:00
if ( fontName . length == 0 )
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't find \" Font=%%[^ \\ n] \" in \" %s \" " , profilePath . chars );
2021-05-13 12:27:44 +02:00
else
2021-05-23 19:40:34 +02:00
{
FFfont font ;
ffFontInitQt ( & font , fontName . chars );
printTerminalFont ( instance , fontName . chars , & font );
ffFontDestroy ( & font );
}
2021-05-13 12:27:44 +02:00
2021-05-23 19:40:34 +02:00
ffStrbufDestroy ( & fontName );
2021-05-13 12:27:44 +02:00
ffStrbufDestroy ( & profilePath );
ffStrbufDestroy ( & profile );
2021-03-11 17:21:18 +01:00
}
2021-05-11 22:28:10 +02:00
static void printXCFETerminal ( FFinstance * instance )
{
2021-05-13 12:27:44 +02:00
FFstrbuf useSysFont ;
ffStrbufInit ( & useSysFont );
2021-05-11 22:28:10 +02:00
2021-05-15 14:58:43 +02:00
if ( ! ffParsePropFileConfig ( instance , "xfce4/terminal/terminalrc" , "FontUseSystem =" , & useSysFont ))
2021-05-11 22:28:10 +02:00
{
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't open \" $XDG_CONFIG_HOME/xfce4/terminal/terminalrc \" " );
2021-05-13 12:27:44 +02:00
ffStrbufDestroy ( & useSysFont );
2021-05-11 22:28:10 +02:00
return ;
}
2021-05-13 12:27:44 +02:00
if ( useSysFont . length == 0 || ffStrbufIgnCaseCompS ( & useSysFont , "FALSE" ) == 0 )
2021-05-11 22:28:10 +02:00
{
2021-05-15 14:58:43 +02:00
printTerminalFontFromConfigFile ( instance , "xfce4/terminal/terminalrc" , "FontName =" );
2021-05-13 12:27:44 +02:00
ffStrbufDestroy ( & useSysFont );
2021-05-11 22:28:10 +02:00
return ;
}
2021-05-12 20:13:56 +02:00
2021-05-13 12:27:44 +02:00
ffStrbufDestroy ( & useSysFont );
2021-05-12 20:13:56 +02:00
const char * fontName = ffSettingsGetXFConf ( instance , "xsettings" , "/Gtk/MonospaceFontName" , FF_VARIANT_TYPE_STRING ). strValue ;
2021-05-11 22:28:10 +02:00
if ( fontName == NULL )
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Couldn't find \" xsettings::/Gtk/MonospaceFontName \" in XFConf" );
2021-05-13 12:27:44 +02:00
else
2021-05-23 19:40:34 +02:00
{
FFfont font ;
ffFontInitPango ( & font , fontName );
printTerminalFont ( instance , fontName , & font );
ffFontDestroy ( & font );
}
2021-05-11 22:28:10 +02:00
}
2022-07-12 14:38:06 +02:00
static void printAlacritty ( FFinstance * instance ) {
FFstrbuf fontName ;
ffStrbufInit ( & fontName );
// alacritty parses config files in this order
ffParsePropFileConfig ( instance , "alacritty/alacritty.yml" , "family:" , & fontName );
if ( fontName . length == 0 )
ffParsePropFileConfig ( instance , "alacritty.yml" , "family:" , & fontName );
if ( fontName . length == 0 )
ffParsePropFileConfig ( instance , ".alacritty.yml" , "family:" , & fontName );
if ( fontName . length == 0 )
ffStrbufAppendS ( & fontName , "alacritty" ); //by default alacritty uses it's own font called alacritty
FFfont font ;
ffFontInitPango ( & font , fontName . chars );
printTerminalFont ( instance , fontName . chars , & font );
ffFontDestroy ( & font );
ffStrbufDestroy ( & fontName );
}
2021-03-11 17:21:18 +01:00
static void printTTY ( FFinstance * instance )
{
2021-05-23 19:40:34 +02:00
FFstrbuf fontName ;
ffStrbufInit ( & fontName );
2021-03-27 18:14:19 +01:00
2022-04-10 15:07:52 +02:00
ffParsePropFile ( FASTFETCH_TARGET_DIR_ROOT "/etc/vconsole.conf" , "Font =" , & fontName );
2021-05-01 16:54:59 +02:00
2021-05-23 19:40:34 +02:00
if ( fontName . length == 0 )
2021-05-01 16:54:59 +02:00
{
2021-05-23 19:40:34 +02:00
ffStrbufAppendS ( & fontName , "VGA default kernel font " );
ffProcessAppendStdOut ( & fontName , ( char * const []){
2021-05-01 16:54:59 +02:00
"showconsolefont" ,
"--info" ,
NULL
});
}
2021-05-23 19:40:34 +02:00
ffStrbufTrimRight ( & fontName , ' ' );
FFfont font ;
ffFontInitCopy ( & font , fontName . chars );
printTerminalFont ( instance , fontName . chars , & font );
ffFontDestroy ( & font );
ffStrbufDestroy ( & fontName );
2021-03-11 17:21:18 +01:00
}
void ffPrintTerminalFont ( FFinstance * instance )
{
2021-06-14 23:22:14 +02:00
const FFTerminalShellResult * result = ffDetectTerminalShell ( instance );
2021-03-27 18:14:19 +01:00
2021-06-14 23:22:14 +02:00
if ( result -> terminalProcessName . length == 0 )
2021-03-11 17:21:18 +01:00
{
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Terminal font needs successfull terminal detection" );
2021-03-19 20:57:07 +01:00
return ;
2021-03-11 17:21:18 +01:00
}
2021-06-14 23:22:14 +02:00
if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "konsole" ) == 0 )
2021-03-11 17:21:18 +01:00
printKonsole ( instance );
2021-06-14 23:22:14 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "xfce4-terminal" ) == 0 )
2021-05-09 13:40:17 +02:00
printXCFETerminal ( instance );
2021-06-14 23:22:14 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "lxterminal" ) == 0 )
2021-05-15 14:58:43 +02:00
printTerminalFontFromConfigFile ( instance , "lxterminal/lxterminal.conf" , "fontname =" );
2021-06-14 23:22:14 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "tilix" ) == 0 )
2021-05-20 20:06:08 +02:00
printTerminalFontFromGSettings ( instance , "/com/gexperts/Tilix/profiles/" , "com.gexperts.Tilix.ProfilesList" , "com.gexperts.Tilix.Profile" );
2021-06-14 23:22:14 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "gnome-terminal-" ) == 0 )
2021-05-20 20:06:08 +02:00
printTerminalFontFromGSettings ( instance , "/org/gnome/terminal/legacy/profiles:/:" , "org.gnome.Terminal.ProfilesList" , "org.gnome.Terminal.Legacy.Profile" );
2021-06-15 09:21:38 +02:00
else if ( ffStrbufStartsWithIgnCaseS ( & result -> terminalExe , "/dev/tty" ))
2021-03-11 17:21:18 +01:00
printTTY ( instance );
2022-07-12 14:38:06 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> terminalProcessName , "alacritty" ) == 0 )
printAlacritty ( instance );
2021-03-11 17:21:18 +01:00
else
2022-06-17 18:15:36 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . terminalFont , "Unknown terminal: %s" , result -> terminalProcessName . chars );
2021-03-27 18:14:19 +01:00
}