Commit d5ab1401 authored by Rémi Bernon's avatar Rémi Bernon Committed by Alexandre Julliard

msvcrt: Lazily initialize console handles.

parent ae5cd00f
...@@ -33,25 +33,35 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); ...@@ -33,25 +33,35 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
#define LOCK_CONSOLE _lock(_CONIO_LOCK) #define LOCK_CONSOLE _lock(_CONIO_LOCK)
#define UNLOCK_CONSOLE _unlock(_CONIO_LOCK) #define UNLOCK_CONSOLE _unlock(_CONIO_LOCK)
static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE; static HANDLE MSVCRT_console_in;
static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE; static HANDLE MSVCRT_console_out;
static int __MSVCRT_console_buffer = EOF; static int __MSVCRT_console_buffer = EOF;
static wchar_t __MSVCRT_console_buffer_w = WEOF; static wchar_t __MSVCRT_console_buffer_w = WEOF;
/* INTERNAL: Initialise console handles */ /* INTERNAL: Initialise console handles, _CONIO_LOCK must be held */
void msvcrt_init_console(void) static HANDLE msvcrt_input_console(void)
{ {
TRACE(":Opening console handles\n"); if (!MSVCRT_console_in)
{
MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_WRITE|GENERIC_READ, MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_WRITE|FILE_SHARE_READ, FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL); NULL, OPEN_EXISTING, 0, NULL);
MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, if (MSVCRT_console_in == INVALID_HANDLE_VALUE)
NULL, OPEN_EXISTING, 0, NULL); WARN("Input console handle initialization failed!\n");
}
return MSVCRT_console_in;
}
if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) || static HANDLE msvcrt_output_console(void)
(MSVCRT_console_out== INVALID_HANDLE_VALUE)) {
WARN(":Console handle Initialisation FAILED!\n"); 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 */ /* INTERNAL: Free console handles */
...@@ -74,7 +84,7 @@ int CDECL _cputs(const char* str) ...@@ -74,7 +84,7 @@ int CDECL _cputs(const char* str)
len = strlen(str); len = strlen(str);
LOCK_CONSOLE; LOCK_CONSOLE;
if (WriteConsoleA(MSVCRT_console_out, str, len, &count, NULL) if (WriteConsoleA(msvcrt_output_console(), str, len, &count, NULL)
&& count == len) && count == len)
retval = 0; retval = 0;
UNLOCK_CONSOLE; UNLOCK_CONSOLE;
...@@ -93,7 +103,7 @@ int CDECL _cputws(const wchar_t* str) ...@@ -93,7 +103,7 @@ int CDECL _cputws(const wchar_t* str)
len = wcslen(str); len = wcslen(str);
LOCK_CONSOLE; LOCK_CONSOLE;
if (WriteConsoleW(MSVCRT_console_out, str, len, &count, NULL) if (WriteConsoleW(msvcrt_output_console(), str, len, &count, NULL)
&& count == len) && count == len)
retval = 0; retval = 0;
UNLOCK_CONSOLE; UNLOCK_CONSOLE;
...@@ -166,12 +176,12 @@ int CDECL _getch_nolock(void) ...@@ -166,12 +176,12 @@ int CDECL _getch_nolock(void)
DWORD count; DWORD count;
DWORD mode = 0; DWORD mode = 0;
GetConsoleMode(MSVCRT_console_in, &mode); GetConsoleMode(msvcrt_input_console(), &mode);
if(mode) if(mode)
SetConsoleMode(MSVCRT_console_in, 0); SetConsoleMode(msvcrt_input_console(), 0);
do { do {
if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count)) if (ReadConsoleInputA(msvcrt_input_console(), &ir, 1, &count))
{ {
/* Only interested in ASCII chars */ /* Only interested in ASCII chars */
if (ir.EventType == KEY_EVENT && if (ir.EventType == KEY_EVENT &&
...@@ -197,7 +207,7 @@ int CDECL _getch_nolock(void) ...@@ -197,7 +207,7 @@ int CDECL _getch_nolock(void)
break; break;
} while(1); } while(1);
if (mode) if (mode)
SetConsoleMode(MSVCRT_console_in, mode); SetConsoleMode(msvcrt_input_console(), mode);
} }
return retval; return retval;
} }
...@@ -233,12 +243,12 @@ wchar_t CDECL _getwch_nolock(void) ...@@ -233,12 +243,12 @@ wchar_t CDECL _getwch_nolock(void)
DWORD count; DWORD count;
DWORD mode = 0; DWORD mode = 0;
GetConsoleMode(MSVCRT_console_in, &mode); GetConsoleMode(msvcrt_input_console(), &mode);
if(mode) if(mode)
SetConsoleMode(MSVCRT_console_in, 0); SetConsoleMode(msvcrt_input_console(), 0);
do { do {
if (ReadConsoleInputW(MSVCRT_console_in, &ir, 1, &count)) if (ReadConsoleInputW(msvcrt_input_console(), &ir, 1, &count))
{ {
/* Only interested in ASCII chars */ /* Only interested in ASCII chars */
if (ir.EventType == KEY_EVENT && if (ir.EventType == KEY_EVENT &&
...@@ -264,7 +274,7 @@ wchar_t CDECL _getwch_nolock(void) ...@@ -264,7 +274,7 @@ wchar_t CDECL _getwch_nolock(void)
break; break;
} while(1); } while(1);
if (mode) if (mode)
SetConsoleMode(MSVCRT_console_in, mode); SetConsoleMode(msvcrt_input_console(), mode);
} }
return retval; return retval;
} }
...@@ -288,7 +298,7 @@ wchar_t CDECL _getwch(void) ...@@ -288,7 +298,7 @@ wchar_t CDECL _getwch(void)
int CDECL _putch_nolock(int c) int CDECL _putch_nolock(int c)
{ {
DWORD count; 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 c;
return EOF; return EOF;
} }
...@@ -310,7 +320,7 @@ int CDECL _putch(int c) ...@@ -310,7 +320,7 @@ int CDECL _putch(int c)
wchar_t CDECL _putwch_nolock(wchar_t c) wchar_t CDECL _putwch_nolock(wchar_t c)
{ {
DWORD count; 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 c;
return WEOF; return WEOF;
} }
...@@ -388,10 +398,10 @@ char* CDECL _cgets(char* str) ...@@ -388,10 +398,10 @@ char* CDECL _cgets(char* str)
TRACE("(%p)\n", str); TRACE("(%p)\n", str);
str[1] = 0; /* Length */ str[1] = 0; /* Length */
LOCK_CONSOLE; LOCK_CONSOLE;
GetConsoleMode(MSVCRT_console_in, &conmode); GetConsoleMode(msvcrt_input_console(), &conmode);
SetConsoleMode(MSVCRT_console_in, ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT); 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') { if(buf[got-2] == '\r') {
buf[got-2] = 0; buf[got-2] = 0;
str[1] = got-2; str[1] = got-2;
...@@ -409,7 +419,7 @@ char* CDECL _cgets(char* str) ...@@ -409,7 +419,7 @@ char* CDECL _cgets(char* str)
} }
else else
buf = NULL; buf = NULL;
SetConsoleMode(MSVCRT_console_in, conmode); SetConsoleMode(msvcrt_input_console(), conmode);
UNLOCK_CONSOLE; UNLOCK_CONSOLE;
return buf; return buf;
} }
...@@ -474,10 +484,10 @@ int CDECL _kbhit(void) ...@@ -474,10 +484,10 @@ int CDECL _kbhit(void)
INPUT_RECORD *ir = NULL; INPUT_RECORD *ir = NULL;
DWORD count = 0, i; DWORD count = 0, i;
GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count); GetNumberOfConsoleInputEvents(msvcrt_input_console(), &count);
if (count && (ir = malloc(count * sizeof(INPUT_RECORD))) && 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++) for(i = 0; i < count - 1; i++)
{ {
if (ir[i].EventType == KEY_EVENT && if (ir[i].EventType == KEY_EVENT &&
...@@ -497,7 +507,7 @@ int CDECL _kbhit(void) ...@@ -497,7 +507,7 @@ int CDECL _kbhit(void)
static int puts_clbk_console_a(void *ctx, int len, const char *str) static int puts_clbk_console_a(void *ctx, int len, const char *str)
{ {
LOCK_CONSOLE; LOCK_CONSOLE;
if(!WriteConsoleA(MSVCRT_console_out, str, len, NULL, NULL)) if(!WriteConsoleA(msvcrt_output_console(), str, len, NULL, NULL))
len = -1; len = -1;
UNLOCK_CONSOLE; UNLOCK_CONSOLE;
return len; return len;
...@@ -506,7 +516,7 @@ static int puts_clbk_console_a(void *ctx, int len, const char *str) ...@@ -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) static int puts_clbk_console_w(void *ctx, int len, const wchar_t *str)
{ {
LOCK_CONSOLE; LOCK_CONSOLE;
if(!WriteConsoleW(MSVCRT_console_out, str, len, NULL, NULL)) if(!WriteConsoleW(msvcrt_output_console(), str, len, NULL, NULL))
len = -1; len = -1;
UNLOCK_CONSOLE; UNLOCK_CONSOLE;
return len; return len;
......
...@@ -113,7 +113,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) ...@@ -113,7 +113,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
} }
msvcrt_init_math(hinstDLL); msvcrt_init_math(hinstDLL);
msvcrt_init_io(); msvcrt_init_io();
msvcrt_init_console();
msvcrt_init_args(); msvcrt_init_args();
msvcrt_init_signals(); msvcrt_init_signals();
#if _MSVCR_VER >= 100 && _MSVCR_VER <= 120 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
......
...@@ -232,7 +232,6 @@ extern BOOL msvcrt_init_locale(void) DECLSPEC_HIDDEN; ...@@ -232,7 +232,6 @@ extern BOOL msvcrt_init_locale(void) DECLSPEC_HIDDEN;
extern void msvcrt_init_math(void*) DECLSPEC_HIDDEN; extern void msvcrt_init_math(void*) DECLSPEC_HIDDEN;
extern void msvcrt_init_io(void) DECLSPEC_HIDDEN; extern void msvcrt_init_io(void) DECLSPEC_HIDDEN;
extern void msvcrt_free_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_free_console(void) DECLSPEC_HIDDEN;
extern void msvcrt_init_args(void) DECLSPEC_HIDDEN; extern void msvcrt_init_args(void) DECLSPEC_HIDDEN;
extern void msvcrt_free_args(void) DECLSPEC_HIDDEN; extern void msvcrt_free_args(void) DECLSPEC_HIDDEN;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment