14#include <ncursesw/ncurses.h>
18#include "spdlog/spdlog.h"
23#define CALL_DEFAULT_SIGNAL_HANDLER(p_SignalType) \
25 signal(p_SignalType, SIG_DFL); \
35static void HandleTerminationSignal(
int p_Parameter)
38 "Termination signal sent with parameter {}.",
48static void HandleInterruptSignal(
int p_Parameter)
51 "Interrupt signal sent with parameter {}.",
61static void HandleQuitSignal(
int p_Parameter)
64 "Quit signal sent with parameter {}.",
76static void HandleHangupSignal(
int p_Parameter)
79 "Hang-up signal sent with parameter {}.",
88static bool InitializeNcurses(
void)
90 const WINDOW*
const t_Window = initscr();
93 spdlog::error(
"Failed to initialise the screen.");
101 spdlog::error(
"Failed to make input unbuffered.");
109 spdlog::error(
"Failed to disable input echoing.");
117 spdlog::error(
"Failed to disable newlines.");
123 if (intrflush(stdscr, FALSE) != OK)
125 spdlog::error(
"Failed to disable screen flushing.");
131 if (nodelay(stdscr, TRUE) != OK)
133 spdlog::error(
"Failed to make nodelay non-blocking.");
139 if (keypad(stdscr, TRUE) != OK)
141 spdlog::error(
"Failed to enable keypad input.");
150static bool InitializeTermios(
void)
158 if (tcgetattr(STDIN_FILENO, &t_Settings) != 0)
160 spdlog::error(
"Failed to get Termios settings.");
165 t_Settings.c_oflag |= ONLCR;
166 if (tcsetattr(0, TCSANOW, &t_Settings) != 0)
168 spdlog::error(
"Failed to set Termios settings.");
177static bool SetUpSignalHandlers(
void)
180 const __sighandler_t t_OldTerminationHandler = signal(
182 HandleTerminationSignal);
183 if (t_OldTerminationHandler == SIG_ERR)
185 spdlog::error(
"Failed to set termination signal handler.");
191 const __sighandler_t t_OldInterruptHandler = signal(
193 HandleInterruptSignal);
194 if (t_OldInterruptHandler == SIG_ERR)
196 spdlog::error(
"Failed to set interrupt signal handler.");
202 const __sighandler_t t_OldQuitHandler = signal(
205 if (t_OldQuitHandler == SIG_ERR)
207 spdlog::error(
"Failed to set quit signal handler.");
213 const __sighandler_t t_OldHangupHandler = signal(
216 if (t_OldHangupHandler == SIG_ERR)
218 spdlog::error(
"Failed to set hang-up signal handler.");
240 t_Key->second =
false;
252 t_Key->second =
false;
259 if (t_Ch >=
'a' && t_Ch <=
'z')
269 t_Ch = toupper(t_Ch);
294 const bool t_IsPressed =
IsPressed(p_Key);
297 const bool t_PreviousValue =
300 : t_PreviousState->second;
302 const bool t_Down = t_IsPressed && !t_PreviousValue;
313 const bool t_IsPressed =
IsPressed(p_Key);
316 const bool t_PreviousValue =
319 : t_PreviousState->second;
321 const bool t_Up = !t_IsPressed && t_PreviousValue;
339 return t_CurrentlyPressed->second;
367 return t_StateLastUpdate->second;
383 const bool t_NcursesResult = InitializeNcurses();
384 const bool t_TermiosResult = InitializeTermios();
385 const bool t_SignalResult = SetUpSignalHandlers();
387 return t_NcursesResult && t_TermiosResult && t_SignalResult;
404 char*
const p_Target,
405 const size_t p_MaxLengthThatWillFitInTarget,
406 const std::string& p_Source)
411 "Tried to copy a string, but the target was null. The string was \"{}\".",
417 if (p_MaxLengthThatWillFitInTarget == 0)
420 "Tried to copy a string, but the target's size is zero. The string was \"{}\".",
426 if (p_MaxLengthThatWillFitInTarget <= p_Source.length())
429 "Tried to copy a string that was longer than {} characters, which makes it too big for its target buffer. The string was \"{}\".",
430 p_MaxLengthThatWillFitInTarget,
436 strcpy(p_Target, p_Source.c_str());
442 const short int p_ConsoleWidth,
443 const short int p_ConsoleHeight,
444 const short int p_ConsoleScrollback)
455 printf(
"\e[8;%d;%dt", p_ConsoleHeight, p_ConsoleWidth);
470 const int p_ConsoleCurrentOffset)
472 printf(
"\e[%d;1H", p_ConsoleCurrentOffset);
491 spdlog::error(
"Failed to clear the screen.");
499 return g_Input.
GetKey(p_Key);
514 const char*
const t_Xdg = getenv(
"XDG_DOCUMENTS_DIR");
517 const char*
const t_Home = getenv(
"HOME");
518 if (!t_Xdg && !t_Home)
520 return std::string(
"");
523 const std::string t_DocumentsDir =
524 (!t_Xdg || strlen(t_Xdg) == 0)
525 ? std::string(t_Home) + std::string(
"/Documents")
526 : std::string(t_Xdg);
528 return t_DocumentsDir;
532 std::string p_Path_UTF8)
534 return std::ifstream(p_Path_UTF8, std::ifstream::binary);
538 std::string p_Path_UTF8)
540 return std::ofstream(p_Path_UTF8, std::ofstream::binary);
545 return std::filesystem::exists(p_Path_UTF8);
549 std::string p_Path_UTF8)
553 std::filesystem::create_directory(p_Path_UTF8);
CORESDK_API SDKReturnCode CoreSdk_ShutDown()
Shut down the wrapper. This needs to be called last.
void ApplyConsolePosition(const int p_ConsoleCurrentOffset)
Set the current console position. This handles the platform-specific part of advancing the console po...
static void ClearConsole(void)
Clear the console window.
void UpdateInput(void)
Update the current keyboard state.
bool PlatformSpecificInitialization(void)
Initialise things only needed for this platform.
bool GetKeyDown(const int p_Key)
Returns true first time it is called when key is pressed.
bool GetKeyUp(const int p_Key)
Returns true first time it is called when key is released.
static bool CopyString(char *const p_Target, const size_t p_MaxLengthThatWillFitInTarget, const std::string &p_Source)
Copy the given string into the given target.
bool ResizeWindow(const short int p_ConsoleWidth, const short int p_ConsoleHeight, const short int p_ConsoleScrollback)
Resize the window, so the log messages will fit. Make sure not to enter illegal sizes in here or SetC...
bool DoesFolderOrFileExist(std::string p_Path_UTF8)
Check if the given folder or file exists. The folder path given should be in UTF-8 format.
bool PlatformSpecificShutdown(void)
Shut down things only needed for this platform.
bool GetKey(const int p_Key)
Gets key's current state. True is pressed false is not pressed.
std::ofstream GetOutputFileStream(std::string p_Path_UTF8)
Get an output stream for the given file. The file's path should be in UTF-8 format.
std::string GetDocumentsDirectoryPath_UTF8(void)
Get the path to the user's Documents folder. The string should be in UTF-8 format.
void CreateFolderIfItDoesNotExist(std::string p_Path_UTF8)
Create the given folder if it does not exist. The folder path given should be in UTF-8 format.
static const std::string s_SlashForFilesystemPath
The slash character that is used in the filesystem.
std::ifstream GetInputFileStream(std::string p_Path_UTF8)
Get an input stream for the given file. The file's path should be in UTF-8 format.