Skip to content

Commit 21b315d

Browse files
Use a timeout when waiting for terminal responses
1 parent 14fa052 commit 21b315d

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/common/io.c

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

33
#include <stdio.h>
4-
#include <malloc.h>
54
#include <unistd.h>
65
#include <fcntl.h>
76
#include <stdarg.h>
87
#include <termios.h>
8+
#include <poll.h>
99

1010
bool ffWriteFDContent(int fd, const FFstrbuf* content)
1111
{
@@ -128,32 +128,42 @@ bool ffFileExists(const char* fileName, mode_t mode)
128128
return stat(fileName, &fileStat) == 0 && ((fileStat.st_mode & S_IFMT) == mode);
129129
}
130130

131-
void ffGetTerminalResponse(const char* request, char end, const char* format, ...)
131+
void ffGetTerminalResponse(const char* request, const char* format, ...)
132132
{
133133
struct termios oldTerm, newTerm;
134-
tcgetattr(STDIN_FILENO, &oldTerm);
134+
if(tcgetattr(STDIN_FILENO, &oldTerm) == -1)
135+
return;
135136

136137
newTerm = oldTerm;
137138
newTerm.c_lflag &= (tcflag_t) ~(ICANON | ECHO);
138-
tcsetattr(STDIN_FILENO, TCSANOW, &newTerm);
139+
if(tcsetattr(STDIN_FILENO, TCSANOW, &newTerm) == -1)
140+
return;
139141

140142
fputs(request, stdout);
143+
fflush(stdout);
141144

142-
char buffer[512];
143-
int pos = 0;
144-
while((size_t) pos < sizeof(buffer) - 1)
145+
struct pollfd pfd;
146+
pfd.fd = STDIN_FILENO;
147+
pfd.events = POLLIN;
148+
pfd.revents = 0;
149+
150+
//Give the terminal 20ms to respond
151+
if(poll(&pfd, 1, 20) <= 0)
145152
{
146-
char c = (char) getc(stdin);
147-
if(c == '\0')
148-
break;
149-
buffer[pos++] = c;
150-
if(c == end)
151-
break;
153+
tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm);
154+
return;
152155
}
153-
buffer[pos] = '\0';
156+
157+
char buffer[512];
158+
ssize_t readed = read(STDIN_FILENO, buffer, sizeof(buffer) - 1);
154159

155160
tcsetattr(STDIN_FILENO, TCSANOW, &oldTerm);
156161

162+
if(readed <= 0)
163+
return;
164+
165+
buffer[readed] = '\0';
166+
157167
va_list args;
158168
va_start(args, format);
159169
vsscanf(buffer, format, args);

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 50
7+
#define FF_DBUS_TIMEOUT_MILLISECONDS 20
88

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

src/fastfetch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ bool 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, ...);
408+
void ffGetTerminalResponse(const char* request, const char* format, ...);
409409

410410
//common/printing.c
411411
void ffPrintError(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numFormatArgs, const char* message, ...);

src/logo/image/image.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ static bool getTermInfo(struct winsize* winsize)
330330
ioctl(STDOUT_FILENO, TIOCGWINSZ, winsize);
331331

332332
if(winsize->ws_row == 0 || winsize->ws_col == 0)
333-
ffGetTerminalResponse("\033[18t", 't', "\033[8;%hu;%hut", &winsize->ws_row, &winsize->ws_col);
333+
ffGetTerminalResponse("\033[18t", "\033[8;%hu;%hut", &winsize->ws_row, &winsize->ws_col);
334334

335335
if(winsize->ws_ypixel == 0 || winsize->ws_xpixel == 0)
336-
ffGetTerminalResponse("\033[14t", 't', "\033[4;%hu;%hut", &winsize->ws_ypixel, &winsize->ws_xpixel);
336+
ffGetTerminalResponse("\033[14t", "\033[4;%hu;%hut", &winsize->ws_ypixel, &winsize->ws_xpixel);
337337

338338
return
339339
winsize->ws_row > 0 &&

0 commit comments

Comments
 (0)