/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ----------------------------------------------------------------------------- * File: Console.c * Description: * This file contains the functions for reading and writting to the console. * * Author: Mineplay * ----------------------------------------------------------------------------- */ #include "../../Include/Iony/Core/Console.h" #include "../../Include/Iony/Utils/System.h" #include "../../Include/Iony/Utils/Base.h" #if defined(_WIN32) static HANDLE iony_standard_console_out = IONY_NULL; static HANDLE iony_standard_console_in = IONY_NULL; static HANDLE iony_standard_console_error = IONY_NULL; static WORD iony_original_console_attributes = 0; #elif defined(__GNUC__) static const char IONY_RESET_COLOR[] = "\033[0m"; static const char IONY_ANSI_COLORS[][8] = { "\x1B[0;30m", "\x1B[0;34m", "\x1B[0;32m", "\x1B[0;36m", "\x1B[0;31m", "\x1B[0;35m", "\x1B[0;33m", "\x1B[0;37m", "\x1B[1;30m", "\x1B[1;34m", "\x1B[1;32m", "\x1B[1;36m", "\x1B[1;31m", "\x1B[1;35m", "\x1B[1;33m", "\x1B[1;37m", }; #endif IonyError iony_console_print(const char *text, const unsigned long length) { if (text == IONY_NULL) { return IONY_ERROR_INVALID_POINTER; } #if defined(_WIN32) if (iony_standard_console_out == IONY_NULL) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); if (iony_standard_console_out == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } DWORD characters_written; if (!WriteFile(iony_standard_console_out, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, (long)length, 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; } IonyError iony_console_error(const char *text, unsigned long length) { if (text == IONY_NULL) { return IONY_ERROR_INVALID_POINTER; } #if defined(_WIN32) if (iony_standard_console_error == IONY_NULL) { iony_standard_console_error = GetStdHandle(STD_ERROR_HANDLE); if (iony_standard_console_error == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } DWORD characters_written; if (!WriteFile(iony_standard_console_error, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) if (iony_system_call(IONY_SYS_WRITE, 2, (long)text, (long)length, 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; } IonyError iony_console_clear(void) { #if defined(_WIN32) if (iony_standard_console_out == IONY_NULL) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); if (iony_standard_console_out == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } CONSOLE_SCREEN_BUFFER_INFO console_info; DWORD characters_written, cell_count; COORD cursor_coords = { 0, 0 }; if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } cell_count = console_info.dwSize.X * console_info.dwSize.Y; if (!FillConsoleOutputCharacterA(iony_standard_console_out, ' ', cell_count, cursor_coords, &characters_written)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } if (!FillConsoleOutputAttribute(iony_standard_console_out, console_info.wAttributes, cell_count, cursor_coords, &characters_written)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } SetConsoleCursorPosition(iony_standard_console_out, cursor_coords); #elif defined(__GNUC__) const char clear[] = "\033[2J\033[H"; if (iony_system_call(IONY_SYS_WRITE, 1, (long)clear, sizeof(clear) - 1, 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; } IonyError iony_console_get_character(char *character) { if (character == IONY_NULL) { return IONY_ERROR_INVALID_POINTER; } #if defined(_WIN32) if (iony_standard_console_in == IONY_NULL) { iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); if (iony_standard_console_in == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } DWORD characters_read; if (!ReadFile(iony_standard_console_in, character, 1, &characters_read, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } if (*character == '\r') { 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) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; } IonyError iony_console_get_line(char *character_string, const long max_length, unsigned long *characters_read) { if (character_string == IONY_NULL) { return IONY_ERROR_INVALID_POINTER; } #if defined(_WIN32) if (iony_standard_console_in == IONY_NULL) { iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE); if (iony_standard_console_in == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } if (!ReadFile(iony_standard_console_in, character_string, max_length, characters_read, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) long result = iony_system_call(IONY_SYS_READ, 0, (long)character_string, max_length, 0, 0, 0); if (result < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } *characters_read = result; #endif return IONY_ERROR_NONE; } IonyError iony_console_set_color(IonyConsoleColor color) { #if defined(_WIN32) if (iony_standard_console_out == IONY_NULL) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); if (iony_standard_console_out == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } if (iony_original_console_attributes == 0) { CONSOLE_SCREEN_BUFFER_INFO console_info; if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } iony_original_console_attributes = console_info.wAttributes; } if (SetConsoleTextAttribute(iony_standard_console_out, color) == 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_ANSI_COLORS[color], sizeof(IONY_ANSI_COLORS[color]), 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; } IonyError iony_console_reset_color(void) { #if defined(_WIN32) if (iony_standard_console_out == IONY_NULL) { iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE); if (iony_standard_console_out == INVALID_HANDLE_VALUE) { return IONY_ERROR_HANDLE_NOT_FOUND; } } if (iony_original_console_attributes == 0) { CONSOLE_SCREEN_BUFFER_INFO console_info; if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) { return IONY_ERROR_SYSTEM_CALL_FAILED; } iony_original_console_attributes = console_info.wAttributes; } if (SetConsoleTextAttribute(iony_standard_console_out, iony_original_console_attributes) == 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #elif defined(__GNUC__) if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_RESET_COLOR, sizeof(IONY_RESET_COLOR), 0, 0, 0) < 0) { return IONY_ERROR_SYSTEM_CALL_FAILED; } #endif return IONY_ERROR_NONE; }