LibC: Implement tc{get,set}winsize

These were added in POSIX issue 8 :^)
This commit is contained in:
2026-05-15 16:51:58 +03:00
parent 05c9f0640c
commit bf2121e166
5 changed files with 31 additions and 21 deletions

View File

@@ -42,15 +42,8 @@ __BEGIN_DECLS
#define FIONREAD 40 /* get number of input bytes available */ #define FIONREAD 40 /* get number of input bytes available */
#define FIONBIO 41 #define FIONBIO 41
struct winsize #define TIOCGWINSZ 50
{ #define TIOCSWINSZ 51
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* unused by kernel */
unsigned short ws_ypixel; /* unused by kernel */
};
#define TIOCGWINSZ 50
#define TIOCSWINSZ 51
#define TCGETS 52 #define TCGETS 52
#define TCSETS 53 #define TCSETS 53
#define TCSETSW 54 #define TCSETSW 54

View File

@@ -1,7 +1,7 @@
#ifndef _TERMIOS_H #ifndef _TERMIOS_H
#define _TERMIOS_H 1 #define _TERMIOS_H 1
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/termios.h.html // https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/termios.h.html
#include <sys/cdefs.h> #include <sys/cdefs.h>
@@ -39,6 +39,14 @@ struct termios
speed_t c_ispeed; speed_t c_ispeed;
}; };
struct winsize
{
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* unused by kernel */
unsigned short ws_ypixel; /* unused by kernel */
};
#define BRKINT 0x001 #define BRKINT 0x001
#define ICRNL 0x002 #define ICRNL 0x002
#define IGNBRK 0x004 #define IGNBRK 0x004
@@ -143,8 +151,10 @@ int tcflow(int fildes, int action);
int tcflush(int fildes, int queue_selector); int tcflush(int fildes, int queue_selector);
int tcgetattr(int fildes, struct termios* termios_p); int tcgetattr(int fildes, struct termios* termios_p);
pid_t tcgetsid(int fildes); pid_t tcgetsid(int fildes);
int tcgetwinsize(int fildes, struct winsize* winsize_p);
int tcsendbreak(int fildes, int duration); int tcsendbreak(int fildes, int duration);
int tcsetattr(int fildes, int optional_actions, const struct termios* termios_p); int tcsetattr(int fildes, int optional_actions, const struct termios* termios_p);
int tcsetwinsize(int fildes, const struct winsize* winsize_p);
__END_DECLS __END_DECLS

View File

@@ -117,3 +117,13 @@ int tcsendbreak(int fd, int duration)
dwarnln("FIXME: tcsendbreak({}, {})", fd, duration); dwarnln("FIXME: tcsendbreak({}, {})", fd, duration);
return -1; return -1;
} }
int tcgetwinsize(int fildes, struct winsize* winsize_p)
{
return ioctl(fildes, TIOCGWINSZ, winsize_p);
}
int tcsetwinsize(int fildes, const struct winsize* winsize_p)
{
return ioctl(fildes, TIOCSWINSZ, winsize_p);
}

View File

@@ -9,8 +9,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/select.h> #include <sys/select.h>
#include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@@ -141,14 +141,14 @@ void Terminal::run()
m_cells_cols = cols(); m_cells_cols = cols();
{ {
winsize winsize { const winsize winsize {
.ws_row = static_cast<unsigned short>(rows()), .ws_row = static_cast<unsigned short>(rows()),
.ws_col = static_cast<unsigned short>(cols()), .ws_col = static_cast<unsigned short>(cols()),
.ws_xpixel = static_cast<unsigned short>(m_window->width()), .ws_xpixel = static_cast<unsigned short>(m_window->width()),
.ws_ypixel = static_cast<unsigned short>(m_window->height()), .ws_ypixel = static_cast<unsigned short>(m_window->height()),
}; };
if (ioctl(m_shell_info.pts_master, TIOCSWINSZ, &winsize) == -1) if (tcsetwinsize(m_shell_info.pts_master, &winsize) == -1)
perror("ioctl"); perror("tcsetwinsize");
} }
{ {
@@ -212,11 +212,8 @@ void Terminal::run()
.ws_xpixel = static_cast<unsigned short>(m_window->width()), .ws_xpixel = static_cast<unsigned short>(m_window->width()),
.ws_ypixel = static_cast<unsigned short>(m_window->height()), .ws_ypixel = static_cast<unsigned short>(m_window->height()),
}; };
if (ioctl(m_shell_info.pts_master, TIOCSWINSZ, &winsize) == -1) if (tcsetwinsize(m_shell_info.pts_master, &winsize) == -1)
{ perror("tcsetwinsize");
perror("ioctl");
return;
}
}); });
m_window->set_key_event_callback([&](LibGUI::EventPacket::KeyEvent::event_t event) { m_window->set_key_event_callback([&](LibGUI::EventPacket::KeyEvent::event_t event) {

View File

@@ -8,8 +8,8 @@
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <termios.h>
struct config_t struct config_t
{ {
@@ -406,7 +406,7 @@ int main(int argc, const char* argv[])
else for (; i < argc; i++) else for (; i < argc; i++)
MUST(files.emplace_back(BAN::StringView(argv[i]))); MUST(files.emplace_back(BAN::StringView(argv[i])));
g_stdout_terminal = isatty(STDOUT_FILENO) && ioctl(STDOUT_FILENO, TIOCGWINSZ, &g_terminal_size) == 0; g_stdout_terminal = isatty(STDOUT_FILENO) && tcgetwinsize(STDOUT_FILENO, &g_terminal_size) == 0;
int ret = 0; int ret = 0;
for (size_t i = 0; i < files.size(); i++) for (size_t i = 0; i < files.size(); i++)