180 lines
3.4 KiB
C
180 lines
3.4 KiB
C
#include "term.h"
|
|
#include <stdio.h>
|
|
|
|
char __term_input_buffer__[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
|
|
void set_char_push_back(char t) {
|
|
char tmp1;
|
|
char hold1;
|
|
char hold2;
|
|
hold1 = __term_input_buffer__[0];
|
|
for(int i = 0; i < 8 - 1; i++) {
|
|
hold2 = __term_input_buffer__[i+1];
|
|
__term_input_buffer__[i+1] = hold1;
|
|
hold1 = hold2;
|
|
|
|
}
|
|
__term_input_buffer__[0] = t;
|
|
}
|
|
|
|
// if windows is defined.
|
|
|
|
#if defined(_WIN64) || defined(_WIN32)
|
|
#include <Windows.h>
|
|
#include <conio.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;
|
|
}
|
|
|
|
HANDLE __term_input_thread;
|
|
int __term_input_thread_stop;
|
|
|
|
DWORD WINAPI input_threaded_func(void* data) {
|
|
while(__term_input_thread_stop == 1) {
|
|
if(_kbhit())
|
|
set_char_push_back(_getch());
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
int term_threaded_input_init() {
|
|
__term_input_thread_stop = 1;
|
|
__term_input_thread = CreateThread(NULL,0,input_threaded_func,NULL, 0, NULL);
|
|
return 0;
|
|
}
|
|
int term_threaded_input_stop() {
|
|
__term_input_thread_stop = 0;
|
|
WaitForSingleObject(__term_input_thread, INFINITE);
|
|
return 0;
|
|
}
|
|
|
|
#else
|
|
#include <sys/ioctl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#include <threads.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,0, ¤t);
|
|
}
|
|
|
|
void reset_termios(void) {
|
|
//current.c_lflag &= ~ECHO;
|
|
tcsetattr(0, 0, &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;
|
|
}
|
|
|
|
|
|
pthread_t input_thread;
|
|
|
|
|
|
void* input_threaded_func(void* vargp) {
|
|
int myid = getpid();
|
|
while(1) {
|
|
set_char_push_back(term_getch());
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int term_threaded_input_stop() {
|
|
pthread_cancel(input_thread);
|
|
return 1;
|
|
}
|
|
|
|
int term_threaded_input_init() {
|
|
|
|
pthread_create(&input_thread, NULL, input_threaded_func, NULL);
|
|
return 1;
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
char term_tinputi(int idx){
|
|
if(idx < 8) {
|
|
return __term_input_buffer__[idx];
|
|
}
|
|
return 0x0;
|
|
}
|
|
|
|
char* term_tinputb() {
|
|
return __term_input_buffer__;
|
|
}
|
|
|
|
char term_tinput() {
|
|
return __term_input_buffer__[0];
|
|
}
|
|
void term_disable_cursor() {
|
|
printf("\033[?25l", stdout);
|
|
}
|
|
|
|
void term_enable_cursor() {
|
|
printf("\033[?25h", stdout);
|
|
}
|