Skip to content

Commit cc1e876

Browse files
Better automatic logo type detection
1 parent 964a5aa commit cc1e876

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

src/common/io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ void ffGetTerminalResponse(const char* request, const char* format, ...)
147147
pfd.events = POLLIN;
148148
pfd.revents = 0;
149149

150-
//Give the terminal 20ms to respond
151-
if(poll(&pfd, 1, 20) <= 0)
150+
//Give the terminal 35ms to respond
151+
if(poll(&pfd, 1, 35) <= 0)
152152
{
153153
tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm);
154154
return;

src/detection/media.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <pthread.h>
55

66
#define FF_DBUS_MPRIS_PREFIX "org.mpris.MediaPlayer2."
7-
#define FF_DBUS_TIMEOUT_MILLISECONDS 20
7+
#define FF_DBUS_TIMEOUT_MILLISECONDS 35
88

99
#ifdef FF_HAVE_DBUS
1010
#include <dbus/dbus.h>

src/logo/image/image.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,24 @@ static bool getTermInfo(struct winsize* winsize)
427427
winsize->ws_xpixel > 0;
428428
}
429429

430-
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
430+
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
431431
{
432-
//Performance optimization
433432
#ifndef FF_HAVE_CHAFA
434433
if(type == FF_LOGO_TYPE_CHAFA)
435-
return false;
434+
return FF_LOGO_IMAGE_RESULT_INIT_ERROR;
436435
#endif
437436

438437
FFLogoRequestData requestData;
439438

440439
if(!getTermInfo(&requestData.winsize))
441-
return false;
440+
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
442441

443442
requestData.type = type;
444443
requestData.characterPixelWidth = requestData.winsize.ws_xpixel / (double) requestData.winsize.ws_col;
445444
requestData.characterPixelHeight = requestData.winsize.ws_ypixel / (double) requestData.winsize.ws_row;
446445
requestData.imagePixelWidth = (uint32_t) (instance->config.logoWidth * requestData.characterPixelWidth);
447446
if(requestData.imagePixelWidth < 1)
448-
return false;
447+
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
449448

450449
ffStrbufInitA(&requestData.cacheDir, PATH_MAX * 2);
451450
ffStrbufAppend(&requestData.cacheDir, &instance->state.cacheDir);
@@ -456,7 +455,7 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
456455
{
457456
//We can safely return here, because if realpath failed, we surely won't be able to read the file
458457
ffStrbufDestroy(&requestData.cacheDir);
459-
return false;
458+
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
460459
}
461460
ffStrbufRecalculateLength(&requestData.cacheDir);
462461
if(!ffStrbufEndsWithC(&requestData.cacheDir, '/'))
@@ -468,7 +467,7 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
468467
if(!instance->config.recache && printCached(instance, &requestData))
469468
{
470469
ffStrbufDestroy(&requestData.cacheDir);
471-
return true;
470+
return FF_LOGO_IMAGE_RESULT_SUCCESS;
472471
}
473472

474473
FFLogoImageResult result = FF_LOGO_IMAGE_RESULT_INIT_ERROR;
@@ -483,13 +482,13 @@ bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
483482
#endif
484483

485484
ffStrbufDestroy(&requestData.cacheDir);
486-
return result == FF_LOGO_IMAGE_RESULT_SUCCESS;
485+
return result;
487486
}
488487

489488
#else //FF_HAVE_IMAGEMAGICK{6, 7}
490-
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
489+
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type)
491490
{
492491
FF_UNUSED(instance, type);
493-
return false;
492+
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
494493
}
495494
#endif //FF_HAVE_IMAGEMAGICK{6, 7}

src/logo/image/image.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef FF_INCLUDED_LOGO_SIXEL_sixel
44
#define FF_INCLUDED_LOGO_SIXEL_sixel
55

6-
#include "fastfetch.h"
6+
#include "../logo.h"
77

88
#if defined(FF_HAVE_IMAGEMAGICK7) || defined(FF_HAVE_IMAGEMAGICK6)
99

@@ -12,13 +12,6 @@
1212
#define MAGICKCORE_HDRI_ENABLE 1
1313
#define MAGICKCORE_QUANTUM_DEPTH 16
1414

15-
typedef enum FFLogoImageResult
16-
{
17-
FF_LOGO_IMAGE_RESULT_SUCCESS, //Logo printed
18-
FF_LOGO_IMAGE_RESULT_INIT_ERROR, //Failed to load library, try again with next IM version
19-
FF_LOGO_IMAGE_RESULT_RUN_ERROR //Failed to load / convert image, cancle whole sixel code
20-
} FFLogoImageResult;
21-
2215
typedef struct FFLogoRequestData
2316
{
2417
FFLogoType type;

src/logo/logo.c

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,32 +158,51 @@ static void logoPrintFile(FFinstance* instance, bool doColorReplacement)
158158

159159
static void logoPrintDetected(FFinstance* instance)
160160
{
161-
if(instance->config.logoSource.length > 0)
162-
{
163-
if(
164-
!ffLogoPrintBuiltinIfExists(instance) &&
165-
!ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_KITTY) &&
166-
!ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_CHAFA)
167-
) logoPrintFile(instance, true);
161+
//If no logo source is given, detect the logo from OS and print the right builtin one
162+
if(instance->config.logoSource.length == 0)
163+
ffLogoPrintBuiltinDetected(instance);
164+
165+
//If a logo source is given, first look if it is the name of a builtin logo
166+
if(ffLogoPrintBuiltinIfExists(instance))
168167
return;
168+
169+
const FFTerminalShellResult* terminalShell = ffDetectTerminalShell(instance);
170+
171+
FFLogoImageResult imageResult = FF_LOGO_IMAGE_RESULT_INIT_ERROR;
172+
173+
//Terminals that are known to support kitty graphics protocol
174+
//Try to load the logo as image.
175+
if(
176+
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "kitty") == 0 ||
177+
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "konsole") == 0 ||
178+
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "wezterm") == 0 ||
179+
ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "wayst") == 0
180+
) {
181+
imageResult = ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_KITTY);
182+
if(imageResult == FF_LOGO_IMAGE_RESULT_SUCCESS)
183+
return;
169184
}
170185

171-
ffLogoPrintBuiltinDetected(instance);
186+
//If the logo can be loaded as an image, convert it to ascii art.
187+
//This happens in all terminals, that don't support kitty graphics protocol
188+
if(
189+
imageResult != FF_LOGO_IMAGE_RESULT_RUN_ERROR &&
190+
ffLogoPrintImageIfExists(instance, FF_LOGO_TYPE_CHAFA) == FF_LOGO_IMAGE_RESULT_SUCCESS
191+
) return;
192+
193+
//Print the content of the file as logo
194+
logoPrintFile(instance, true);
172195
}
173196

174197
void ffPrintLogo(FFinstance* instance)
175198
{
176199
if(instance->config.mainColor.length == 0)
177200
ffLogoSetMainColor(instance);
178201

179-
if(( //Logo type needs set logo name, but nothing was given. Print question mark
180-
instance->config.logoType == FF_LOGO_TYPE_BUILTIN ||
181-
instance->config.logoType == FF_LOGO_TYPE_FILE ||
182-
instance->config.logoType == FF_LOGO_TYPE_RAW ||
183-
instance->config.logoType == FF_LOGO_TYPE_SIXEL ||
184-
instance->config.logoType == FF_LOGO_TYPE_KITTY ||
185-
instance->config.logoType == FF_LOGO_TYPE_CHAFA
186-
) && instance->config.logoSource.length == 0)
202+
if( //Logo type needs set logo name, but nothing was given. Print question mark
203+
instance->config.logoType != FF_LOGO_TYPE_AUTO &&
204+
instance->config.logoSource.length == 0
205+
)
187206
ffLogoPrintUnknown(instance);
188207
else if(instance->config.logoType == FF_LOGO_TYPE_BUILTIN)
189208
{
@@ -196,7 +215,7 @@ void ffPrintLogo(FFinstance* instance)
196215
logoPrintFile(instance, false);
197216
else if(instance->config.logoType == FF_LOGO_TYPE_SIXEL || instance->config.logoType == FF_LOGO_TYPE_KITTY || instance->config.logoType == FF_LOGO_TYPE_CHAFA)
198217
{
199-
if(!ffLogoPrintImageIfExists(instance, instance->config.logoType))
218+
if(ffLogoPrintImageIfExists(instance, instance->config.logoType) != FF_LOGO_IMAGE_RESULT_SUCCESS)
200219
ffLogoPrintBuiltinDetected(instance);
201220
}
202221
else

src/logo/logo.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ void ffLogoPrintUnknown(FFinstance* instance);
1515
bool ffLogoPrintBuiltinIfExists(FFinstance* instance);
1616
void ffLogoPrintBuiltinDetected(FFinstance* instance);
1717

18+
typedef enum FFLogoImageResult
19+
{
20+
FF_LOGO_IMAGE_RESULT_SUCCESS, //Logo printed
21+
FF_LOGO_IMAGE_RESULT_INIT_ERROR, //Failed to load library, try again with next IM version
22+
FF_LOGO_IMAGE_RESULT_RUN_ERROR //Failed to load / convert image, cancle whole sixel code
23+
} FFLogoImageResult;
24+
1825
//image/image.c
19-
bool ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type);
26+
FFLogoImageResult ffLogoPrintImageIfExists(FFinstance* instance, FFLogoType type);
2027

2128
#endif

0 commit comments

Comments
 (0)