term.c
1 2 #include <stdio.h> 3 #include <termios.h> 4 #include <unistd.h> 5 #include <ctype.h> 6 7 #define RESPONSE_SIZE 10 8 9 int 10 get_root_size(int *x, int *y) 11 { 12 struct termios original, changed; 13 char response[RESPONSE_SIZE] = ""; 14 int index = 0; 15 int ch = 0; 16 int result; 17 18 tcgetattr(STDIN_FILENO, &original); 19 changed = original; 20 changed.c_lflag &= ~(ICANON | ECHO); 21 changed.c_cc[VMIN] = 1; 22 changed.c_cc[VTIME] = 0; 23 tcsetattr(STDIN_FILENO, TCSANOW, &changed); 24 25 /* move down/right until end of screen */ 26 printf("\033[9999;9999H"); 27 fflush(stdout); 28 29 /* query cursor position */ 30 printf("\033[6n"); 31 fflush(stdout); 32 33 for (;;) { 34 ch = getchar(); 35 if (ch == 'R' || ch == EOF || index >= RESPONSE_SIZE) break; 36 if (!isprint(ch)) continue; 37 response[index++] = ch; 38 } 39 response[index] = '\0'; 40 41 result = sscanf(response, "[%d;%d", y, x); 42 43 /* move cursor to top-left of screen */ 44 printf("\033[1;1H"); 45 46 // tcsetattr(STDIN_FILENO, TCSANOW, &original); 47 48 return result == 2; 49 }