linux input port done

This commit is contained in:
2025-01-31 19:47:49 +00:00
parent 23be603dfc
commit 69ce396f38
3 changed files with 95 additions and 24 deletions

View File

@@ -23,7 +23,8 @@ struct tart_vec2 term_current_size() {
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <threads.h>
#include <termios.h>
struct tart_vec2 term_current_size() {
struct tart_vec2 ret;
@@ -60,11 +61,11 @@ void init_termios(int args) {
current.c_lflag &= ~ECHO;
}
tcsetattr(0,TCSANOW, &current);
tcsetattr(0,TCSAFLUSH, &current);
}
void reset_termios(void) {
tcsetattr(0, TCSANOW, &old);
tcsetattr(0, TCSAFLUSH, &old);
}
char term_getch() {
@@ -83,4 +84,64 @@ char term_getche() {
return tmp;
}
char __term_input_buffer__[8] = {' ',' ',' ',' ',' ',' ',' ',' '};
pthread_t input_thread;
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;
}
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;
}
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];
}
#endif
void term_disable_cursor() {
fputs("\033[?25l", stdout);
}
void term_enable_cursor() {
fputs("\033[?25h", stdout);
}

View File

@@ -16,4 +16,15 @@ struct tart_vec2 term_current_size();
char term_getch();
char term_getche();
// tinput is threaded input
int term_threaded_input_init();
int term_threaded_input_stop();
char term_tinputi(int idx);
char* term_tinputb();
char term_tinput();
void term_disable_cursor();
void term_enable_cursor();
#endif

View File

@@ -2,29 +2,28 @@
#include <iostream>
#include <unistd.h>
#include <threads.h>
#include <time.h>
#include <stdio.h>
int counter = 0;
int main (int argc, char *argv[]) {
char t = ' ';
while(t != 'q') {
std::cout << "input a char" << ftell(stdin) <<" [";
fseek(stdin, 0, SEEK_END);
if(ftell(stdin) > 0) {
t = term_getche();
rewind(stdin);
std::cout << t;
std::cout << "]\n\r ";
return 0;
}else {
std::cout << "eof";
std::cout << "]\r ";
}
#include <termios.h>
struct timespec b;
b.tv_sec = 1;
thrd_sleep(&b, NULL); // sleep 1 sec
char* bbuf;
int main (int argc, char *argv[]) {
int n;
int counter = 0;
struct termios te;
term_disable_cursor();
term_threaded_input_init();
bbuf = term_tinputb();
while(bbuf[0] != 'q') {
std::cout << counter++ << '[';
for (int i = 0; i < 8; i++) {
std::cout << bbuf[i] << ',';
}
std::cout << "] \r";
}
std::cout << '\n';
term_threaded_input_stop();
term_enable_cursor();
exit(0);
return 0;
}