From 23be603dfcf7499e3bbf81c0fda1f157861fe932 Mon Sep 17 00:00:00 2001 From: PreacherDHM Date: Thu, 30 Jan 2025 03:31:12 +0000 Subject: [PATCH] trying to fix input --- source/CMakeLists.txt | 4 ++-- source/term.c | 41 +++++++++++++++++++++++++++++++++++++++++ source/term.h | 3 +++ testing/CMakeLists.txt | 6 ++++++ testing/input.cpp | 30 ++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 testing/input.cpp diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2260e72..8bb026f 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,7 +1,7 @@ project(TartLib VERSION 0.1) set( CMAKE_STATIC_LIBRARY_PREFIX "") -set( CMAKE_CXX_STANDARD 11) -set( CMAKE_CXX_STANDARD_REQUIRED ON) +set( CMAKE_C_STANDARD 11) +set( CMAKE_C_STANDARD_REQUIRED ON) set(LIB_SOURCES term.c diff --git a/source/term.c b/source/term.c index e82c0dd..d154046 100644 --- a/source/term.c +++ b/source/term.c @@ -23,6 +23,8 @@ struct tart_vec2 term_current_size() { #include #include #include +#include +#include struct tart_vec2 term_current_size() { struct tart_vec2 ret; @@ -42,4 +44,43 @@ struct tart_vec2 term_current_size() { 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 diff --git a/source/term.h b/source/term.h index 82f00e2..1ea3fc4 100644 --- a/source/term.h +++ b/source/term.h @@ -13,4 +13,7 @@ struct tart_vec2 term_current_size(); +char term_getch(); +char term_getche(); + #endif diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index ce4718a..8b75864 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt @@ -11,3 +11,9 @@ add_executable(${PROJECT_NAME} ${SOURCES} ) target_link_libraries(${PROJECT_NAME} TartLib PickleLib) add_test(${PROJECT_NAME} "${CMAKE_SOURCE_DIR}/bin/testing.exe") + +project(InputTartTest) +set( CMAKE_CXX_STANDARD 11) +set( CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(${PROJECT_NAME} input.cpp ) +target_link_libraries(${PROJECT_NAME} TartLib PickleLib) diff --git a/testing/input.cpp b/testing/input.cpp new file mode 100644 index 0000000..54d7db2 --- /dev/null +++ b/testing/input.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +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 "; + } + + struct timespec b; + b.tv_sec = 1; + thrd_sleep(&b, NULL); // sleep 1 sec + } + std::cout << '\n'; + return 0; +}