msvcrt: Lazily initialize console handles.

Signed-off-by: Rémi Bernon <rbernon@codeweavers.com>
Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Rémi Bernon 2021-03-12 14:02:39 +01:00 committed by Alexandre Julliard
parent ae5cd00fcc
commit d5ab1401c0
3 changed files with 44 additions and 36 deletions

View File

@ -33,25 +33,35 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
#define LOCK_CONSOLE _lock(_CONIO_LOCK)
#define UNLOCK_CONSOLE _unlock(_CONIO_LOCK)
static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
static HANDLE MSVCRT_console_in;
static HANDLE MSVCRT_console_out;
static int __MSVCRT_console_buffer = EOF;
static wchar_t __MSVCRT_console_buffer_w = WEOF;
/* INTERNAL: Initialise console handles */
void msvcrt_init_console(void)
/* INTERNAL: Initialise console handles, _CONIO_LOCK must be held */
static HANDLE msvcrt_input_console(void)
{
TRACE(":Opening console handles\n");
if (!MSVCRT_console_in)
{
MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (MSVCRT_console_in == INVALID_HANDLE_VALUE)
WARN("Input console handle initialization failed!\n");
}
return MSVCRT_console_in;
}
if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
(MSVCRT_console_out== INVALID_HANDLE_VALUE))
WARN(":Console handle Initialisation FAILED!\n");
static HANDLE msvcrt_output_console(void)
{
if (!MSVCRT_console_out)
{
MSVCRT_console_out = CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (MSVCRT_console_out == INVALID_HANDLE_VALUE)
WARN("Output console handle initialization failed!\n");
}
return MSVCRT_console_out;
}
/* INTERNAL: Free console handles */
@ -74,7 +84,7 @@ int CDECL _cputs(const char* str)
len = strlen(str);
LOCK_CONSOLE;
if (WriteConsoleA(MSVCRT_console_out, str, len, &count, NULL)
if (WriteConsoleA(msvcrt_output_console(), str, len, &count, NULL)
&& count == len)
retval = 0;
UNLOCK_CONSOLE;
@ -93,7 +103,7 @@ int CDECL _cputws(const wchar_t* str)
len = wcslen(str);
LOCK_CONSOLE;
if (WriteConsoleW(MSVCRT_console_out, str, len, &count, NULL)
if (WriteConsoleW(msvcrt_output_console(), str, len, &count, NULL)
&& count == len)
retval = 0;
UNLOCK_CONSOLE;
@ -166,12 +176,12 @@ int CDECL _getch_nolock(void)
DWORD count;
DWORD mode = 0;
GetConsoleMode(MSVCRT_console_in, &mode);
GetConsoleMode(msvcrt_input_console(), &mode);
if(mode)
SetConsoleMode(MSVCRT_console_in, 0);
SetConsoleMode(msvcrt_input_console(), 0);
do {
if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
if (ReadConsoleInputA(msvcrt_input_console(), &ir, 1, &count))
{
/* Only interested in ASCII chars */
if (ir.EventType == KEY_EVENT &&
@ -197,7 +207,7 @@ int CDECL _getch_nolock(void)
break;
} while(1);
if (mode)
SetConsoleMode(MSVCRT_console_in, mode);
SetConsoleMode(msvcrt_input_console(), mode);
}
return retval;
}
@ -233,12 +243,12 @@ wchar_t CDECL _getwch_nolock(void)
DWORD count;
DWORD mode = 0;
GetConsoleMode(MSVCRT_console_in, &mode);
GetConsoleMode(msvcrt_input_console(), &mode);
if(mode)
SetConsoleMode(MSVCRT_console_in, 0);
SetConsoleMode(msvcrt_input_console(), 0);
do {
if (ReadConsoleInputW(MSVCRT_console_in, &ir, 1, &count))
if (ReadConsoleInputW(msvcrt_input_console(), &ir, 1, &count))
{
/* Only interested in ASCII chars */
if (ir.EventType == KEY_EVENT &&
@ -264,7 +274,7 @@ wchar_t CDECL _getwch_nolock(void)
break;
} while(1);
if (mode)
SetConsoleMode(MSVCRT_console_in, mode);
SetConsoleMode(msvcrt_input_console(), mode);
}
return retval;
}
@ -288,7 +298,7 @@ wchar_t CDECL _getwch(void)
int CDECL _putch_nolock(int c)
{
DWORD count;
if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
if (WriteConsoleA(msvcrt_output_console(), &c, 1, &count, NULL) && count == 1)
return c;
return EOF;
}
@ -310,7 +320,7 @@ int CDECL _putch(int c)
wchar_t CDECL _putwch_nolock(wchar_t c)
{
DWORD count;
if (WriteConsoleW(MSVCRT_console_out, &c, 1, &count, NULL) && count==1)
if (WriteConsoleW(msvcrt_output_console(), &c, 1, &count, NULL) && count==1)
return c;
return WEOF;
}
@ -388,10 +398,10 @@ char* CDECL _cgets(char* str)
TRACE("(%p)\n", str);
str[1] = 0; /* Length */
LOCK_CONSOLE;
GetConsoleMode(MSVCRT_console_in, &conmode);
SetConsoleMode(MSVCRT_console_in, ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
GetConsoleMode(msvcrt_input_console(), &conmode);
SetConsoleMode(msvcrt_input_console(), ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
if(ReadConsoleA(MSVCRT_console_in, buf, str[0], &got, NULL)) {
if(ReadConsoleA(msvcrt_input_console(), buf, str[0], &got, NULL)) {
if(buf[got-2] == '\r') {
buf[got-2] = 0;
str[1] = got-2;
@ -409,7 +419,7 @@ char* CDECL _cgets(char* str)
}
else
buf = NULL;
SetConsoleMode(MSVCRT_console_in, conmode);
SetConsoleMode(msvcrt_input_console(), conmode);
UNLOCK_CONSOLE;
return buf;
}
@ -474,10 +484,10 @@ int CDECL _kbhit(void)
INPUT_RECORD *ir = NULL;
DWORD count = 0, i;
GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
GetNumberOfConsoleInputEvents(msvcrt_input_console(), &count);
if (count && (ir = malloc(count * sizeof(INPUT_RECORD))) &&
PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
PeekConsoleInputA(msvcrt_input_console(), ir, count, &count))
for(i = 0; i < count - 1; i++)
{
if (ir[i].EventType == KEY_EVENT &&
@ -497,7 +507,7 @@ int CDECL _kbhit(void)
static int puts_clbk_console_a(void *ctx, int len, const char *str)
{
LOCK_CONSOLE;
if(!WriteConsoleA(MSVCRT_console_out, str, len, NULL, NULL))
if(!WriteConsoleA(msvcrt_output_console(), str, len, NULL, NULL))
len = -1;
UNLOCK_CONSOLE;
return len;
@ -506,7 +516,7 @@ static int puts_clbk_console_a(void *ctx, int len, const char *str)
static int puts_clbk_console_w(void *ctx, int len, const wchar_t *str)
{
LOCK_CONSOLE;
if(!WriteConsoleW(MSVCRT_console_out, str, len, NULL, NULL))
if(!WriteConsoleW(msvcrt_output_console(), str, len, NULL, NULL))
len = -1;
UNLOCK_CONSOLE;
return len;

View File

@ -113,7 +113,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
}
msvcrt_init_math(hinstDLL);
msvcrt_init_io();
msvcrt_init_console();
msvcrt_init_args();
msvcrt_init_signals();
#if _MSVCR_VER >= 100 && _MSVCR_VER <= 120

View File

@ -232,7 +232,6 @@ extern BOOL msvcrt_init_locale(void) DECLSPEC_HIDDEN;
extern void msvcrt_init_math(void*) DECLSPEC_HIDDEN;
extern void msvcrt_init_io(void) DECLSPEC_HIDDEN;
extern void msvcrt_free_io(void) DECLSPEC_HIDDEN;
extern void msvcrt_init_console(void) DECLSPEC_HIDDEN;
extern void msvcrt_free_console(void) DECLSPEC_HIDDEN;
extern void msvcrt_init_args(void) DECLSPEC_HIDDEN;
extern void msvcrt_free_args(void) DECLSPEC_HIDDEN;