[*] Made conapi requirement under Win32 optional and completely nuke the code when building under non-win32 targets

This commit is contained in:
Reece Wilson 2024-04-14 23:11:15 +01:00
parent 528cb01679
commit 88a07714e5
5 changed files with 413 additions and 110 deletions

View File

@ -305,7 +305,22 @@ namespace Aurora
ADD_GET_PROC(Kernel32, RemoveDllDirectory) ADD_GET_PROC(Kernel32, RemoveDllDirectory)
ADD_GET_PROC(Kernel32, AddDllDirectory) ADD_GET_PROC(Kernel32, AddDllDirectory)
ADD_GET_PROC(Kernel32, SetProcessInformation) ADD_GET_PROC(Kernel32, SetProcessInformation)
ADD_GET_PROC(Kernel32, GetConsoleScreenBufferInfo)
ADD_GET_PROC(Kernel32, SetConsoleScreenBufferSize)
ADD_GET_PROC(Kernel32, SetConsoleWindowInfo)
ADD_GET_PROC(Kernel32, CreateConsoleScreenBuffer)
ADD_GET_PROC(Kernel32, SetConsoleCursorPosition)
ADD_GET_PROC(Kernel32, FillConsoleOutputCharacterW)
ADD_GET_PROC(Kernel32, FillConsoleOutputAttribute)
ADD_GET_PROC(Kernel32, SetConsoleTextAttribute)
ADD_GET_PROC(Kernel32, SetConsoleActiveScreenBuffer)
ADD_GET_PROC(Kernel32, ScrollConsoleScreenBufferW)
ADD_GET_PROC(Kernel32, WriteConsoleInputW)
ADD_GET_PROC(Kernel32, WriteConsoleW)
ADD_GET_PROC(Kernel32, ReadConsoleInputW)
ADD_GET_PROC(Kernel32, GetNumberOfConsoleInputEvents)
ADD_GET_PROC_BI2(Kernel32, PSAPILegacy, K32GetProcessMemoryInfo, GetProcessMemoryInfo) ADD_GET_PROC_BI2(Kernel32, PSAPILegacy, K32GetProcessMemoryInfo, GetProcessMemoryInfo)
ADD_GET_PROC(Sync, WaitOnAddress) ADD_GET_PROC(Sync, WaitOnAddress)

View File

@ -23,6 +23,9 @@ struct _SP_DEVINFO_DATA;
struct _SP_DEVICE_INTERFACE_DATA; struct _SP_DEVICE_INTERFACE_DATA;
struct _SP_DEVICE_INTERFACE_DETAIL_DATA_W; struct _SP_DEVICE_INTERFACE_DETAIL_DATA_W;
struct _NETRESOURCEW; struct _NETRESOURCEW;
struct _CONSOLE_SCREEN_BUFFER_INFO;
struct _SMALL_RECT;
struct _CHAR_INFO;
enum _TOKEN_INFORMATION_CLASS; enum _TOKEN_INFORMATION_CLASS;
enum _SE_OBJECT_TYPE; enum _SE_OBJECT_TYPE;
enum _MINIDUMP_TYPE; enum _MINIDUMP_TYPE;
@ -353,6 +356,95 @@ namespace Aurora
LPVOID ProcessInformation, LPVOID ProcessInformation,
DWORD ProcessInformationSize DWORD ProcessInformationSize
); );
inline BOOL(__stdcall *pGetConsoleScreenBufferInfo)(
HANDLE hConsoleOutput,
_CONSOLE_SCREEN_BUFFER_INFO*lpConsoleScreenBufferInfo
);
inline BOOL(__stdcall *pSetConsoleScreenBufferSize)(
HANDLE hConsoleOutput,
COORD dwSize
);
inline BOOL(__stdcall *pSetConsoleWindowInfo)(
HANDLE hConsoleOutput,
BOOL bAbsolute,
CONST _SMALL_RECT * lpConsoleWindow
);
inline HANDLE(__stdcall *pCreateConsoleScreenBuffer)(
DWORD dwDesiredAccess,
DWORD dwShareMode,
CONST _SECURITY_ATTRIBUTES *lpSecurityAttributes,
DWORD dwFlags,
LPVOID lpScreenBufferData
);
inline BOOL(__stdcall *pSetConsoleCursorPosition)(
HANDLE hConsoleOutput,
COORD dwCursorPosition
);
inline BOOL(__stdcall *pFillConsoleOutputCharacterW)(
HANDLE hConsoleOutput,
WCHAR cCharacter,
DWORD nLength,
COORD dwWriteCoord,
LPDWORD lpNumberOfCharsWritten
);
inline BOOL(__stdcall *pFillConsoleOutputAttribute)(
HANDLE hConsoleOutput,
WORD wAttribute,
DWORD nLength,
COORD dwWriteCoord,
LPDWORD lpNumberOfAttrsWritten
);
inline BOOL(__stdcall *pSetConsoleTextAttribute)(
HANDLE hConsoleOutput,
WORD wAttributes
);
inline BOOL(__stdcall *pSetConsoleActiveScreenBuffer)(
HANDLE hConsoleOutput
);
inline BOOL(__stdcall *pScrollConsoleScreenBufferW)(
HANDLE hConsoleOutput,
CONST _SMALL_RECT * lpScrollRectangle,
CONST _SMALL_RECT * lpClipRectangle,
COORD dwDestinationOrigin,
CONST _CHAR_INFO * lpFill
);
inline BOOL(__stdcall *pGetNumberOfConsoleInputEvents)(
HANDLE hConsoleInput,
LPDWORD lpNumberOfEvents
);
inline BOOL(__stdcall *pReadConsoleInputW)(
HANDLE hConsoleInput,
_INPUT_RECORD * lpBuffer,
DWORD nLength,
LPDWORD lpNumberOfEventsRead
);
inline BOOL(__stdcall *pWriteConsoleInputW)(
HANDLE hConsoleInput,
CONST _INPUT_RECORD * lpBuffer,
DWORD nLength,
LPDWORD lpNumberOfEventsWritten
);
inline BOOL(__stdcall *pWriteConsoleW)(
HANDLE hConsoleInput,
CONST wchar_t * lpBuffer,
DWORD nLength,
LPDWORD lpNumberOfEventsWritten,
void * pReserved
);
inline BOOL(__stdcall *pPrefetchVirtualMemory)( inline BOOL(__stdcall *pPrefetchVirtualMemory)(
HANDLE hProcess, HANDLE hProcess,

View File

@ -82,7 +82,11 @@ namespace Aurora::Console::ConsoleStd
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
ConsoleSecondaryDataLoopSource(LSEvent *pAltHandle) : LSEvent(false, true, true) ConsoleSecondaryDataLoopSource(LSEvent *pAltHandle) : LSEvent(false, true, true)
{ {
handles = { /*pAltHandle->GetHandle()*/AuUInt(GetStdHandle(STD_INPUT_HANDLE)), LSEvent::GetHandle()}; #if defined(STD_INPUT_HANDLE)
handles = { /*pAltHandle->GetHandle()*/AuUInt(GetStdHandle(STD_INPUT_HANDLE)), LSEvent::GetHandle() };
#else
handles = { LSEvent::GetHandle() };
#endif
} }
#else #else
ConsoleSecondaryDataLoopSource(int fd) : LSEvent(false, true, true) ConsoleSecondaryDataLoopSource(int fd) : LSEvent(false, true, true)
@ -278,6 +282,7 @@ namespace Aurora::Console::ConsoleStd
void ProcessCanonical(HANDLE h) void ProcessCanonical(HANDLE h)
{ {
#if defined(AURORA_PLATFORM_WIN32)
INPUT_RECORD records[4096]; INPUT_RECORD records[4096];
DWORD dwRecords; DWORD dwRecords;
@ -296,7 +301,13 @@ namespace Aurora::Console::ConsoleStd
return; return;
} }
if (!GetNumberOfConsoleInputEvents(h, if (!pGetNumberOfConsoleInputEvents ||
!pReadConsoleInputW)
{
return;
}
if (!pGetNumberOfConsoleInputEvents(h,
&dwRecords)) &dwRecords))
{ {
return; return;
@ -308,10 +319,10 @@ namespace Aurora::Console::ConsoleStd
return; return;
} }
if (!ReadConsoleInputW(h, if (!pReadConsoleInputW(h,
records, records,
dwRecords, dwRecords,
&dwRecords)) &dwRecords))
{ {
return; return;
} }
@ -558,6 +569,9 @@ namespace Aurora::Console::ConsoleStd
return; return;
} }
} }
#else
//
#endif
} }
void NoncanonicalTick() void NoncanonicalTick()
@ -590,6 +604,7 @@ namespace Aurora::Console::ConsoleStd
{ {
DWORD mode; DWORD mode;
#if defined(AURORA_PLATFORM_WIN32)
if (!GetConsoleMode(gInputStream, &mode)) if (!GetConsoleMode(gInputStream, &mode))
{ {
return false; return false;
@ -624,14 +639,16 @@ namespace Aurora::Console::ConsoleStd
niceWorkMicrosoft[1].Event.KeyEvent.wVirtualScanCode = pMapVirtualKeyA(VK_RETURN, MAPVK_VK_TO_VSC); niceWorkMicrosoft[1].Event.KeyEvent.wVirtualScanCode = pMapVirtualKeyA(VK_RETURN, MAPVK_VK_TO_VSC);
DWORD idc; DWORD idc;
WriteConsoleInputW(gInputStream, niceWorkMicrosoft, 2, &idc); pWriteConsoleInputW(gInputStream, niceWorkMicrosoft, 2, &idc);
WriteConsoleInputW(gInputStream, niceWorkMicrosoft, 2, &idc); pWriteConsoleInputW(gInputStream, niceWorkMicrosoft, 2, &idc);
return true; return true;
#endif
} }
void LeaveNoncanonicalMode() void LeaveNoncanonicalMode()
{ {
#if defined(AURORA_PLATFORM_WIN32)
if (!AuExchange(gCanonicalEnabled, false)) if (!AuExchange(gCanonicalEnabled, false))
{ {
return; return;
@ -639,6 +656,7 @@ namespace Aurora::Console::ConsoleStd
SetConsoleMode(gInputStream, gCanonicalBackup); SetConsoleMode(gInputStream, gCanonicalBackup);
gWin32Thread = CreateThread(nullptr, 0, StdInWin32Thread, nullptr, 0, nullptr); gWin32Thread = CreateThread(nullptr, 0, StdInWin32Thread, nullptr, 0, nullptr);
#endif
} }
#else #else
@ -697,7 +715,8 @@ namespace Aurora::Console::ConsoleStd
bool IsStdOutTTY() bool IsStdOutTTY()
{ {
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_PLATFORM_WIN32)
//#if defined(AURORA_IS_MODERNNT_DERIVED)
HANDLE hConsole; HANDLE hConsole;
DWORD dwType; DWORD dwType;
@ -710,7 +729,8 @@ namespace Aurora::Console::ConsoleStd
return dwType == FILE_TYPE_CHAR; return dwType == FILE_TYPE_CHAR;
#else #else
return IsStdOutTTY(STDOUT_FILENO); return false;
//return IsStdOutTTY(STDOUT_FILENO);
#endif #endif
} }
@ -876,6 +896,7 @@ namespace Aurora::Console::ConsoleStd
if (isNotPipe) if (isNotPipe)
{ {
#if defined(AURORA_PLATFORM_WIN32)
// Get current console flags // Get current console flags
if (GetConsoleMode(gOutputStream, &dwMode)) if (GetConsoleMode(gOutputStream, &dwMode))
{ {
@ -915,15 +936,20 @@ namespace Aurora::Console::ConsoleStd
#endif #endif
} }
#endif
} }
if (gRuntimeConfig.console.enableStdPassthrough && gRuntimeConfig.console.enableStdIn && isNotPipe) if (gRuntimeConfig.console.enableStdPassthrough && gRuntimeConfig.console.enableStdIn && isNotPipe)
{ {
#if defined(AURORA_PLATFORM_WIN32)
if (!GetConsoleMode(gInputStream, &dwMode)) if (!GetConsoleMode(gInputStream, &dwMode))
{ {
ok = SetConsoleMode(gInputStream, dwMode & ~(ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT)); ok = SetConsoleMode(gInputStream, dwMode & ~(ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
SysAssert(ok, "Couldn't maintain binary stdin stream"); SysAssert(ok, "Couldn't maintain binary stdin stream");
} }
#endif
} }
//else //else
{ {

View File

@ -10,14 +10,22 @@
#include <Source/Console/ColorConvert.hpp> #include <Source/Console/ColorConvert.hpp>
#include <Source/Console/ConsoleStd/ConsoleStd.hpp> #include <Source/Console/ConsoleStd/ConsoleStd.hpp>
#if !defined(WINAPI_FAMILY_PARTITION) || \
WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM)
#define ENABLE_CONTTY
#endif
namespace Aurora::Console::ConsoleTTY namespace Aurora::Console::ConsoleTTY
{ {
static HANDLE gConsole {INVALID_HANDLE_VALUE}; static HANDLE gConsole { INVALID_HANDLE_VALUE };
static COORD gSavedCoord {};
static bool gIsRecording {}; #if defined(ENABLE_CONTTY)
static AuThreadPrimitives::Mutex gRecordLock; static COORD gSavedCoord {};
static AuList<AuFunction<void()>> gRecordedActions {}; #endif
static bool gIsRecording {};
static AuMutex gRecordLock;
static AuList<AuFunction<void()>> gRecordedActions;
struct Console struct Console
{ {
@ -49,6 +57,14 @@ namespace Aurora::Console::ConsoleTTY
AUKN_SYM void TTYClearLine(EAnsiColor bgColor) AUKN_SYM void TTYClearLine(EAnsiColor bgColor)
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleCursorPosition ||
!pGetConsoleScreenBufferInfo ||
!pFillConsoleOutputCharacterW)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYClearLine, bgColor); TTY_RECORD_FOR_FLIP(TTYClearLine, bgColor);
HANDLE hConsole; HANDLE hConsole;
@ -57,34 +73,43 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
if (!FillConsoleOutputCharacterW(hConsole, if (!pFillConsoleOutputCharacterW(hConsole,
L' ', L' ',
csbi.dwSize.X, csbi.dwSize.X,
COORD {0, csbi.dwCursorPosition.Y}, COORD {0, csbi.dwCursorPosition.Y},
&cCharsWritten)) &cCharsWritten))
{ {
return; return;
} }
if (!FillConsoleOutputAttribute(hConsole, if (!pFillConsoleOutputAttribute(hConsole,
bgColor == EAnsiColor::eEnumCount ? csbi.wAttributes : kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)], bgColor == EAnsiColor::eEnumCount ? csbi.wAttributes : kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)], // TODO: Why?
csbi.dwSize.X, csbi.dwSize.X,
COORD {0, csbi.dwCursorPosition.Y}, COORD {0, csbi.dwCursorPosition.Y},
&cCharsWritten)) &cCharsWritten))
{ {
return; return;
} }
SetConsoleCursorPosition(hConsole, {0, csbi.dwCursorPosition.Y}); pSetConsoleCursorPosition(hConsole, {0, csbi.dwCursorPosition.Y});
#endif
} }
AUKN_SYM void TTYClearScreen() AUKN_SYM void TTYClearScreen()
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo ||
!pFillConsoleOutputCharacterW ||
!pFillConsoleOutputAttribute)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYClearScreen); TTY_RECORD_FOR_FLIP(TTYClearScreen);
HANDLE hConsole; HANDLE hConsole;
@ -94,41 +119,48 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
dwConSize = csbi.dwSize.X * csbi.dwSize.Y; dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacterW(hConsole, if (!pFillConsoleOutputCharacterW(hConsole,
L' ', L' ',
dwConSize, dwConSize,
COORD {0, 0}, COORD {0, 0},
&cCharsWritten))
{
return;
}
if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
if (!pFillConsoleOutputAttribute(hConsole,
csbi.wAttributes,
dwConSize,
COORD {0, 0},
&cCharsWritten)) &cCharsWritten))
{ {
return; return;
} }
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) pSetConsoleCursorPosition(hConsole, {0, 0});
{ #endif
return;
}
if (!FillConsoleOutputAttribute(hConsole,
csbi.wAttributes,
dwConSize,
COORD {0, 0},
&cCharsWritten))
{
return;
}
SetConsoleCursorPosition(hConsole, {0, 0});
} }
AUKN_SYM void TTYFill(char character, EAnsiColor fgColor, EAnsiColor bgColor) AUKN_SYM void TTYFill(char character, EAnsiColor fgColor, EAnsiColor bgColor)
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYFill, character, fgColor, bgColor); TTY_RECORD_FOR_FLIP(TTYFill, character, fgColor, bgColor);
DWORD attrib {0}, cCharsWritten {}; DWORD attrib {0}, cCharsWritten {};
@ -137,7 +169,7 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
@ -156,26 +188,27 @@ namespace Aurora::Console::ConsoleTTY
attrib |= kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)]; attrib |= kAnsiColorBackgroundToNT[AuStaticCast<AuUInt>(bgColor)];
} }
if (!FillConsoleOutputCharacterW(hConsole, if (!pFillConsoleOutputCharacterW(hConsole,
L' ', L' ',
csbi.dwSize.X * csbi.dwSize.Y, csbi.dwSize.X * csbi.dwSize.Y,
COORD {0, 0}, COORD {0, 0},
&cCharsWritten)) &cCharsWritten))
{ {
return; return;
} }
if (attrib) if (attrib)
{ {
if (!FillConsoleOutputAttribute(hConsole, if (!pFillConsoleOutputAttribute(hConsole,
attrib, attrib,
csbi.dwSize.X * csbi.dwSize.Y, csbi.dwSize.X * csbi.dwSize.Y,
COORD {0, 0}, COORD {0, 0},
&cCharsWritten)) &cCharsWritten))
{ {
//return; //return;
} }
} }
#endif
} }
AUKN_SYM AuUInt32 TTYWrite(const void *buffer, AuUInt32 length) AUKN_SYM AuUInt32 TTYWrite(const void *buffer, AuUInt32 length)
@ -186,6 +219,13 @@ namespace Aurora::Console::ConsoleTTY
AUKN_SYM void TTYWrite(const char *string, EAnsiColor fgColor, EAnsiColor bgColor) AUKN_SYM void TTYWrite(const char *string, EAnsiColor fgColor, EAnsiColor bgColor)
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleTextAttribute)
{
ConsoleStd::WriteStdOutBlocking2(string, strlen(string));
return;
}
TTY_RECORD_FOR_FLIP((void(*)(const char *, EAnsiColor, EAnsiColor))(TTYWrite), string, fgColor, bgColor); TTY_RECORD_FOR_FLIP((void(*)(const char *, EAnsiColor, EAnsiColor))(TTYWrite), string, fgColor, bgColor);
DWORD attrib {}; DWORD attrib {};
@ -210,7 +250,7 @@ namespace Aurora::Console::ConsoleTTY
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
@ -218,57 +258,96 @@ namespace Aurora::Console::ConsoleTTY
attrib |= csbi.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); attrib |= csbi.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
} }
SetConsoleTextAttribute(hConsole, attrib); pSetConsoleTextAttribute(hConsole, attrib);
TTYWrite(string, AuUInt32(strlen(string))); TTYWrite(string, AuUInt32(strlen(string)));
SetConsoleTextAttribute(hConsole, FOREGROUND_WHITE); pSetConsoleTextAttribute(hConsole, FOREGROUND_WHITE);
#else
ConsoleStd::WriteStdOutBlocking2(string, strlen(string));
#endif
} }
AUKN_SYM void TTYReturnHome() AUKN_SYM void TTYReturnHome()
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYReturnHome); TTY_RECORD_FOR_FLIP(TTYReturnHome);
SetConsoleCursorPosition(GetTTYHandle(), {0, 0}); pSetConsoleCursorPosition(GetTTYHandle(), {0, 0});
#endif
} }
AUKN_SYM AuPair<AuUInt32, AuUInt32> TTYScreenSize() AUKN_SYM AuPair<AuUInt32, AuUInt32> TTYScreenSize()
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo)
{
return {};
}
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hConsole; HANDLE hConsole;
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return {}; return {};
} }
return AuMakePair(AuStaticCast<AuUInt32>(csbi.srWindow.Right - csbi.srWindow.Left + 1), return AuMakePair(AuStaticCast<AuUInt32>(csbi.srWindow.Right - csbi.srWindow.Left + 1),
AuStaticCast<AuUInt32>(csbi.srWindow.Bottom - csbi.srWindow.Top + 1)); AuStaticCast<AuUInt32>(csbi.srWindow.Bottom - csbi.srWindow.Top + 1));
#else
return {};
#endif
} }
AUKN_SYM void TTYStorePos() AUKN_SYM void TTYStorePos()
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYStorePos); TTY_RECORD_FOR_FLIP(TTYStorePos);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!pGetConsoleScreenBufferInfo(GetTTYHandle(), &csbi))
if (!GetConsoleScreenBufferInfo(GetTTYHandle(), &csbi))
{ {
return; return;
} }
gSavedCoord = csbi.dwCursorPosition; gSavedCoord = csbi.dwCursorPosition;
#endif
} }
AUKN_SYM void TTYRestorePos() AUKN_SYM void TTYRestorePos()
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYRestorePos); TTY_RECORD_FOR_FLIP(TTYRestorePos);
SetConsoleCursorPosition(GetTTYHandle(), gSavedCoord); pSetConsoleCursorPosition(GetTTYHandle(), gSavedCoord);
#endif
} }
AUKN_SYM void TTYMoveY(AuInt16 lines) AUKN_SYM void TTYMoveY(AuInt16 lines)
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo ||
!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYMoveY, lines); TTY_RECORD_FOR_FLIP(TTYMoveY, lines);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -276,17 +355,25 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
csbi.dwCursorPosition.Y += lines; csbi.dwCursorPosition.Y += lines;
SetConsoleCursorPosition(hConsole, gSavedCoord); pSetConsoleCursorPosition(hConsole, gSavedCoord);
#endif
} }
AUKN_SYM void TTYMoveX(AuInt16 lines) AUKN_SYM void TTYMoveX(AuInt16 lines)
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo ||
!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYMoveX, lines); TTY_RECORD_FOR_FLIP(TTYMoveX, lines);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -294,17 +381,25 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
csbi.dwCursorPosition.X += lines; csbi.dwCursorPosition.X += lines;
SetConsoleCursorPosition(hConsole, gSavedCoord); pSetConsoleCursorPosition(hConsole, gSavedCoord);
#endif
} }
AUKN_SYM void TTYSetY(AuUInt16 Y) AUKN_SYM void TTYSetY(AuUInt16 Y)
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo ||
!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYSetY, Y); TTY_RECORD_FOR_FLIP(TTYSetY, Y);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -312,17 +407,25 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
csbi.dwCursorPosition.Y = Y; csbi.dwCursorPosition.Y = Y;
SetConsoleCursorPosition(hConsole, gSavedCoord); pSetConsoleCursorPosition(hConsole, gSavedCoord);
#endif
} }
AUKN_SYM void TTYSetX(AuUInt16 X) AUKN_SYM void TTYSetX(AuUInt16 X)
{ {
#if defined(ENABLE_CONTTY)
if (!pGetConsoleScreenBufferInfo ||
!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYSetX, X); TTY_RECORD_FOR_FLIP(TTYSetX, X);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -330,24 +433,38 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
csbi.dwCursorPosition.X = X; csbi.dwCursorPosition.X = X;
SetConsoleCursorPosition(hConsole, gSavedCoord); pSetConsoleCursorPosition(hConsole, gSavedCoord);
#endif
} }
AUKN_SYM void TTYSetPos(AuPair<AuUInt32, AuUInt32> position) AUKN_SYM void TTYSetPos(AuPair<AuUInt32, AuUInt32> position)
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleCursorPosition)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYSetPos, position); TTY_RECORD_FOR_FLIP(TTYSetPos, position);
SetConsoleCursorPosition(GetTTYHandle(), COORD {AuStaticCast<short>(position.first), AuStaticCast<short>(position.second)}); pSetConsoleCursorPosition(GetTTYHandle(), COORD {AuStaticCast<short>(position.first), AuStaticCast<short>(position.second)});
#endif
} }
AUKN_SYM void TTYScrollBuffer(int Y) AUKN_SYM void TTYScrollBuffer(int Y)
{ {
#if defined(ENABLE_CONTTY)
if (!pScrollConsoleScreenBufferW)
{
return;
}
TTY_RECORD_FOR_FLIP(TTYScrollBuffer, Y); TTY_RECORD_FOR_FLIP(TTYScrollBuffer, Y);
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
@ -357,7 +474,7 @@ namespace Aurora::Console::ConsoleTTY
hConsole = GetTTYHandle(); hConsole = GetTTYHandle();
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) if (!pGetConsoleScreenBufferInfo(hConsole, &csbi))
{ {
return; return;
} }
@ -379,31 +496,41 @@ namespace Aurora::Console::ConsoleTTY
chiFill.Attributes = BACKGROUND_BLACK | FOREGROUND_WHITE; chiFill.Attributes = BACKGROUND_BLACK | FOREGROUND_WHITE;
chiFill.Char.UnicodeChar = ' '; chiFill.Char.UnicodeChar = ' ';
ScrollConsoleScreenBufferW( pScrollConsoleScreenBufferW(hConsole,
hConsole, &srctScrollRect,
&srctScrollRect, &srctClipRect,
&srctClipRect, coordDest,
coordDest, &chiFill);
&chiFill); #endif
} }
HANDLE GetTTYHandle() HANDLE GetTTYHandle()
{ {
#if defined(ENABLE_CONTTY)
if (gConsole != INVALID_HANDLE_VALUE) if (gConsole != INVALID_HANDLE_VALUE)
{ {
return gConsole; return gConsole;
} }
return gConsole = GetStdHandle(STD_OUTPUT_HANDLE); return gConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#else
return INVALID_HANDLE_VALUE;
#endif
} }
static bool AU_NOINLINE HasFBChanged(CONSOLE_SCREEN_BUFFER_INFO &csbi) static bool AU_NOINLINE HasFBChanged(CONSOLE_SCREEN_BUFFER_INFO &csbi)
{ {
#if defined(ENABLE_CONTTY)
auto &curConsole = gConsoles[(gConsoleIndex ) % 2]; auto &curConsole = gConsoles[(gConsoleIndex ) % 2];
if (!GetConsoleScreenBufferInfo(gConsoleHandle, &csbi)) if (!pGetConsoleScreenBufferInfo)
{ {
if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) return false;
}
if (!pGetConsoleScreenBufferInfo(gConsoleHandle, &csbi))
{
if (!pGetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{ {
} }
@ -422,11 +549,11 @@ namespace Aurora::Console::ConsoleTTY
SMALL_RECT screen = {0, 0, AuInt16(res.first - 1), AuInt16(res.second - 1)}; SMALL_RECT screen = {0, 0, AuInt16(res.first - 1), AuInt16(res.second - 1)};
SetConsoleScreenBufferSize(gConsoles[0].h, COORD {(AuInt16)res.first, (AuInt16)res.second}); pSetConsoleScreenBufferSize(gConsoles[0].h, COORD {(AuInt16)res.first, (AuInt16)res.second});
SetConsoleScreenBufferSize(gConsoles[1].h, COORD {(AuInt16)res.first, (AuInt16)res.second}); pSetConsoleScreenBufferSize(gConsoles[1].h, COORD {(AuInt16)res.first, (AuInt16)res.second});
SetConsoleWindowInfo(gConsoles[0].h, true, &screen); pSetConsoleWindowInfo(gConsoles[0].h, true, &screen);
SetConsoleWindowInfo(gConsoles[1].h, true, &screen); pSetConsoleWindowInfo(gConsoles[1].h, true, &screen);
return true; return true;
} }
@ -438,12 +565,22 @@ namespace Aurora::Console::ConsoleTTY
} }
return true; return true;
#else
return false;
#endif
} }
bool AU_NOINLINE InitConsoles() bool AU_NOINLINE InitConsoles()
{ {
#if defined(ENABLE_CONTTY)
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!pSetConsoleScreenBufferSize ||
!pSetConsoleWindowInfo)
{
return false;
}
if (!HasFBChanged(csbi)) if (!HasFBChanged(csbi))
{ {
return {}; return {};
@ -454,8 +591,8 @@ namespace Aurora::Console::ConsoleTTY
if (gConsoles[0].h == INVALID_HANDLE_VALUE) if (gConsoles[0].h == INVALID_HANDLE_VALUE)
{ {
auto a = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CONSOLE_TEXTMODE_BUFFER, nullptr); auto a = pCreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CONSOLE_TEXTMODE_BUFFER, nullptr);
auto b = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CONSOLE_TEXTMODE_BUFFER, nullptr); auto b = pCreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CONSOLE_TEXTMODE_BUFFER, nullptr);
gConsoles[0].h = a; gConsoles[0].h = a;
gConsoles[1].h = b; gConsoles[1].h = b;
@ -463,32 +600,48 @@ namespace Aurora::Console::ConsoleTTY
SMALL_RECT screen = {0, 0, AuInt16(res.first - 1), AuInt16(res.second - 1)}; SMALL_RECT screen = {0, 0, AuInt16(res.first - 1), AuInt16(res.second - 1)};
SetConsoleScreenBufferSize(gConsoles[0].h, COORD {(AuInt16)res.first, (AuInt16)res.second}); pSetConsoleScreenBufferSize(gConsoles[0].h, COORD {(AuInt16)res.first, (AuInt16)res.second});
SetConsoleScreenBufferSize(gConsoles[1].h, COORD {(AuInt16)res.first, (AuInt16)res.second}); pSetConsoleScreenBufferSize(gConsoles[1].h, COORD {(AuInt16)res.first, (AuInt16)res.second});
SetConsoleWindowInfo(gConsoles[0].h, true, &screen); pSetConsoleWindowInfo(gConsoles[0].h, true, &screen);
SetConsoleWindowInfo(gConsoles[1].h, true, &screen); pSetConsoleWindowInfo(gConsoles[1].h, true, &screen);
gConsoles[0].width = gConsoles[1].width = res.first; gConsoles[0].width = gConsoles[1].width = res.first;
gConsoles[0].height = gConsoles[1].height = res.second; gConsoles[0].height = gConsoles[1].height = res.second;
return true; return true;
#else
return false;
#endif
} }
bool WarmBuffering() bool WarmBuffering()
{ {
#if defined(ENABLE_CONTTY)
return InitConsoles(); return InitConsoles();
#else
return false;
#endif
} }
void BeginBuffering() void BeginBuffering()
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleActiveScreenBuffer)
{
return;
}
InitConsoles(); InitConsoles();
gConsoleIndex++; gConsoleIndex++;
gIsRecording = true; gIsRecording = true;
SetConsoleActiveScreenBuffer(gConsoleHandle = gConsoles[(gConsoleIndex + 1) % 2].h); pSetConsoleActiveScreenBuffer(gConsoleHandle = gConsoles[(gConsoleIndex + 1) % 2].h);
gConsole = gConsoles[gConsoleIndex % 2].h; gConsole = gConsoles[gConsoleIndex % 2].h;
#else
#endif
} }
void RecordFunction(const AuFunction<void()> &func) void RecordFunction(const AuFunction<void()> &func)
@ -512,6 +665,7 @@ namespace Aurora::Console::ConsoleTTY
static bool IdkMan() static bool IdkMan()
{ {
#if defined(ENABLE_CONTTY)
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
if (HasFBChanged(csbi)) if (HasFBChanged(csbi))
@ -521,20 +675,30 @@ namespace Aurora::Console::ConsoleTTY
} }
return true; return true;
#else
return false;
#endif
} }
bool EndBuffering() bool EndBuffering()
{ {
#if defined(ENABLE_CONTTY)
if (!pSetConsoleActiveScreenBuffer)
{
return false;
}
if (!IdkMan()) if (!IdkMan())
{ {
gIsRecording = false; gIsRecording = false;
return false; return false;
} }
SetConsoleActiveScreenBuffer(gConsoleHandle = gConsoles[(gConsoleIndex) % 2].h); pSetConsoleActiveScreenBuffer(gConsoleHandle = gConsoles[(gConsoleIndex) % 2].h);
gConsole = gConsoles[(gConsoleIndex + 1) % 2].h; gConsole = gConsoles[(gConsoleIndex + 1) % 2].h;
gIsRecording = false; gIsRecording = false;
RepeatRecord(); RepeatRecord();
#endif
return true; return true;
} }

View File

@ -340,7 +340,8 @@ namespace Aurora::Console::ConsoleTTY
gTTYConsoleEnabled = true; gTTYConsoleEnabled = true;
#if defined(AURORA_IS_MODERNNT_DERIVED) //#if defined(AURORA_IS_MODERNNT_DERIVED)
#if defined(AURORA_PLATFORM_WIN32)
this->oldCP = GetConsoleOutputCP(); this->oldCP = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
#endif #endif
@ -371,8 +372,12 @@ namespace Aurora::Console::ConsoleTTY
void TTYConsole::End() void TTYConsole::End()
{ {
#if defined(AURORA_IS_MODERNNT_DERIVED) //#if defined(AURORA_IS_MODERNNT_DERIVED)
SetConsoleActiveScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE)); #if defined(AURORA_PLATFORM_WIN32)
if (pSetConsoleActiveScreenBuffer)
{
pSetConsoleActiveScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
}
#endif #endif
if (!AuExchange(gTTYConsoleEnabled, false)) if (!AuExchange(gTTYConsoleEnabled, false))
@ -399,8 +404,9 @@ namespace Aurora::Console::ConsoleTTY
#if defined(AURORA_IS_POSIX_DERIVED) #if defined(AURORA_IS_POSIX_DERIVED)
TTYWrite("\033[?1049l"); TTYWrite("\033[?1049l");
#endif #endif
#if defined(AURORA_IS_MODERNNT_DERIVED) //#if defined(AURORA_IS_MODERNNT_DERIVED)
#if defined(AURORA_PLATFORM_WIN32)
SetConsoleOutputCP(this->oldCP); SetConsoleOutputCP(this->oldCP);
#endif #endif
@ -1445,8 +1451,8 @@ namespace Aurora::Console::ConsoleTTY
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
DWORD idc; DWORD idc;
auto line2 = AuLocale::ConvertFromUTF8(in); auto line2 = AuLocale::ConvertFromUTF8(in);
SetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(XOffset), AuStaticCast<short>(Y) }); pSetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(XOffset), AuStaticCast<short>(Y) });
WriteConsoleW(GetTTYHandle(), line2.data(), AuUInt32(line2.size()), &idc, NULL); pWriteConsoleW(GetTTYHandle(), line2.data(), AuUInt32(line2.size()), &idc, NULL);
#else #else
TTYSetPos({XOffset, Y}); TTYSetPos({XOffset, Y});
ConsoleStd::WriteStdOutBlocking2(in.data(), in.size()); ConsoleStd::WriteStdOutBlocking2(in.data(), in.size());
@ -1491,8 +1497,8 @@ namespace Aurora::Console::ConsoleTTY
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
DWORD idc; DWORD idc;
auto line2 = AuLocale::ConvertFromUTF8(in); auto line2 = AuLocale::ConvertFromUTF8(in);
SetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(pos.first), AuStaticCast<short>(pos.second) }); pSetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(pos.first), AuStaticCast<short>(pos.second) });
WriteConsoleW(GetTTYHandle(), line2.data(), AuUInt32(line2.size()), &idc, NULL); pWriteConsoleW(GetTTYHandle(), line2.data(), AuUInt32(line2.size()), &idc, NULL);
#else #else
TTYSetPos(pos); TTYSetPos(pos);
@ -1530,10 +1536,10 @@ namespace Aurora::Console::ConsoleTTY
} }
auto line2 = AuLocale::ConvertFromUTF8(in); auto line2 = AuLocale::ConvertFromUTF8(in);
SetConsoleTextAttribute(hConsole, attrib); pSetConsoleTextAttribute(hConsole, attrib);
SetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(pos.first), AuStaticCast<short>(pos.second) }); pSetConsoleCursorPosition(GetTTYHandle(), COORD { AuStaticCast<short>(pos.first), AuStaticCast<short>(pos.second) });
WriteConsoleW(hConsole, line2.data(), AuUInt32(line2.size()), &idc, NULL); pWriteConsoleW(hConsole, line2.data(), AuUInt32(line2.size()), &idc, NULL);
SetConsoleTextAttribute(hConsole, FOREGROUND_WHITE); pSetConsoleTextAttribute(hConsole, FOREGROUND_WHITE);
} }
//WriteConsoleA(GetTTYHandle(), in.data(), AuUInt32(in.size()), &idc, NULL); //WriteConsoleA(GetTTYHandle(), in.data(), AuUInt32(in.size()), &idc, NULL);