mirror of
https://github.com/love2d/love.git
synced 2026-08-19 20:19:42 +02:00
If love is run from an existing console window in Windows, it will use that for stdout (when console mode is enabled via love.conf or --console) rather than creating a new console.
This commit is contained in:
+48
-15
@@ -250,44 +250,77 @@ int luaopen_love(lua_State * L)
|
|||||||
|
|
||||||
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
|
#ifdef LOVE_LEGENDARY_CONSOLE_IO_HACK
|
||||||
|
|
||||||
|
// Mostly taken from the Windows 8.1 SDK's VersionHelpers.h.
|
||||||
|
static bool IsWindowsVistaOrGreater()
|
||||||
|
{
|
||||||
|
OSVERSIONINFOEXW osvi = {sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0};
|
||||||
|
|
||||||
|
osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_VISTA);
|
||||||
|
osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_VISTA);
|
||||||
|
osvi.wServicePackMajor = 0;
|
||||||
|
|
||||||
|
DWORDLONG majorversionmask = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
|
||||||
|
DWORDLONG versionmask = VerSetConditionMask(majorversionmask, VER_MINORVERSION, VER_GREATER_EQUAL);
|
||||||
|
DWORDLONG mask = VerSetConditionMask(versionmask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
|
||||||
|
|
||||||
|
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, mask) != FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
int w__openConsole(lua_State *L)
|
int w__openConsole(lua_State *L)
|
||||||
{
|
{
|
||||||
static bool is_open = false;
|
static bool is_open = false;
|
||||||
|
|
||||||
if (is_open)
|
if (is_open)
|
||||||
return 0;
|
{
|
||||||
|
love::luax_pushboolean(L, is_open);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
is_open = true;
|
is_open = true;
|
||||||
|
|
||||||
if (GetConsoleWindow() != NULL || AllocConsole() == 0)
|
// FIXME: we don't call AttachConsole in Windows XP because it seems to
|
||||||
return 0;
|
// break later AllocConsole calls if it fails. A better workaround might be
|
||||||
|
// possible, but it's hard to find a WinXP system to test on these days...
|
||||||
|
if (!IsWindowsVistaOrGreater() || !AttachConsole(ATTACH_PARENT_PROCESS))
|
||||||
|
{
|
||||||
|
// Create our own console if we can't attach to an existing one.
|
||||||
|
if (!AllocConsole())
|
||||||
|
{
|
||||||
|
is_open = false;
|
||||||
|
love::luax_pushboolean(L, is_open);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
const int MAX_CONSOLE_LINES = 5000;
|
SetConsoleTitle(TEXT("LOVE Console"));
|
||||||
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
|
||||||
|
|
||||||
// Set size.
|
const int MAX_CONSOLE_LINES = 5000;
|
||||||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &console_info);
|
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
||||||
console_info.dwSize.Y = MAX_CONSOLE_LINES;
|
|
||||||
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), console_info.dwSize);
|
|
||||||
|
|
||||||
SetConsoleTitle(TEXT("LOVE Console"));
|
// Set size.
|
||||||
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &console_info);
|
||||||
|
console_info.dwSize.Y = MAX_CONSOLE_LINES;
|
||||||
|
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), console_info.dwSize);
|
||||||
|
}
|
||||||
|
|
||||||
FILE * fp;
|
FILE *fp;
|
||||||
|
|
||||||
// Redirect stdout.
|
// Redirect stdout.
|
||||||
fp = freopen("CONOUT$", "w", stdout);
|
fp = freopen("CONOUT$", "w", stdout);
|
||||||
if (L && fp == NULL)
|
if (L && fp == NULL)
|
||||||
luaL_error(L, "Console redirection of stdout failed.");
|
return luaL_error(L, "Console redirection of stdout failed.");
|
||||||
|
|
||||||
// Redirect stdin.
|
// Redirect stdin.
|
||||||
fp = freopen("CONIN$", "r", stdin);
|
fp = freopen("CONIN$", "r", stdin);
|
||||||
if (L && fp == NULL)
|
if (L && fp == NULL)
|
||||||
luaL_error(L, "Console redirection of stdin failed.");
|
return luaL_error(L, "Console redirection of stdin failed.");
|
||||||
|
|
||||||
// Redirect stderr.
|
// Redirect stderr.
|
||||||
fp = freopen("CONOUT$", "w", stderr);
|
fp = freopen("CONOUT$", "w", stderr);
|
||||||
if (L && fp == NULL)
|
if (L && fp == NULL)
|
||||||
luaL_error(L, "Console redirection of stderr failed.");
|
return luaL_error(L, "Console redirection of stderr failed.");
|
||||||
|
|
||||||
return 0;
|
love::luax_pushboolean(L, is_open);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
|
#endif // LOVE_LEGENDARY_CONSOLE_IO_HACK
|
||||||
|
|||||||
Reference in New Issue
Block a user