Files
fastfetch/src/detection/terminalsize/terminalsize_linux.c
T

39 lines
1009 B
C
Raw Normal View History

2023-07-26 10:27:40 +08:00
#include "terminalsize.h"
2026-01-06 09:48:38 +08:00
#include "common/io.h"
2023-07-26 10:27:40 +08:00
#include <sys/ioctl.h>
#include <fcntl.h>
2023-07-26 10:27:40 +08:00
#include <unistd.h>
2024-06-17 01:20:04 +08:00
#ifdef __sun
#include <sys/termios.h>
2024-06-17 01:20:04 +08:00
#endif
2026-03-29 09:28:49 +08:00
bool ffDetectTerminalSize(FFTerminalSizeResult* result) {
2023-07-26 10:27:40 +08:00
struct winsize winsize = {};
static int ttyfd = STDOUT_FILENO;
2026-03-29 09:28:49 +08:00
if (!isatty(ttyfd)) {
ttyfd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
2026-03-29 09:28:49 +08:00
}
ioctl(ttyfd, TIOCGWINSZ, &winsize);
2023-07-26 10:27:40 +08:00
2026-03-29 09:28:49 +08:00
if (winsize.ws_row == 0 || winsize.ws_col == 0) {
ffGetTerminalResponse("\e[18t", 2, "\e[8;%hu;%hut", &winsize.ws_row, &winsize.ws_col);
2026-03-29 09:28:49 +08:00
}
2023-07-26 10:27:40 +08:00
2026-03-29 09:28:49 +08:00
if (winsize.ws_ypixel == 0 || winsize.ws_xpixel == 0) {
ffGetTerminalResponse("\e[14t", 2, "\e[4;%hu;%hut", &winsize.ws_ypixel, &winsize.ws_xpixel);
2026-03-29 09:28:49 +08:00
}
2023-07-26 10:27:40 +08:00
2026-03-29 09:28:49 +08:00
if (winsize.ws_row == 0 && winsize.ws_col == 0) {
2023-07-26 10:27:40 +08:00
return false;
2026-03-29 09:28:49 +08:00
}
2023-07-26 10:27:40 +08:00
result->rows = winsize.ws_row;
result->columns = winsize.ws_col;
result->width = winsize.ws_xpixel;
result->height = winsize.ws_ypixel;
return true;
}