2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief ttysize - Magically divine terminal size
|
2018-08-15 04:07:33 +03:00
|
|
|
*
|
|
|
|
* This is called by getty to determine the size of foreign
|
|
|
|
* terminals, such as ones attached over serial.
|
|
|
|
*
|
|
|
|
* It works by placing the cursor in the lower right of the
|
|
|
|
* screen and requesting its position. Note that typing things
|
|
|
|
* while this happens can cause problems. Maybe we can flush
|
|
|
|
* stdin before doing this to try to avoid any conflicting data?
|
2021-11-26 06:41:56 +03:00
|
|
|
*
|
|
|
|
* @copyright
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2018 K. Lange
|
2018-08-15 04:07:33 +03:00
|
|
|
*/
|
2018-07-06 04:40:49 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-07-06 06:22:03 +03:00
|
|
|
#include <string.h>
|
2018-08-01 04:03:34 +03:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2018-07-06 04:40:49 +03:00
|
|
|
#include <sys/ioctl.h>
|
2018-08-02 07:13:27 +03:00
|
|
|
#include <sys/fswait.h>
|
2018-07-06 06:22:03 +03:00
|
|
|
|
|
|
|
static struct termios old;
|
|
|
|
|
|
|
|
static void set_unbuffered() {
|
|
|
|
tcgetattr(fileno(stdin), &old);
|
|
|
|
struct termios new = old;
|
|
|
|
new.c_lflag &= (~ICANON & ~ECHO);
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_buffered() {
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getc_timeout(FILE * f, int timeout) {
|
|
|
|
int fds[1] = {fileno(f)};
|
2018-08-02 07:13:27 +03:00
|
|
|
int index = fswait2(1,fds,timeout);
|
2018-07-06 06:22:03 +03:00
|
|
|
if (index == 0) {
|
|
|
|
return fgetc(f);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void divine_size(int * width, int * height) {
|
|
|
|
set_unbuffered();
|
|
|
|
|
|
|
|
*width = 80;
|
|
|
|
*height = 24;
|
|
|
|
|
2018-07-06 06:47:54 +03:00
|
|
|
fprintf(stderr, "\033[s\033[1000;1000H\033[6n\033[u");
|
2018-07-06 06:22:03 +03:00
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
char buf[1024] = {0};
|
|
|
|
size_t i = 0;
|
|
|
|
while (1) {
|
|
|
|
char c = getc_timeout(stdin, 200);
|
|
|
|
if (c == 'R') break;
|
|
|
|
if (c == -1) goto _done;
|
|
|
|
if (c == '\033') continue;
|
|
|
|
if (c == '[') continue;
|
|
|
|
buf[i++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * s = strstr(buf, ";");
|
|
|
|
if (s) {
|
|
|
|
*(s++) = '\0';
|
|
|
|
|
|
|
|
*height = atoi(buf);
|
|
|
|
*width = atoi(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
_done:
|
|
|
|
fflush(stderr);
|
|
|
|
set_buffered();
|
|
|
|
}
|
2018-07-06 04:40:49 +03:00
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
2018-07-06 06:22:03 +03:00
|
|
|
int width, height;
|
2018-08-01 04:03:34 +03:00
|
|
|
int opt;
|
|
|
|
int quiet = 0;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "q")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 06:22:03 +03:00
|
|
|
|
2018-08-01 04:03:34 +03:00
|
|
|
if (optind + 2 == argc) {
|
|
|
|
width = atoi(argv[optind]);
|
|
|
|
height = atoi(argv[optind+1]);
|
2018-07-06 06:22:03 +03:00
|
|
|
} else {
|
2018-08-01 04:03:34 +03:00
|
|
|
divine_size(&width, &height);
|
2018-07-06 04:40:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct winsize w;
|
2018-07-06 06:22:03 +03:00
|
|
|
w.ws_col = width;
|
|
|
|
w.ws_row = height;
|
2018-07-06 04:40:49 +03:00
|
|
|
w.ws_xpixel = 0;
|
|
|
|
w.ws_ypixel = 0;
|
|
|
|
ioctl(0, TIOCSWINSZ, &w);
|
2018-07-06 06:22:03 +03:00
|
|
|
|
2018-08-01 04:03:34 +03:00
|
|
|
if (!quiet) {
|
|
|
|
fprintf(stderr, "%dx%d\n", width, height);
|
|
|
|
}
|
2018-07-06 06:22:03 +03:00
|
|
|
|
2018-07-06 04:40:49 +03:00
|
|
|
return 0;
|
|
|
|
}
|