87 lines
1.7 KiB
C
87 lines
1.7 KiB
C
#include "term.h"
|
|
|
|
// if windows is defined.
|
|
|
|
#if defined(_WIN64) || defined(_WIN32)
|
|
#include <Windows.h>
|
|
struct tart_vec2 term_current_size() {
|
|
struct tart_vec2 ret;
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
|
unsigned int rows = (csbi.srWindow.Right - csbi.srWindow.Left + 1);
|
|
unsigned int cols = (csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
|
|
|
|
unsigned short max_short = 0XFFFF;
|
|
if(rows < max_short && cols < max_short) {
|
|
|
|
ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols};
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
#else
|
|
#include <sys/ioctl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
struct tart_vec2 term_current_size() {
|
|
struct tart_vec2 ret;
|
|
|
|
struct winsize w;
|
|
|
|
|
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
|
|
unsigned int rows = w.ws_col;
|
|
unsigned int cols = w.ws_row;
|
|
|
|
unsigned short max_short = 0XFFFF;
|
|
if(rows < max_short && cols < max_short) {
|
|
|
|
ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols};
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
struct termios old, current;
|
|
|
|
void init_termios(int args) {
|
|
tcgetattr(0, &old);
|
|
|
|
current = old;
|
|
|
|
current.c_lflag &= ~ICANON;
|
|
|
|
if(args == 0x01) {
|
|
current.c_lflag |= ECHO;
|
|
} else {
|
|
current.c_lflag &= ~ECHO;
|
|
}
|
|
|
|
tcsetattr(0,TCSANOW, ¤t);
|
|
}
|
|
|
|
void reset_termios(void) {
|
|
tcsetattr(0, TCSANOW, &old);
|
|
}
|
|
|
|
char term_getch() {
|
|
char tmp;
|
|
init_termios(0x00);
|
|
tmp = getchar();
|
|
reset_termios();
|
|
return tmp;
|
|
}
|
|
|
|
char term_getche() {
|
|
char tmp;
|
|
init_termios(0x01);
|
|
tmp = getchar();
|
|
reset_termios();
|
|
return tmp;
|
|
}
|
|
|
|
#endif
|