Skip to content

Commit 8b15383

Browse files
Fallback to escape sequences if ioctl failed
1 parent adc92b5 commit 8b15383

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

src/common/io.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "fastfetch.h"
22

3+
#include <stdio.h>
34
#include <malloc.h>
45
#include <unistd.h>
56
#include <fcntl.h>
7+
#include <stdarg.h>
8+
#include <termios.h>
69

710
bool ffWriteFDContent(int fd, const FFstrbuf* content)
811
{
@@ -98,3 +101,33 @@ bool ffFileExists(const char* fileName, mode_t mode)
98101
struct stat fileStat;
99102
return stat(fileName, &fileStat) == 0 && ((fileStat.st_mode & S_IFMT) == mode);
100103
}
104+
105+
void ffGetTerminalResponse(const char* request, char end, const char* format, ...)
106+
{
107+
struct termios oldTerm, newTerm;
108+
tcgetattr(STDIN_FILENO, &oldTerm);
109+
110+
newTerm = oldTerm;
111+
newTerm.c_lflag &= (tcflag_t) ~(ICANON | ECHO);
112+
tcsetattr(STDIN_FILENO, TCSANOW, &newTerm);
113+
114+
fputs(request, stdout);
115+
116+
char buffer[512];
117+
int pos = 0;
118+
while((size_t) pos < sizeof(buffer) - 1)
119+
{
120+
char c = (char) getc(stdin);
121+
buffer[pos++] = c;
122+
if(c == end)
123+
break;
124+
}
125+
buffer[pos] = '\0';
126+
127+
tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm);
128+
129+
va_list args;
130+
va_start(args, format);
131+
vsscanf(buffer, format, args);
132+
va_end(args);
133+
}

src/fastfetch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ void ffWriteFileContent(const char* fileName, const FFstrbuf* buffer);
405405
bool ffFileExists(const char* fileName, mode_t mode);
406406
void ffSuppressIO(bool suppress); // Not thread safe!
407407

408+
void ffGetTerminalResponse(const char* request, char end, const char* format, ...);
409+
408410
//common/printing.c
409411
void ffPrintError(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numFormatArgs, const char* message, ...);
410412
void ffPrintFormatString(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, const FFstrbuf* error, uint32_t numArgs, const FFformatarg* arguments);

src/logo/image/image.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@
44

55
#define FF_KITTY_MAX_CHUNK_SIZE 4096
66

7+
#include <sys/ioctl.h>
8+
#include <unistd.h>
9+
#include <string.h> // memset
10+
11+
static bool getTermInfo(struct winsize* winsize)
12+
{
13+
//Initialize every member to 0, because it isn't guaranteed that every terminal sets them all
14+
memset(winsize, 0, sizeof(struct winsize));
15+
16+
ioctl(STDOUT_FILENO, TIOCGWINSZ, winsize);
17+
18+
if(winsize->ws_row == 0 || winsize->ws_col == 0)
19+
ffGetTerminalResponse("\033[18t", 't', "\033[8;%hu;%hut", &winsize->ws_row, &winsize->ws_col);
20+
21+
if(winsize->ws_ypixel == 0 || winsize->ws_xpixel == 0)
22+
ffGetTerminalResponse("\033[14t", 't', "\033[4;%hu;%hut", &winsize->ws_ypixel, &winsize->ws_xpixel);
23+
24+
return
25+
winsize->ws_row > 0 &&
26+
winsize->ws_col > 0 &&
27+
winsize->ws_ypixel > 0 &&
28+
winsize->ws_xpixel > 0;
29+
}
30+
731
#ifdef FF_HAVE_ZLIB
832
#include <zlib.h>
933

@@ -129,7 +153,7 @@ static bool printImageSixel(ImageInfo* imageInfo, Image* image, ExceptionInfo* e
129153
FFLogoImageResult ffLogoPrintImageImpl(FFinstance* instance, void* imageMagick, FFLogoIMResizeFunc resizeFunc, FFLogoIMWriteFunc writeFunc, FFLogoType type)
130154
{
131155
struct winsize winsize;
132-
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != 0)
156+
if(!getTermInfo(&winsize))
133157
return FF_LOGO_IMAGE_RESULT_RUN_ERROR;
134158

135159
FF_LIBRARY_LOAD_SYMBOL(imageMagick, AcquireExceptionInfo, FF_LOGO_IMAGE_RESULT_INIT_ERROR)

src/logo/image/image.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

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

10-
#include <sys/ioctl.h>
11-
#include <unistd.h>
12-
1310
#define MAGICKCORE_HDRI_ENABLE 1
1411
#define MAGICKCORE_QUANTUM_DEPTH 16
1512

0 commit comments

Comments
 (0)