Better automatic logo type detection

This commit is contained in:
Linus Dierheimer
2022-04-29 21:44:32 +02:00
parent 964a5aa8b4
commit cc1e876faf
6 changed files with 57 additions and 39 deletions
+2 -2
View File
@@ -147,8 +147,8 @@ void ffGetTerminalResponse(const char* request, const char* format, ...)
pfd.events = POLLIN;
pfd.revents = 0;
//Give the terminal 20ms to respond
if(poll(&pfd, 1, 20) <= 0)
//Give the terminal 35ms to respond
if(poll(&pfd, 1, 35) <= 0)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm);
return;
+1 -1
View File
@@ -4,7 +4,7 @@
#include <pthread.h>
#define FF_DBUS_MPRIS_PREFIX "org.mpris.MediaPlayer2."
#define FF_DBUS_TIMEOUT_MILLISECONDS 20
#define FF_DBUS_TIMEOUT_MILLISECONDS 35
#ifdef FF_HAVE_DBUS
#include <dbus/dbus.h>
+9 -10
View File
@@ -427,25 +427,24 @@ static bool getTermInfo(struct winsize* winsize)
winsize->ws_xpixel > 0;
}
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
{
//Performance optimization
#ifndef FF_HAVE_CHAFA
if(type == FF_LOGO_TYPE_CHAFA)
return false;
return FF_LOGO_IMAGE_RESULT_INIT_ERROR;
#endif
FFLogoRequestData requestData;
if(!getTermInfo(&requestData.winsize))
return false;
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
requestData.type = type;
requestData.characterPixelWidth = requestData.winsize.ws_xpixel / (double) requestData.winsize.ws_col;
requestData.characterPixelHeight = requestData.winsize.ws_ypixel / (double) requestData.winsize.ws_row;
requestData.imagePixelWidth = (uint32_t) (instance->config.logoWidth * requestData.characterPixelWidth);
if(requestData.imagePixelWidth < 1)
return false;
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
ffStrbufInitA(&requestData.cacheDir, PATH_MAX * 2);
ffStrbufAppend(&requestData.cacheDir, &instance->state.cacheDir);
@@ -456,7 +455,7 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
{
//We can safely return here, because if realpath failed, we surely won't be able to read the file
ffStrbufDestroy(&requestData.cacheDir);
return false;
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
}
ffStrbufRecalculateLength(&requestData.cacheDir);
if(!ffStrbufEndsWithC(&requestData.cacheDir, '/'))
@@ -468,7 +467,7 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
if(!instance->config.recache && printCached(instance, &requestData))
{
ffStrbufDestroy(&requestData.cacheDir);
return true;
return FF_LOGO_IMAGE_RESULT_SUCCESS;
}
FFLogoImageResult result = FF_LOGO_IMAGE_RESULT_INIT_ERROR;
@@ -483,13 +482,13 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
#endif
ffStrbufDestroy(&requestData.cacheDir);
return result == FF_LOGO_IMAGE_RESULT_SUCCESS;
return result;
}
#else //FF_HAVE_IMAGEMAGICK{6, 7}
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
{
FF_UNUSED(instance, type);
return false;
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
}
#endif //FF_HAVE_IMAGEMAGICK{6, 7}
+1 -8
View File
@@ -3,7 +3,7 @@
#ifndef FF_INCLUDED_LOGO_SIXEL_sixel
#define FF_INCLUDED_LOGO_SIXEL_sixel
#include "fastfetch.h"
#include "../logo.h"
#if defined(FF_HAVE_IMAGEMAGICK7) || defined(FF_HAVE_IMAGEMAGICK6)
@@ -12,13 +12,6 @@
#define MAGICKCORE_HDRI_ENABLE 1
#define MAGICKCORE_QUANTUM_DEPTH 16
typedef enum FFLogoImageResult
{
FF_LOGO_IMAGE_RESULT_SUCCESS, //Logo printed
FF_LOGO_IMAGE_RESULT_INIT_ERROR, //Failed to load library, try again with next IM version
FF_LOGO_IMAGE_RESULT_RUN_ERROR //Failed to load / convert image, cancle whole sixel code
} FFLogoImageResult;
typedef struct FFLogoRequestData
{
FFLogoType type;
+36 -17
View File
@@ -158,17 +158,40 @@ static void logoPrintFile(FFinstance* instance, bool doColorReplacement)
static void logoPrintDetected(FFinstance* instance)
{
if(instance->config.logoSource.length > 0)
{
if(
!ffLogoPrintBuiltinIfExists(instance) &&
!ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_KITTY) &&
!ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_CHAFA)
) logoPrintFile(instance, true);
//If no logo source is given, detect the logo from OS and print the right builtin one
if(instance->config.logoSource.length == 0)
ffLogoPrintBuiltinDetected(instance);
//If a logo source is given, first look if it is the name of a builtin logo
if(ffLogoPrintBuiltinIfExists(instance))
return;
const FFTerminalShellResult* terminalShell = ffDetectTerminalShell(instance);
FFLogoImageResult imageResult = FF_LOGO_IMAGE_RESULT_INIT_ERROR;
//Terminals that are known to support kitty graphics protocol
//Try to load the logo as image.
if(
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "kitty") == 0 ||
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "konsole") == 0 ||
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "wezterm") == 0 ||
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "wayst") == 0
) {
imageResult = ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_KITTY);
if(imageResult == FF_LOGO_IMAGE_RESULT_SUCCESS)
return;
}
ffLogoPrintBuiltinDetected(instance);
//If the logo can be loaded as an image, convert it to ascii art.
//This happens in all terminals, that don't support kitty graphics protocol
if(
imageResult != FF_LOGO_IMAGE_RESULT_RUN_ERROR &&
ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_CHAFA) == FF_LOGO_IMAGE_RESULT_SUCCESS
) return;
//Print the content of the file as logo
logoPrintFile(instance, true);
}
void ffPrintLogo(FFinstance* instance)
@@ -176,14 +199,10 @@ void ffPrintLogo(FFinstance* instance)
if(instance->config.mainColor.length == 0)
ffLogoSetMainColor(instance);
if(( //Logo type needs set logo name, but nothing was given. Print question mark
instance->config.logoType == FF_LOGO_TYPE_BUILTIN ||
instance->config.logoType == FF_LOGO_TYPE_FILE ||
instance->config.logoType == FF_LOGO_TYPE_RAW ||
instance->config.logoType == FF_LOGO_TYPE_SIXEL ||
instance->config.logoType == FF_LOGO_TYPE_KITTY ||
instance->config.logoType == FF_LOGO_TYPE_CHAFA
) && instance->config.logoSource.length == 0)
if( //Logo type needs set logo name, but nothing was given. Print question mark
instance->config.logoType != FF_LOGO_TYPE_AUTO &&
instance->config.logoSource.length == 0
)
ffLogoPrintUnknown(instance);
else if(instance->config.logoType == FF_LOGO_TYPE_BUILTIN)
{
@@ -196,7 +215,7 @@ void ffPrintLogo(FFinstance* instance)
logoPrintFile(instance, false);
else if(instance->config.logoType == FF_LOGO_TYPE_SIXEL || instance->config.logoType == FF_LOGO_TYPE_KITTY || instance->config.logoType == FF_LOGO_TYPE_CHAFA)
{
if(!ffLogoPrintImageIfExists(instance, instance->config.logoType))
if(ffLogoPrintImageIfExists(instance, instance->config.logoType) != FF_LOGO_IMAGE_RESULT_SUCCESS)
ffLogoPrintBuiltinDetected(instance);
}
else
+8 -1
View File
@@ -15,7 +15,14 @@ void ffLogoPrintUnknown(FFinstance* instance);
bool ffLogoPrintBuiltinIfExists(FFinstance* instance);
void ffLogoPrintBuiltinDetected(FFinstance* instance);
typedef enum FFLogoImageResult
{
FF_LOGO_IMAGE_RESULT_SUCCESS, //Logo printed
FF_LOGO_IMAGE_RESULT_INIT_ERROR, //Failed to load library, try again with next IM version
FF_LOGO_IMAGE_RESULT_RUN_ERROR //Failed to load / convert image, cancle whole sixel code
} FFLogoImageResult;
//image/image.c
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type);
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type);
#endif