linux input port done
This commit is contained in:
@@ -23,7 +23,8 @@ struct tart_vec2 term_current_size() {
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <pthread.h>
|
||||||
|
#include <threads.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
struct tart_vec2 term_current_size() {
|
struct tart_vec2 term_current_size() {
|
||||||
struct tart_vec2 ret;
|
struct tart_vec2 ret;
|
||||||
@@ -60,11 +61,11 @@ void init_termios(int args) {
|
|||||||
current.c_lflag &= ~ECHO;
|
current.c_lflag &= ~ECHO;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcsetattr(0,TCSANOW, ¤t);
|
tcsetattr(0,TCSAFLUSH, ¤t);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_termios(void) {
|
void reset_termios(void) {
|
||||||
tcsetattr(0, TCSANOW, &old);
|
tcsetattr(0, TCSAFLUSH, &old);
|
||||||
}
|
}
|
||||||
|
|
||||||
char term_getch() {
|
char term_getch() {
|
||||||
@@ -83,4 +84,64 @@ char term_getche() {
|
|||||||
return tmp;
|
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
|
#endif
|
||||||
|
|
||||||
|
void term_disable_cursor() {
|
||||||
|
fputs("\033[?25l", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void term_enable_cursor() {
|
||||||
|
fputs("\033[?25h", stdout);
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,4 +16,15 @@ struct tart_vec2 term_current_size();
|
|||||||
char term_getch();
|
char term_getch();
|
||||||
char term_getche();
|
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
|
#endif
|
||||||
|
|||||||
@@ -2,29 +2,28 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <threads.h>
|
#include <threads.h>
|
||||||
#include <time.h>
|
#include <termios.h>
|
||||||
#include <stdio.h>
|
|
||||||
int counter = 0;
|
char* bbuf;
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
int main (int argc, char *argv[]) {
|
||||||
char t = ' ';
|
int n;
|
||||||
while(t != 'q') {
|
int counter = 0;
|
||||||
std::cout << "input a char" << ftell(stdin) <<" [";
|
struct termios te;
|
||||||
fseek(stdin, 0, SEEK_END);
|
term_disable_cursor();
|
||||||
if(ftell(stdin) > 0) {
|
term_threaded_input_init();
|
||||||
t = term_getche();
|
bbuf = term_tinputb();
|
||||||
rewind(stdin);
|
while(bbuf[0] != 'q') {
|
||||||
std::cout << t;
|
|
||||||
std::cout << "]\n\r ";
|
std::cout << counter++ << '[';
|
||||||
return 0;
|
for (int i = 0; i < 8; i++) {
|
||||||
}else {
|
std::cout << bbuf[i] << ',';
|
||||||
std::cout << "eof";
|
}
|
||||||
std::cout << "] \r";
|
std::cout << "] \r";
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timespec b;
|
|
||||||
b.tv_sec = 1;
|
|
||||||
thrd_sleep(&b, NULL); // sleep 1 sec
|
|
||||||
}
|
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
|
term_threaded_input_stop();
|
||||||
|
term_enable_cursor();
|
||||||
|
exit(0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user