From 6935d0534992a4f256831d9891be75afd2c6476e Mon Sep 17 00:00:00 2001 From: PreacherDHM Date: Fri, 31 Jan 2025 13:11:21 -0800 Subject: [PATCH] added windows input port and added defines for windows and linux systems --- CMakeLists.txt | 1 + source/CMakeLists.txt | 10 +++++++ source/term.c | 65 ++++++++++++++++++++++++++++++++----------- testing/input.cpp | 6 ++-- 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0d3b83..b912b54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set( CMAKE_CXX_STANDARD_REQUIRED ON) set( CMAKE_C_STANDARD 11) set( CMAKE_C_STANDARD_REQUIRED ON) + #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/libs) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 8bb026f..8b1763c 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -3,6 +3,7 @@ set( CMAKE_STATIC_LIBRARY_PREFIX "") set( CMAKE_C_STANDARD 11) set( CMAKE_C_STANDARD_REQUIRED ON) + set(LIB_SOURCES term.c term.h @@ -10,4 +11,13 @@ set(LIB_SOURCES ) add_library(${PROJECT_NAME} STATIC ${LIB_SOURCES}) + target_include_directories(${PROJECT_NAME} BEFORE PUBLIC "../includes/") + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_compile_definitions(${PROJECT_NAME} PUBLIC "_LINUX") +endif() +if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + target_compile_definitions(${PROJECT_NAME} PUBLIC "_WIN32") + target_compile_definitions(${PROJECT_NAME} PUBLIC "_WIN64") +endif() diff --git a/source/term.c b/source/term.c index e9b4f85..3b000c9 100644 --- a/source/term.c +++ b/source/term.c @@ -1,9 +1,30 @@ #include "term.h" +#include + +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 +#include + + + + struct tart_vec2 term_current_size() { struct tart_vec2 ret; CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -19,6 +40,30 @@ struct tart_vec2 term_current_size() { 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 #include @@ -85,22 +130,8 @@ char term_getche() { } -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(); @@ -121,6 +152,9 @@ int term_threaded_input_init() { return 1; } + +#endif + char term_tinputi(int idx){ if(idx < 8) { return __term_input_buffer__[idx]; @@ -135,9 +169,6 @@ char* term_tinputb() { char term_tinput() { return __term_input_buffer__[0]; } - -#endif - void term_disable_cursor() { fputs("\033[?25l", stdout); } diff --git a/testing/input.cpp b/testing/input.cpp index cb800f2..1609df7 100644 --- a/testing/input.cpp +++ b/testing/input.cpp @@ -1,15 +1,13 @@ #include #include -#include -#include -#include +#ifdef _LINUX +#endif 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();