From 49f58a1c2b7219cf9641c0f5ecc5246da3e58c7d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 17:05:10 -0500 Subject: [PATCH] feat(console input): added get character function --- Include/Iony/Core/Console.h | 2 ++ Include/Iony/Utils/System.h | 12 ++++++++---- Src/Core/Console.c | 30 ++++++++++++++++++++++++++++-- Tests/Main.c | 5 +++++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index d52719b..09af028 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -26,4 +26,6 @@ IonyError iony_print(const char *text, long length); +IonyError iony_get_character(char *character); + #endif diff --git a/Include/Iony/Utils/System.h b/Include/Iony/Utils/System.h index 71c8a66..fa85951 100644 --- a/Include/Iony/Utils/System.h +++ b/Include/Iony/Utils/System.h @@ -25,13 +25,17 @@ #if defined(__GNUC__) #if defined(__x86_64__) -#define SYS_WRITE 1 +#define IONY_SYS_READ 0 +#define IONY_SYS_WRITE 1 #elif defined(__i386__) -#define SYS_WRITE 4 +#define IONY_SYS_READ 3 +#define IONY_SYS_WRITE 4 #elif defined(__aarch64__) -#define SYS_WRITE 64 +#define IONY_SYS_READ 63 +#define IONY_SYS_WRITE 64 #elif defined(__arm__) -#define SYS_WRITE 4 +#define IONY_SYS_READ 3 +#define IONY_SYS_WRITE 4 #endif static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { diff --git a/Src/Core/Console.c b/Src/Core/Console.c index a4b351e..df69d0b 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -20,9 +20,12 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Iony/Core/Console.h" +#include "Iony/Utils/Error.h" #if defined(_WIN32) #include + +static HANDLE iony_standard_console_in = 0; #elif defined(__GNUC__) #include "../../Include/Iony/Utils/System.h" #endif @@ -37,13 +40,36 @@ IonyError iony_print(const char *text, const long length) { if (standard_console_out_handle == 0) { standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); } - + DWORD characters_written; if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) - if (iony_system_call(SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #endif + + return IONY_ERROR_NONE; +} + +IonyError iony_get_character(char *character) { + if (character == 0) { + return IONY_ERROR_INVALID_POINTER; + } + + #if defined(_WIN32) + if (iony_standard_console_in == 0) { + iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); + } + + DWORD characters_read; + if (!ReadFile(iony_standard_console_in, character, 1, &characters_read, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } + #elif defined(__GNUC__) + if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif diff --git a/Tests/Main.c b/Tests/Main.c index 357c7c9..18d4eff 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -27,5 +27,10 @@ int main(void) { return -1; } + char character = ' '; + if (iony_get_character(&character) != IONY_ERROR_NONE) { + return -1; + } + return 0; } \ No newline at end of file