ohmu
·
2026-10-15
1#include "terminalsize.h"
2#include "common/io.h"
3
4#include <sys/ioctl.h>
5#include <fcntl.h>
6#include <unistd.h>
7
8#ifdef __sun
9 #include <sys/termios.h>
10#endif
11
12bool ffDetectTerminalSize(FFTerminalSizeResult* result, bool fastOnly) {
13 *result = (FFTerminalSizeResult){};
14
15 struct winsize winsize = {};
16 static int ttyfd = STDOUT_FILENO;
17 if (!isatty(ttyfd)) {
18 ttyfd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
19 }
20
21 ioctl(ttyfd, TIOCGWINSZ, &winsize);
22
23 if (!fastOnly) {
24 if (winsize.ws_row == 0 || winsize.ws_col == 0) {
25 ffGetTerminalResponse("\e[18t", 2, "\e[8;%hu;%hut", &winsize.ws_row, &winsize.ws_col);
26 }
27
28 if (winsize.ws_ypixel == 0 || winsize.ws_xpixel == 0) {
29 ffGetTerminalResponse("\e[14t", 2, "\e[4;%hu;%hut", &winsize.ws_ypixel, &winsize.ws_xpixel);
30 }
31 }
32
33 result->rows = winsize.ws_row;
34 result->columns = winsize.ws_col;
35 result->width = winsize.ws_xpixel;
36 result->height = winsize.ws_ypixel;
37
38 return result->rows > 0 && result->columns > 0;
39}