^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include "term.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) void get_term_dimensions(struct winsize *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) char *s = getenv("LINES");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) ws->ws_row = atoi(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) s = getenv("COLUMNS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) if (s != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) ws->ws_col = atoi(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (ws->ws_row && ws->ws_col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #ifdef TIOCGWINSZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ws->ws_row && ws->ws_col)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ws->ws_row = 25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) ws->ws_col = 80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) void set_term_quiet_input(struct termios *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct termios tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) tcgetattr(0, old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) tc = *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) tc.c_lflag &= ~(ICANON | ECHO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) tc.c_cc[VMIN] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) tc.c_cc[VTIME] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) tcsetattr(0, TCSANOW, &tc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }