[CONSRV]
[reactos.git] / win32ss / user / consrv / tuiconsole.c
index 4c3f2f3..db1a6c0 100644 (file)
@@ -1,8 +1,9 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS system libraries
+ * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/tuiconsole.c
- * PURPOSE:         Implementation of text-mode consoles
+ * PURPOSE:         Interface to text-mode consoles
+ * PROGRAMMERS:
  */
 
 #include "consrv.h"
 #define NDEBUG
 #include <debug.h>
 
+
+/* TUI Console Window Class name */
+#define TUI_CONSOLE_WINDOW_CLASS L"TuiConsoleWindowClass"
+
+
 CRITICAL_SECTION ActiveConsoleLock;
 static COORD PhysicalConsoleSize;
 static HANDLE ConsoleDeviceHandle;
-static PCSRSS_CONSOLE ActiveConsole;
+static PCONSOLE ActiveConsole;
 
 static BOOL ConsInitialized = FALSE;
 
@@ -68,6 +74,7 @@ TuiInit(DWORD OemCP)
     CONSOLE_SCREEN_BUFFER_INFO ScrInfo;
     DWORD BytesReturned;
     WNDCLASSEXW wc;
+    ATOM ConsoleClassAtom;
     USHORT TextAttribute = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
 
     TuiStartService(L"Blue");
@@ -97,8 +104,8 @@ TuiInit(DWORD OemCP)
 
     ActiveConsole = NULL;
     InitializeCriticalSection(&ActiveConsoleLock);
-    if (! DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO,
-                          NULL, 0, &ScrInfo, sizeof(ScrInfo), &BytesReturned, NULL))
+    if (!DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO,
+                         NULL, 0, &ScrInfo, sizeof(ScrInfo), &BytesReturned, NULL))
     {
         DPRINT1("Failed to get console info\n");
         return FALSE;
@@ -107,26 +114,33 @@ TuiInit(DWORD OemCP)
 
     RtlZeroMemory(&wc, sizeof(WNDCLASSEXW));
     wc.cbSize = sizeof(WNDCLASSEXW);
-    wc.lpszClassName = L"TuiConsoleWindowClass";
+    wc.lpszClassName = TUI_CONSOLE_WINDOW_CLASS;
     wc.lpfnWndProc = TuiConsoleWndProc;
-    wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
-    if (RegisterClassExW(&wc) == 0)
+    wc.cbWndExtra = GWLP_CONSOLEWND_ALLOC;
+    wc.hInstance = (HINSTANCE)GetModuleHandleW(NULL);
+
+    ConsoleClassAtom = RegisterClassExW(&wc);
+    if (ConsoleClassAtom == 0)
     {
-        DPRINT1("Failed to register console wndproc\n");
+        DPRINT1("Failed to register TUI console wndproc\n");
         return FALSE;
     }
+    else
+    {
+        NtUserConsoleControl(TuiConsoleWndClassAtom, &ConsoleClassAtom, sizeof(ATOM));
+    }
 
     return TRUE;
 }
 
 static VOID WINAPI
-TuiInitScreenBuffer(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buffer)
+TuiInitScreenBuffer(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buffer)
 {
     Buffer->DefaultAttrib = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
 }
 
 static void FASTCALL
-TuiCopyRect(char *Dest, PCSRSS_SCREEN_BUFFER Buff, SMALL_RECT *Region)
+TuiCopyRect(char *Dest, PCONSOLE_SCREEN_BUFFER Buff, SMALL_RECT *Region)
 {
     UINT SrcDelta, DestDelta;
     LONG i;
@@ -149,10 +163,10 @@ TuiCopyRect(char *Dest, PCSRSS_SCREEN_BUFFER Buff, SMALL_RECT *Region)
 }
 
 static VOID WINAPI
-TuiDrawRegion(PCSRSS_CONSOLE Console, SMALL_RECT *Region)
+TuiDrawRegion(PCONSOLE Console, SMALL_RECT *Region)
 {
     DWORD BytesReturned;
-    PCSRSS_SCREEN_BUFFER Buff = Console->ActiveBuffer;
+    PCONSOLE_SCREEN_BUFFER Buff = Console->ActiveBuffer;
     PCONSOLE_DRAW ConsoleDraw;
     UINT ConsoleDrawSize;
 
@@ -178,8 +192,8 @@ TuiDrawRegion(PCSRSS_CONSOLE Console, SMALL_RECT *Region)
 
     TuiCopyRect((char *) (ConsoleDraw + 1), Buff, Region);
 
-    if (! DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_DRAW,
-                          NULL, 0, ConsoleDraw, ConsoleDrawSize, &BytesReturned, NULL))
+    if (!DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_DRAW,
+                         NULL, 0, ConsoleDraw, ConsoleDrawSize, &BytesReturned, NULL))
     {
         DPRINT1("Failed to draw console\n");
         HeapFree(ConSrvHeap, 0, ConsoleDraw);
@@ -190,11 +204,11 @@ TuiDrawRegion(PCSRSS_CONSOLE Console, SMALL_RECT *Region)
 }
 
 static VOID WINAPI
-TuiWriteStream(PCSRSS_CONSOLE Console, SMALL_RECT *Region, LONG CursorStartX, LONG CursorStartY,
+TuiWriteStream(PCONSOLE Console, SMALL_RECT *Region, LONG CursorStartX, LONG CursorStartY,
                UINT ScrolledLines, CHAR *Buffer, UINT Length)
 {
     DWORD BytesWritten;
-    PCSRSS_SCREEN_BUFFER Buff = Console->ActiveBuffer;
+    PCONSOLE_SCREEN_BUFFER Buff = Console->ActiveBuffer;
 
     if (ActiveConsole->ActiveBuffer != Buff)
     {
@@ -208,7 +222,7 @@ TuiWriteStream(PCSRSS_CONSOLE Console, SMALL_RECT *Region, LONG CursorStartX, LO
 }
 
 static BOOL WINAPI
-TuiSetCursorInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
+TuiSetCursorInfo(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buff)
 {
     CONSOLE_CURSOR_INFO Info;
     DWORD BytesReturned;
@@ -221,8 +235,8 @@ TuiSetCursorInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
     Info.dwSize = ConioEffectiveCursorSize(Console, 100);
     Info.bVisible = Buff->CursorInfo.bVisible;
 
-    if (! DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_SET_CURSOR_INFO,
-                          &Info, sizeof(Info), NULL, 0, &BytesReturned, NULL))
+    if (!DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_SET_CURSOR_INFO,
+                         &Info, sizeof(Info), NULL, 0, &BytesReturned, NULL))
     {
         DPRINT1( "Failed to set cursor info\n" );
         return FALSE;
@@ -232,7 +246,7 @@ TuiSetCursorInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
 }
 
 static BOOL WINAPI
-TuiSetScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff, UINT OldCursorX, UINT OldCursorY)
+TuiSetScreenInfo(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buff, UINT OldCursorX, UINT OldCursorY)
 {
     CONSOLE_SCREEN_BUFFER_INFO Info;
     DWORD BytesReturned;
@@ -246,9 +260,9 @@ TuiSetScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff, UINT OldCurs
     Info.dwCursorPosition.Y = Buff->CurrentY;
     Info.wAttributes = Buff->DefaultAttrib;
 
-    if (! DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO,
-                          &Info, sizeof(CONSOLE_SCREEN_BUFFER_INFO), NULL, 0,
-                          &BytesReturned, NULL))
+    if (!DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO,
+                         &Info, sizeof(CONSOLE_SCREEN_BUFFER_INFO), NULL, 0,
+                         &BytesReturned, NULL))
     {
         DPRINT1( "Failed to set cursor position\n" );
         return FALSE;
@@ -258,19 +272,19 @@ TuiSetScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff, UINT OldCurs
 }
 
 static BOOL WINAPI
-TuiUpdateScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
+TuiUpdateScreenInfo(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buff)
 {
     return TRUE;
 }
 
 static BOOL WINAPI
-TuiChangeTitle(PCSRSS_CONSOLE Console)
+TuiChangeTitle(PCONSOLE Console)
 {
     return TRUE;
 }
 
 static VOID WINAPI
-TuiCleanupConsole(PCSRSS_CONSOLE Console)
+TuiCleanupConsole(PCONSOLE Console)
 {
     DestroyWindow(Console->hWindow);
 
@@ -296,38 +310,39 @@ TuiCleanupConsole(PCSRSS_CONSOLE Console)
 }
 
 static BOOL WINAPI
-TuiChangeIcon(PCSRSS_CONSOLE Console, HICON hWindowIcon)
+TuiChangeIcon(PCONSOLE Console, HICON hWindowIcon)
 {
   return TRUE;
 }
 
 static NTSTATUS WINAPI
-TuiResizeBuffer(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER ScreenBuffer, COORD Size)
+TuiResizeBuffer(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER ScreenBuffer, COORD Size)
 {
   UNIMPLEMENTED;
   return STATUS_NOT_IMPLEMENTED;
 }
 
 DWORD WINAPI
-TuiConsoleThread (PVOID Data)
+TuiConsoleThread(PVOID Data)
 {
-    PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) Data;
+    PCONSOLE Console = (PCONSOLE) Data;
     HWND NewWindow;
     MSG msg;
 
-    NewWindow = CreateWindowW(L"TuiConsoleWindowClass",
+    NewWindow = CreateWindowW(TUI_CONSOLE_WINDOW_CLASS,
                               Console->Title.Buffer,
                               0,
                               -32000, -32000, 0, 0,
                               NULL, NULL,
-                              (HINSTANCE) GetModuleHandleW(NULL),
-                              (PVOID) Console);
-    Console->hWindow = NewWindow;
+                              (HINSTANCE)GetModuleHandleW(NULL),
+                              (PVOID)Console);
     if (NULL == NewWindow)
     {
         DPRINT1("CSR: Unable to create console window\n");
         return 1;
     }
+    Console->hWindow = NewWindow;
+    SetConsoleWndConsoleLeaderCID(Console);
 
     SetForegroundWindow(Console->hWindow);
 
@@ -348,7 +363,7 @@ TuiConsoleThread (PVOID Data)
     return 0;
 }
 
-static CSRSS_CONSOLE_VTBL TuiVtbl =
+static CONSOLE_VTBL TuiVtbl =
 {
     TuiInitScreenBuffer,
     TuiWriteStream,
@@ -363,14 +378,14 @@ static CSRSS_CONSOLE_VTBL TuiVtbl =
 };
 
 NTSTATUS FASTCALL
-TuiInitConsole(PCSRSS_CONSOLE Console)
+TuiInitConsole(PCONSOLE Console)
 {
     HANDLE ThreadHandle;
 
-    if (! ConsInitialized)
+    if (!ConsInitialized)
     {
         ConsInitialized = TRUE;
-        if (! TuiInit(Console->CodePage))
+        if (!TuiInit(Console->CodePage))
         {
             ConsInitialized = FALSE;
             return STATUS_UNSUCCESSFUL;
@@ -383,8 +398,12 @@ TuiInitConsole(PCSRSS_CONSOLE Console)
     Console->ActiveBuffer->MaxX = PhysicalConsoleSize.X;
     Console->ActiveBuffer->MaxY = PhysicalConsoleSize.Y;
 
-    ThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) TuiConsoleThread,
-                                Console, 0, NULL);
+    ThreadHandle = CreateThread(NULL,
+                                0,
+                                TuiConsoleThread,
+                                (PVOID)Console,
+                                0,
+                                NULL);
     if (NULL == ThreadHandle)
     {
         DPRINT1("CSR: Unable to create console thread\n");
@@ -411,7 +430,7 @@ TuiInitConsole(PCSRSS_CONSOLE Console)
     return STATUS_SUCCESS;
 }
 
-PCSRSS_CONSOLE FASTCALL
+PCONSOLE FASTCALL
 TuiGetFocusConsole(VOID)
 {
     return ActiveConsole;
@@ -420,7 +439,7 @@ TuiGetFocusConsole(VOID)
 BOOL FASTCALL
 TuiSwapConsole(int Next)
 {
-    static PCSRSS_CONSOLE SwapConsole = NULL; /* console we are thinking about swapping with */
+    static PCONSOLE SwapConsole = NULL; /* console we are thinking about swapping with */
     DWORD BytesReturned;
     ANSI_STRING Title;
     void * Buffer;
@@ -450,9 +469,9 @@ TuiSwapConsole(int Next)
         pos->X = (PhysicalConsoleSize.X - Title.Length) / 2;
         /* redraw the console to clear off old title */
         ConioDrawConsole(ActiveConsole);
-        if (! DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER,
-                              NULL, 0, Buffer, sizeof(COORD) + Title.Length,
-                              &BytesReturned, NULL))
+        if (!DeviceIoControl(ConsoleDeviceHandle, IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER,
+                             NULL, 0, Buffer, sizeof(COORD) + Title.Length,
+                             &BytesReturned, NULL))
         {
             DPRINT1( "Error writing to console\n" );
         }