[CONSRV]
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 7 Apr 2013 23:18:59 +0000 (23:18 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Sun, 7 Apr 2013 23:18:59 +0000 (23:18 +0000)
Code reorganization. Common functions declarations, shared between the console server and the frontends, are put inside a include/ subdirectory. Functions needed only for the console server are in the usual root directory. Functions used only for the frontends are in their corresponding directories in under frontends/.

svn path=/branches/ros-csrss/; revision=58717

27 files changed:
win32ss/user/consrv/alias.c
win32ss/user/consrv/alias.h
win32ss/user/consrv/api.h [new file with mode: 0644]
win32ss/user/consrv/coninput.c
win32ss/user/consrv/coninput.h [new file with mode: 0644]
win32ss/user/consrv/conio.h
win32ss/user/consrv/conoutput.c
win32ss/user/consrv/conoutput.h [new file with mode: 0644]
win32ss/user/consrv/console.c
win32ss/user/consrv/console.h
win32ss/user/consrv/consrv.h
win32ss/user/consrv/frontends/gui/consolecpl.h
win32ss/user/consrv/frontends/gui/guisettings.c
win32ss/user/consrv/frontends/gui/guisettings.h
win32ss/user/consrv/frontends/gui/guiterm.c
win32ss/user/consrv/frontends/gui/guiterm.h
win32ss/user/consrv/frontends/tui/tuiterm.c
win32ss/user/consrv/frontends/tui/tuiterm.h
win32ss/user/consrv/handle.c
win32ss/user/consrv/handle.h [new file with mode: 0644]
win32ss/user/consrv/include/conio.h [new file with mode: 0644]
win32ss/user/consrv/include/console.h [new file with mode: 0644]
win32ss/user/consrv/include/settings.h [moved from win32ss/user/consrv/settings.h with 93% similarity]
win32ss/user/consrv/init.c
win32ss/user/consrv/lineinput.c
win32ss/user/consrv/procinit.h [new file with mode: 0644]
win32ss/user/consrv/settings.c

index 95a23c8..6a410c6 100644 (file)
@@ -10,7 +10,8 @@
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
-#include "conio.h"
+#include "console.h"
+#include "include/conio.h"
 
 #define NDEBUG
 #include <debug.h>
@@ -254,12 +255,12 @@ IntDeleteAliasEntry(PALIAS_HEADER Header, PALIAS_ENTRY Entry)
 }
 
 VOID
-IntDeleteAllAliases(PALIAS_HEADER RootHeader)
+IntDeleteAllAliases(PCONSOLE Console)
 {
     PALIAS_HEADER Header, NextHeader;
     PALIAS_ENTRY Entry, NextEntry;
 
-    for (Header = RootHeader; Header; Header = NextHeader)
+    for (Header = Console->Aliases; Header; Header = NextHeader)
     {
         NextHeader = Header->Next;
         for (Entry = Header->Data; Entry; Entry = NextEntry)
index fc805f8..cc501d9 100644 (file)
@@ -9,6 +9,6 @@
 
 #pragma once
 
-VOID IntDeleteAllAliases(struct _ALIAS_HEADER *RootHeader);
+VOID IntDeleteAllAliases(PCONSOLE Console);
 
 /* EOF */
diff --git a/win32ss/user/consrv/api.h b/win32ss/user/consrv/api.h
new file mode 100644 (file)
index 0000000..f562ae2
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/api.h
+ * PURPOSE:         Public server APIs definitions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+/* alias.c */
+CSR_API(SrvAddConsoleAlias);
+CSR_API(SrvGetConsoleAlias);
+CSR_API(SrvGetConsoleAliases);
+CSR_API(SrvGetConsoleAliasesLength);
+CSR_API(SrvGetConsoleAliasExes);
+CSR_API(SrvGetConsoleAliasExesLength);
+
+/* coninput.c */
+CSR_API(SrvGetConsoleInput);
+CSR_API(SrvWriteConsoleInput);
+CSR_API(SrvReadConsole);
+CSR_API(SrvFlushConsoleInputBuffer);
+CSR_API(SrvGetConsoleNumberOfInputEvents);
+
+/* conoutput.c */
+CSR_API(SrvReadConsoleOutput);
+CSR_API(SrvWriteConsoleOutput);
+CSR_API(SrvReadConsoleOutputString);
+CSR_API(SrvWriteConsoleOutputString);
+CSR_API(SrvFillConsoleOutput);
+CSR_API(SrvWriteConsole);
+CSR_API(SrvSetConsoleCursorPosition);
+CSR_API(SrvGetConsoleCursorInfo);
+CSR_API(SrvSetConsoleCursorInfo);
+CSR_API(SrvSetConsoleTextAttribute);
+CSR_API(SrvCreateConsoleScreenBuffer);
+CSR_API(SrvGetConsoleScreenBufferInfo);
+CSR_API(SrvSetConsoleActiveScreenBuffer);
+CSR_API(SrvScrollConsoleScreenBuffer);
+CSR_API(SrvSetConsoleScreenBufferSize);
+
+/* console.c */
+CSR_API(SrvOpenConsole);
+CSR_API(SrvAllocConsole);
+CSR_API(SrvAttachConsole);
+CSR_API(SrvFreeConsole);
+CSR_API(SrvSetConsoleMode);
+CSR_API(SrvGetConsoleMode);
+CSR_API(SrvSetConsoleTitle);
+CSR_API(SrvGetConsoleTitle);
+CSR_API(SrvGetConsoleHardwareState);
+CSR_API(SrvSetConsoleHardwareState);
+CSR_API(SrvGetConsoleDisplayMode);
+CSR_API(SrvSetConsoleDisplayMode);
+CSR_API(SrvGetConsoleWindow);
+CSR_API(SrvSetConsoleIcon);
+CSR_API(SrvGetConsoleCP);
+CSR_API(SrvSetConsoleCP);
+CSR_API(SrvGetConsoleProcessList);
+CSR_API(SrvGenerateConsoleCtrlEvent);
+CSR_API(SrvGetConsoleSelectionInfo);
+
+/* handle.c */
+CSR_API(SrvCloseHandle);
+CSR_API(SrvVerifyConsoleIoHandle);
+CSR_API(SrvDuplicateHandle);
+
+/* lineinput.c */
+CSR_API(SrvGetConsoleCommandHistoryLength);
+CSR_API(SrvGetConsoleCommandHistory);
+CSR_API(SrvExpungeConsoleCommandHistory);
+CSR_API(SrvSetConsoleNumberOfCommands);
+CSR_API(SrvGetConsoleHistory);
+CSR_API(SrvSetConsoleHistory);
+
+/* EOF */
index 83ee837..4cf052b 100644 (file)
@@ -2,14 +2,16 @@
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/coninput.c
- * PURPOSE:         Console I/O functions
- * PROGRAMMERS:
+ * PURPOSE:         Console Input functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
+#include "include/conio.h"
 #include "conio.h"
+#include "handle.h"
 #include "lineinput.h"
 
 #define NDEBUG
 
 /* GLOBALS ********************************************************************/
 
+#define ConSrvGetInputBuffer(ProcessData, Handle, Ptr, Access, LockConsole)     \
+    ConSrvGetObject((ProcessData), (Handle), (PCONSOLE_IO_OBJECT*)(Ptr), NULL,  \
+                    (Access), (LockConsole), INPUT_BUFFER)
+#define ConSrvGetInputBufferAndHandleEntry(ProcessData, Handle, Ptr, Entry, Access, LockConsole)    \
+    ConSrvGetObject((ProcessData), (Handle), (PCONSOLE_IO_OBJECT*)(Ptr), (Entry),                   \
+                    (Access), (LockConsole), INPUT_BUFFER)
+#define ConSrvReleaseInputBuffer(Buff, IsConsoleLocked) \
+    ConSrvReleaseObject(&(Buff)->Header, (IsConsoleLocked))
+
+
 #define ConsoleInputUnicodeCharToAnsiChar(Console, dChar, sWChar) \
     WideCharToMultiByte((Console)->CodePage, 0, (sWChar), 1, (dChar), 1, NULL, NULL)
 
 #define ConsoleInputAnsiCharToUnicodeChar(Console, dWChar, sChar) \
     MultiByteToWideChar((Console)->CodePage, 0, (sChar), 1, (dWChar), 1)
 
+typedef struct ConsoleInput_t
+{
+    LIST_ENTRY ListEntry;
+    INPUT_RECORD InputEvent;
+} ConsoleInput;
 
 typedef struct _GET_INPUT_INFO
 {
     PCSR_THREAD           CallingThread;    // The thread which called the input API.
-    PCONSOLE_IO_HANDLE    HandleEntry;      // The handle data associated with the wait thread.
+    PVOID                 HandleEntry;      // The handle data associated with the wait thread.
     PCONSOLE_INPUT_BUFFER InputBuffer;      // The input buffer corresponding to the handle.
 } GET_INPUT_INFO, *PGET_INPUT_INFO;
 
@@ -101,6 +118,22 @@ ConioProcessInputEvent(PCONSOLE Console,
     return STATUS_SUCCESS;
 }
 
+VOID FASTCALL
+PurgeInputBuffer(PCONSOLE Console)
+{
+    PLIST_ENTRY CurrentEntry;
+    ConsoleInput* Event;
+
+    while (!IsListEmpty(&Console->InputBuffer.InputEvents))
+    {
+        CurrentEntry = RemoveHeadList(&Console->InputBuffer.InputEvents);
+        Event = CONTAINING_RECORD(CurrentEntry, ConsoleInput, ListEntry);
+        RtlFreeHeap(ConSrvHeap, 0, Event);
+    }
+
+    CloseHandle(Console->InputBuffer.ActiveEvent);
+}
+
 static DWORD FASTCALL
 ConioGetShiftState(PBYTE KeyState, LPARAM lParam)
 {
@@ -331,7 +364,7 @@ ReadInputBufferThread(IN PLIST_ENTRY WaitList,
     PCONSOLE_GETINPUT GetInputRequest = &((PCONSOLE_API_MESSAGE)WaitApiMessage)->Data.GetInputRequest;
     PGET_INPUT_INFO InputInfo = (PGET_INPUT_INFO)WaitContext;
 
-    PCONSOLE_IO_HANDLE InputHandle = (PCONSOLE_IO_HANDLE)WaitArgument2;
+    PVOID InputHandle = WaitArgument2;
 
     DPRINT1("ReadInputBufferThread - WaitContext = 0x%p, WaitArgument1 = 0x%p, WaitArgument2 = 0x%p, WaitFlags = %lu\n", WaitContext, WaitArgument1, WaitArgument2, WaitFlags);
 
@@ -464,7 +497,7 @@ ReadCharsThread(IN PLIST_ENTRY WaitList,
     NTSTATUS Status;
     PGET_INPUT_INFO InputInfo = (PGET_INPUT_INFO)WaitContext;
 
-    PCONSOLE_IO_HANDLE InputHandle = (PCONSOLE_IO_HANDLE)WaitArgument2;
+    PVOID InputHandle = WaitArgument2;
 
     DPRINT1("ReadCharsThread - WaitContext = 0x%p, WaitArgument1 = 0x%p, WaitArgument2 = 0x%p, WaitFlags = %lu\n", WaitContext, WaitArgument1, WaitArgument2, WaitFlags);
 
@@ -676,7 +709,7 @@ CSR_API(SrvReadConsole)
     NTSTATUS Status;
     PCONSOLE_READCONSOLE ReadConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ReadConsoleRequest;
     PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
-    PCONSOLE_IO_HANDLE HandleEntry;
+    PVOID HandleEntry;
     PCONSOLE_INPUT_BUFFER InputBuffer;
     GET_INPUT_INFO InputInfo;
 
@@ -721,7 +754,7 @@ CSR_API(SrvGetConsoleInput)
     NTSTATUS Status;
     PCONSOLE_GETINPUT GetInputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetInputRequest;
     PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
-    PCONSOLE_IO_HANDLE HandleEntry;
+    PVOID HandleEntry;
     PCONSOLE_INPUT_BUFFER InputBuffer;
     GET_INPUT_INFO InputInfo;
 
diff --git a/win32ss/user/consrv/coninput.h b/win32ss/user/consrv/coninput.h
new file mode 100644 (file)
index 0000000..effbcd3
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/coninput.h
+ * PURPOSE:         Console Input functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+VOID FASTCALL PurgeInputBuffer(PCONSOLE Console);
+
+/* EOF */
index 6be2e04..e9bbbe9 100644 (file)
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/conio.h
- * PURPOSE:         Internal console I/O interface
- * PROGRAMMERS:
+ * PURPOSE:         Internal Console I/O Interface
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #pragma once
 
-#define CSR_DEFAULT_CURSOR_SIZE 25
-#define CURSOR_BLINK_TIME 500
-
-/* Default attributes */
-#define DEFAULT_SCREEN_ATTRIB   (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
-#define DEFAULT_POPUP_ATTRIB    (FOREGROUND_BLUE | FOREGROUND_RED   | \
-                                 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)
-
-
-/************************************************************************
- * Screen buffer structure represents the win32 screen buffer object.   *
- * Internally, the portion of the buffer being shown CAN loop past the  *
- * bottom of the virtual buffer and wrap around to the top.  Win32 does *
- * not do this.  I decided to do this because it eliminates the need to *
- * do a massive memcpy() to scroll the contents of the buffer up to     *
- * scroll the screen on output, instead I just shift down the position  *
- * to be displayed, and let it wrap around to the top again.            *
- * The VirtualY member keeps track of the top Y coord that win32        *
- * clients THINK is currently being displayed, because they think that  *
- * when the display reaches the bottom of the buffer and another line   *
- * being printed causes another line to scroll down, that the buffer IS *
- * memcpy()'s up, and the bottom of the buffer is still displayed, but  *
- * internally, I just wrap back to the top of the buffer.               *
- ************************************************************************/
-
-typedef struct _CONSOLE_SCREEN_BUFFER
-{
-    Object_t Header;                /* Object header */
-    LIST_ENTRY ListEntry;           /* Entry in console's list of buffers */
-
-    BYTE *Buffer; /* CHAR_INFO */   /* Pointer to screen buffer */
-
-    COORD ScreenBufferSize;         /* Size of this screen buffer */
-    COORD CursorPosition;           /* Current cursor position */
-
-    USHORT ShowX, ShowY;            /* Beginning offset for the actual display area */
-    USHORT VirtualY;                /* Top row of buffer being displayed, reported to callers */
-
-    BOOLEAN CursorBlinkOn;
-    BOOLEAN ForceCursorOff;
-    ULONG   CursorSize;
-    CONSOLE_CURSOR_INFO CursorInfo; // FIXME: Keep this member or not ??
-
-    WORD ScreenDefaultAttrib;       /* Default screen char attribute */
-    WORD PopupDefaultAttrib;        /* Default popup char attribute */
-    USHORT Mode;
-    ULONG  DisplayMode;
-} CONSOLE_SCREEN_BUFFER, *PCONSOLE_SCREEN_BUFFER;
-
-typedef struct _CONSOLE_INPUT_BUFFER
-{
-    Object_t Header;                /* Object header */
-
-    ULONG InputBufferSize;          /* Size of this input buffer */
-    LIST_ENTRY InputEvents;         /* List head for input event queue */
-    HANDLE ActiveEvent;             /* Event set when an input event is added in its queue */
-    LIST_ENTRY ReadWaitQueue;       /* List head for the queue of read wait blocks */
-
-    USHORT Mode;                    /* Console Input Buffer mode flags */
-} CONSOLE_INPUT_BUFFER, *PCONSOLE_INPUT_BUFFER;
-
-typedef struct ConsoleInput_t
-{
-    LIST_ENTRY ListEntry;
-    INPUT_RECORD InputEvent;
-} ConsoleInput;
-
-typedef struct _FRONTEND_VTBL
-{
-    /*
-     * Internal interface (functions called by the console server only)
-     */
-    VOID (WINAPI *CleanupConsole)(struct _CONSOLE* Console);
-    VOID (WINAPI *WriteStream)(struct _CONSOLE* Console,
-                               SMALL_RECT* Block,
-                               LONG CursorStartX,
-                               LONG CursorStartY,
-                               UINT ScrolledLines,
-                               CHAR *Buffer,
-                               UINT Length);
-    VOID (WINAPI *DrawRegion)(struct _CONSOLE* Console,
-                              SMALL_RECT* Region);
-    BOOL (WINAPI *SetCursorInfo)(struct _CONSOLE* Console,
-                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer);
-    BOOL (WINAPI *SetScreenInfo)(struct _CONSOLE* Console,
-                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer,
-                                 UINT OldCursorX,
-                                 UINT OldCursorY);
-    BOOL (WINAPI *UpdateScreenInfo)(struct _CONSOLE* Console,
-                                    PCONSOLE_SCREEN_BUFFER ScreenBuffer);
-    NTSTATUS (WINAPI *ResizeBuffer)(struct _CONSOLE* Console,
-                                    PCONSOLE_SCREEN_BUFFER ScreenBuffer,
-                                    COORD Size);
-    VOID (WINAPI *ResizeTerminal)(struct _CONSOLE* Console);
-    BOOL (WINAPI *ProcessKeyCallback)(struct _CONSOLE* Console,
-                                      MSG* msg,
-                                      BYTE KeyStateMenu,
-                                      DWORD ShiftState,
-                                      UINT VirtualKeyCode,
-                                      BOOL Down);
-    VOID (WINAPI *RefreshInternalInfo)(struct _CONSOLE* Console);
-
-    /*
-     * External interface (functions corresponding to the Console API)
-     */
-    VOID (WINAPI *ChangeTitle)(struct _CONSOLE* Console);
-    BOOL (WINAPI *ChangeIcon)(struct _CONSOLE* Console,
-                              HICON hWindowIcon);
-    HWND (WINAPI *GetConsoleWindowHandle)(struct _CONSOLE* Console);
-
-} FRONTEND_VTBL, *PFRONTEND_VTBL;
-
+/* Macros used to call functions in the FRONTEND_VTBL virtual table */
 #define ConioDrawRegion(Console, Region) (Console)->TermIFace.Vtbl->DrawRegion((Console), (Region))
 #define ConioWriteStream(Console, Block, CurStartX, CurStartY, ScrolledLines, Buffer, Length) \
           (Console)->TermIFace.Vtbl->WriteStream((Console), (Block), (CurStartX), (CurStartY), \
@@ -141,134 +30,4 @@ typedef struct _FRONTEND_VTBL
 #define ConioRefreshInternalInfo(Console) \
           (Console)->TermIFace.Vtbl->RefreshInternalInfo((Console))
 
-typedef struct _FRONTEND_IFACE
-{
-    PFRONTEND_VTBL Vtbl;    /* Virtual table */
-    PVOID Data;             /* Private data  */
-    PVOID OldData;          /* Reserved      */
-} FRONTEND_IFACE, *PFRONTEND_IFACE;
-
-#if 0 // Temporarily put in consrv.h
-/*
- * WARNING: Change the state of the console ONLY when the console is locked !
- */
-typedef enum _CONSOLE_STATE
-{
-    CONSOLE_INITIALIZING,   /* Console is initializing */
-    CONSOLE_RUNNING     ,   /* Console running */
-    CONSOLE_TERMINATING ,   /* Console about to be destroyed (but still not) */
-    CONSOLE_IN_DESTRUCTION  /* Console in destruction */
-} CONSOLE_STATE, *PCONSOLE_STATE;
-#endif
-
-typedef struct _CONSOLE
-{
-    LONG ReferenceCount;                    /* Is incremented each time a handle to something in the console (a screen-buffer or the input buffer of this console) gets referenced */
-    CRITICAL_SECTION Lock;
-    CONSOLE_STATE State;                    /* State of the console */
-
-    // struct _CONSOLE *Prev, *Next;           /* Next and Prev consoles in console wheel */
-    LIST_ENTRY Entry;                       /* Entry in the list of consoles */
-    LIST_ENTRY ProcessList;                 /* List of processes owning the console. The first one is the so-called "Console Leader Process" */
-
-    FRONTEND_IFACE TermIFace;               /* Frontend-specific interface */
-
-/**************************** Input buffer and data ***************************/
-    CONSOLE_INPUT_BUFFER InputBuffer;       /* Input buffer of the console */
-
-    PWCHAR LineBuffer;                      /* Current line being input, in line buffered mode */
-    WORD LineMaxSize;                       /* Maximum size of line in characters (including CR+LF) */
-    WORD LineSize;                          /* Current size of line */
-    WORD LinePos;                           /* Current position within line */
-    BOOLEAN LineComplete;                   /* User pressed enter, ready to send back to client */
-    BOOLEAN LineUpPressed;
-    BOOLEAN LineInsertToggle;               /* Replace character over cursor instead of inserting */
-    ULONG LineWakeupMask;                   /* Bitmap of which control characters will end line input */
-
-    BOOLEAN QuickEdit;
-    BOOLEAN InsertMode;
-    UINT CodePage;
-    UINT OutputCodePage;
-
-    CONSOLE_SELECTION_INFO Selection;       /* Contains information about the selection */
-    COORD dwSelectionCursor;                /* Selection cursor position, most of the time different from Selection.dwSelectionAnchor */
-
-/******************************* Screen buffers *******************************/
-    LIST_ENTRY BufferList;                  /* List of all screen buffers for this console */
-    PCONSOLE_SCREEN_BUFFER ActiveBuffer;    /* Pointer to currently active screen buffer */
-    BYTE PauseFlags;
-    HANDLE UnpauseEvent;
-    LIST_ENTRY WriteWaitQueue;              /* List head for the queue of write wait blocks */
-
-    ULONG HardwareState;                    /* _GDI_MANAGED, _DIRECT */
-
-/**************************** Aliases and Histories ***************************/
-    struct _ALIAS_HEADER *Aliases;
-    LIST_ENTRY HistoryBuffers;
-    ULONG HistoryBufferSize;                /* Size for newly created history buffers */
-    ULONG NumberOfHistoryBuffers;           /* Maximum number of history buffers allowed */
-    BOOLEAN HistoryNoDup;                   /* Remove old duplicate history entries */
-
-/****************************** Other properties ******************************/
-    UNICODE_STRING OriginalTitle;           /* Original title of console, the one when the console leader is launched. Always NULL-terminated */
-    UNICODE_STRING Title;                   /* Title of console. Always NULL-terminated */
-
-/* SIZE */    COORD   ConsoleSize;          /* The size of the console */
-    COLORREF Colors[16];                    /* Colour palette */
-
-} CONSOLE, *PCONSOLE;
-
-/* PauseFlags values (internal only) */
-#define PAUSED_FROM_KEYBOARD  0x1
-#define PAUSED_FROM_SCROLLBAR 0x2
-#define PAUSED_FROM_SELECTION 0x4
-
-/* console.c */
-VOID FASTCALL ConioPause(PCONSOLE Console, UINT Flags);
-VOID FASTCALL ConioUnpause(PCONSOLE Console, UINT Flags);
-ULONG FASTCALL ConSrvConsoleProcessCtrlEvent(PCONSOLE Console,
-                                             ULONG ProcessGroupId,
-                                             DWORD Event);
-
-/* coninput.c */
-#define ConSrvGetInputBuffer(ProcessData, Handle, Ptr, Access, LockConsole) \
-    ConSrvGetObject((ProcessData), (Handle), (Object_t **)(Ptr), NULL,      \
-                    (Access), (LockConsole), CONIO_INPUT_BUFFER_MAGIC)
-#define ConSrvGetInputBufferAndHandleEntry(ProcessData, Handle, Ptr, Entry, Access, LockConsole)    \
-    ConSrvGetObject((ProcessData), (Handle), (Object_t **)(Ptr), (Entry),                           \
-                    (Access), (LockConsole), CONIO_INPUT_BUFFER_MAGIC)
-#define ConSrvReleaseInputBuffer(Buff, IsConsoleLocked) \
-    ConSrvReleaseObject(&(Buff)->Header, (IsConsoleLocked))
-VOID WINAPI ConioProcessKey(PCONSOLE Console, MSG* msg);
-NTSTATUS FASTCALL ConioProcessInputEvent(PCONSOLE Console,
-                                         PINPUT_RECORD InputEvent);
-
-/* conoutput.c */
-#define ConioRectHeight(Rect) \
-    (((Rect)->Top) > ((Rect)->Bottom) ? 0 : ((Rect)->Bottom) - ((Rect)->Top) + 1)
-#define ConioRectWidth(Rect) \
-    (((Rect)->Left) > ((Rect)->Right) ? 0 : ((Rect)->Right) - ((Rect)->Left) + 1)
-#define ConSrvGetScreenBuffer(ProcessData, Handle, Ptr, Access, LockConsole)    \
-    ConSrvGetObject((ProcessData), (Handle), (Object_t **)(Ptr), NULL,          \
-                    (Access), (LockConsole), CONIO_SCREEN_BUFFER_MAGIC)
-#define ConSrvGetScreenBufferAndHandleEntry(ProcessData, Handle, Ptr, Entry, Access, LockConsole)   \
-    ConSrvGetObject((ProcessData), (Handle), (Object_t **)(Ptr), (Entry),                           \
-                    (Access), (LockConsole), CONIO_SCREEN_BUFFER_MAGIC)
-#define ConSrvReleaseScreenBuffer(Buff, IsConsoleLocked)    \
-    ConSrvReleaseObject(&(Buff)->Header, (IsConsoleLocked))
-PBYTE FASTCALL ConioCoordToPointer(PCONSOLE_SCREEN_BUFFER Buf, ULONG X, ULONG Y);
-VOID FASTCALL ConioDrawConsole(PCONSOLE Console);
-NTSTATUS FASTCALL ConioWriteConsole(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buff,
-                                    CHAR *Buffer, DWORD Length, BOOL Attrib);
-NTSTATUS FASTCALL ConSrvCreateScreenBuffer(IN OUT PCONSOLE Console,
-                                           OUT PCONSOLE_SCREEN_BUFFER* Buffer,
-                                           IN COORD ScreenBufferSize,
-                                           IN USHORT ScreenAttrib,
-                                           IN USHORT PopupAttrib,
-                                           IN ULONG DisplayMode,
-                                           IN BOOLEAN IsCursorVisible,
-                                           IN ULONG CursorSize);
-VOID WINAPI ConioDeleteScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer);
-DWORD FASTCALL ConioEffectiveCursorSize(PCONSOLE Console, DWORD Scale);
-
 /* EOF */
index 9fbf203..6bd00ca 100644 (file)
@@ -2,14 +2,18 @@
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/conoutput.c
- * PURPOSE:         Console I/O functions
- * PROGRAMMERS:
+ * PURPOSE:         Console Output functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
+#include "console.h"
+#include "include/conio.h"
 #include "conio.h"
+#include "conoutput.h"
+#include "handle.h"
 
 #define NDEBUG
 #include <debug.h>
@@ -78,7 +82,7 @@ ConSrvCreateScreenBuffer(IN OUT PCONSOLE Console,
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    (*Buffer)->Header.Type = CONIO_SCREEN_BUFFER_MAGIC;
+    (*Buffer)->Header.Type = SCREEN_BUFFER;
     (*Buffer)->Header.Console = Console;
     (*Buffer)->Header.HandleCount = 0;
     (*Buffer)->ScreenBufferSize = ScreenBufferSize;
@@ -1282,16 +1286,8 @@ CSR_API(SrvCreateConsoleScreenBuffer)
 
     DPRINT("SrvCreateConsoleScreenBuffer\n");
 
-    // RtlEnterCriticalSection(&ProcessData->HandleTableLock);
-
     Status = ConSrvGetConsole(ProcessData, &Console, TRUE);
-    if (!NT_SUCCESS(Status))
-    {
-        // RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
-        return Status;
-    }
-
-    RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+    if (!NT_SUCCESS(Status)) return Status;
 
     /*
     if (Console->ActiveBuffer)
@@ -1335,6 +1331,8 @@ CSR_API(SrvCreateConsoleScreenBuffer)
                                       CursorSize);
     if (NT_SUCCESS(Status))
     {
+        RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+
         /* Insert the new handle inside the process handles table */
         Status = ConSrvInsertObject(ProcessData,
                                     &CreateScreenBufferRequest->OutputHandle,
@@ -1342,11 +1340,9 @@ CSR_API(SrvCreateConsoleScreenBuffer)
                                     CreateScreenBufferRequest->Access,
                                     CreateScreenBufferRequest->Inheritable,
                                     CreateScreenBufferRequest->ShareMode);
-    }
 
-    // ConSrvReleaseConsole(Console, TRUE);
-
-    RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+        RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+    }
 
     ConSrvReleaseConsole(Console, TRUE);
     return Status;
diff --git a/win32ss/user/consrv/conoutput.h b/win32ss/user/consrv/conoutput.h
new file mode 100644 (file)
index 0000000..c117d2c
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/conoutput.h
+ * PURPOSE:         Console Output functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+#define ConSrvGetScreenBuffer(ProcessData, Handle, Ptr, Access, LockConsole)    \
+    ConSrvGetObject((ProcessData), (Handle), (PCONSOLE_IO_OBJECT*)(Ptr), NULL,  \
+                    (Access), (LockConsole), SCREEN_BUFFER)
+#define ConSrvGetScreenBufferAndHandleEntry(ProcessData, Handle, Ptr, Entry, Access, LockConsole)   \
+    ConSrvGetObject((ProcessData), (Handle), (PCONSOLE_IO_OBJECT*)(Ptr), (Entry),                   \
+                    (Access), (LockConsole), SCREEN_BUFFER)
+#define ConSrvReleaseScreenBuffer(Buff, IsConsoleLocked)    \
+    ConSrvReleaseObject(&(Buff)->Header, (IsConsoleLocked))
+
+NTSTATUS FASTCALL ConSrvCreateScreenBuffer(IN OUT PCONSOLE Console,
+                                           OUT PCONSOLE_SCREEN_BUFFER* Buffer,
+                                           IN COORD ScreenBufferSize,
+                                           IN USHORT ScreenAttrib,
+                                           IN USHORT PopupAttrib,
+                                           IN ULONG DisplayMode,
+                                           IN BOOLEAN IsCursorVisible,
+                                           IN ULONG CursorSize);
+VOID WINAPI ConioDeleteScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer);
+
+/* EOF */
index e8b5350..6845bd1 100644 (file)
@@ -2,8 +2,8 @@
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/console.c
- * PURPOSE:         Console I/O functions
- * PROGRAMMERS:
+ * PURPOSE:         Console Management Functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 #define NONAMELESSUNION
 
 #include "consrv.h"
+#include "include/conio.h"
 #include "conio.h"
+#include "handle.h"
+#include "procinit.h"
 #include "alias.h"
+#include "coninput.h"
+#include "conoutput.h"
 #include "lineinput.h"
-#include "settings.h"
+#include "include/settings.h"
 
 #include "frontends/gui/guiterm.h"
 
@@ -23,6 +28,7 @@
     #include "frontends/tui/tuiterm.h"
 #endif
 
+#include "include/console.h"
 #include "console.h"
 #include "resource.h"
 
@@ -181,6 +187,149 @@ ConioUnpause(PCONSOLE Console, UINT Flags)
     }
 }
 
+BOOL FASTCALL
+ConSrvValidateConsolePointer(PCONSOLE Console)
+{
+    PLIST_ENTRY ConsoleEntry;
+    PCONSOLE CurrentConsole = NULL;
+
+    if (!Console) return FALSE;
+
+    /* The console list must be locked */
+    // ASSERT(Console_list_locked);
+
+    ConsoleEntry = ConsoleList.Flink;
+    while (ConsoleEntry != &ConsoleList)
+    {
+        CurrentConsole = CONTAINING_RECORD(ConsoleEntry, CONSOLE, Entry);
+        ConsoleEntry = ConsoleEntry->Flink;
+        if (CurrentConsole == Console) return TRUE;
+    }
+
+    return FALSE;
+}
+
+BOOL FASTCALL
+ConSrvValidateConsoleState(PCONSOLE Console,
+                           CONSOLE_STATE ExpectedState)
+{
+    // if (!Console) return FALSE;
+
+    /* The console must be locked */
+    // ASSERT(Console_locked);
+
+    return (Console->State == ExpectedState);
+}
+
+BOOL FASTCALL
+ConSrvValidateConsoleUnsafe(PCONSOLE Console,
+                            CONSOLE_STATE ExpectedState,
+                            BOOL LockConsole)
+{
+    if (!Console) return FALSE;
+
+    /*
+     * Lock the console to forbid possible console's state changes
+     * (which must be done when the console is already locked).
+     * If we don't want to lock it, it's because the lock is already
+     * held. So there must be no problems.
+     */
+    if (LockConsole) EnterCriticalSection(&Console->Lock);
+
+    // ASSERT(Console_locked);
+
+    /* Check whether the console's state is what we expect */
+    if (!ConSrvValidateConsoleState(Console, ExpectedState))
+    {
+        if (LockConsole) LeaveCriticalSection(&Console->Lock);
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+BOOL FASTCALL
+ConSrvValidateConsole(PCONSOLE Console,
+                      CONSOLE_STATE ExpectedState,
+                      BOOL LockConsole)
+{
+    BOOL RetVal = FALSE;
+
+    if (!Console) return FALSE;
+
+    /*
+     * Forbid creation or deletion of consoles when
+     * checking for the existence of a console.
+     */
+    ConSrvLockConsoleListShared();
+
+    if (ConSrvValidateConsolePointer(Console))
+    {
+        RetVal = ConSrvValidateConsoleUnsafe(Console,
+                                             ExpectedState,
+                                             LockConsole);
+    }
+
+    /* Unlock the console list and return */
+    ConSrvUnlockConsoleList();
+    return RetVal;
+}
+
+NTSTATUS
+FASTCALL
+ConSrvGetConsole(PCONSOLE_PROCESS_DATA ProcessData,
+                 PCONSOLE* Console,
+                 BOOL LockConsole)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+    PCONSOLE ProcessConsole;
+
+    RtlEnterCriticalSection(&ProcessData->HandleTableLock);
+    ProcessConsole = ProcessData->Console;
+
+    if (ConSrvValidateConsole(ProcessConsole, CONSOLE_RUNNING, LockConsole))
+    {
+        InterlockedIncrement(&ProcessConsole->ReferenceCount);
+        *Console = ProcessConsole;
+    }
+    else
+    {
+        *Console = NULL;
+        Status = STATUS_INVALID_HANDLE;
+    }
+
+    RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
+    return Status;
+}
+
+VOID FASTCALL
+ConSrvReleaseConsole(PCONSOLE Console,
+                     BOOL WasConsoleLocked)
+{
+    LONG RefCount = 0;
+
+    if (!Console) return;
+    // if (Console->ReferenceCount == 0) return; // This shouldn't happen
+    ASSERT(Console->ReferenceCount > 0);
+
+    /* The console must be locked */
+    // ASSERT(Console_locked);
+
+    /*
+     * Decrement the reference count. Save the new value too,
+     * because Console->ReferenceCount might be modified after
+     * the console gets unlocked but before we check whether we
+     * can destroy it.
+     */
+    RefCount = _InterlockedDecrement(&Console->ReferenceCount);
+
+    /* Unlock the console if needed */
+    if (WasConsoleLocked) LeaveCriticalSection(&Console->Lock);
+
+    /* Delete the console if needed */
+    if (RefCount <= 0) ConSrvDeleteConsole(Console);
+}
+
 VOID WINAPI
 ConSrvInitConsoleSupport(VOID)
 {
@@ -409,7 +558,7 @@ ConSrvInitConsole(OUT PCONSOLE* NewConsole,
     /*
      * Initialize the input buffer
      */
-    Console->InputBuffer.Header.Type = CONIO_INPUT_BUFFER_MAGIC;
+    Console->InputBuffer.Header.Type = INPUT_BUFFER;
     Console->InputBuffer.Header.Console = Console;
 
     SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
@@ -573,9 +722,6 @@ ConSrvInitConsole(OUT PCONSOLE* NewConsole,
 VOID WINAPI
 ConSrvDeleteConsole(PCONSOLE Console)
 {
-    PLIST_ENTRY CurrentEntry;
-    ConsoleInput* Event;
-
     DPRINT("ConSrvDeleteConsole\n");
 
     /*
@@ -658,16 +804,11 @@ ConSrvDeleteConsole(PCONSOLE Console)
     Console->ReferenceCount = 0;
 
     /* Discard all entries in the input event queue */
-    while (!IsListEmpty(&Console->InputBuffer.InputEvents))
-    {
-        CurrentEntry = RemoveHeadList(&Console->InputBuffer.InputEvents);
-        Event = CONTAINING_RECORD(CurrentEntry, ConsoleInput, ListEntry);
-        RtlFreeHeap(ConSrvHeap, 0, Event);
-    }
+    PurgeInputBuffer(Console);
 
-    if (Console->LineBuffer)
-        RtlFreeHeap(ConSrvHeap, 0, Console->LineBuffer);
+    if (Console->LineBuffer) RtlFreeHeap(ConSrvHeap, 0, Console->LineBuffer);
 
+    IntDeleteAllAliases(Console);
     HistoryDeleteBuffers(Console);
 
     ConioDeleteScreenBuffer(Console->ActiveBuffer);
@@ -676,12 +817,11 @@ ConSrvDeleteConsole(PCONSOLE Console)
         DPRINT1("BUG: screen buffer list not empty\n");
     }
 
-    CloseHandle(Console->InputBuffer.ActiveEvent);
+    // CloseHandle(Console->InputBuffer.ActiveEvent);
     if (Console->UnpauseEvent) CloseHandle(Console->UnpauseEvent);
 
     RtlFreeUnicodeString(&Console->OriginalTitle);
     RtlFreeUnicodeString(&Console->Title);
-    IntDeleteAllAliases(Console->Aliases);
 
     DPRINT("ConSrvDeleteConsole - Unlocking\n");
     LeaveCriticalSection(&Console->Lock);
@@ -696,155 +836,9 @@ ConSrvDeleteConsole(PCONSOLE Console)
     ConSrvUnlockConsoleList();
 }
 
-BOOL FASTCALL
-ConSrvValidateConsolePointer(PCONSOLE Console)
-{
-    PLIST_ENTRY ConsoleEntry;
-    PCONSOLE CurrentConsole = NULL;
-
-    if (!Console) return FALSE;
-
-    /* The console list must be locked */
-    // ASSERT(Console_list_locked);
-
-    ConsoleEntry = ConsoleList.Flink;
-    while (ConsoleEntry != &ConsoleList)
-    {
-        CurrentConsole = CONTAINING_RECORD(ConsoleEntry, CONSOLE, Entry);
-        ConsoleEntry = ConsoleEntry->Flink;
-        if (CurrentConsole == Console) return TRUE;
-    }
-
-    return FALSE;
-}
-
-BOOL FASTCALL
-ConSrvValidateConsoleState(PCONSOLE Console,
-                           CONSOLE_STATE ExpectedState)
-{
-    // if (!Console) return FALSE;
-
-    /* The console must be locked */
-    // ASSERT(Console_locked);
-
-    return (Console->State == ExpectedState);
-}
-
-BOOL FASTCALL
-ConSrvValidateConsoleUnsafe(PCONSOLE Console,
-                            CONSOLE_STATE ExpectedState,
-                            BOOL LockConsole)
-{
-    if (!Console) return FALSE;
-
-    /*
-     * Lock the console to forbid possible console's state changes
-     * (which must be done when the console is already locked).
-     * If we don't want to lock it, it's because the lock is already
-     * held. So there must be no problems.
-     */
-    if (LockConsole) EnterCriticalSection(&Console->Lock);
-
-    // ASSERT(Console_locked);
-
-    /* Check whether the console's state is what we expect */
-    if (!ConSrvValidateConsoleState(Console, ExpectedState))
-    {
-        if (LockConsole) LeaveCriticalSection(&Console->Lock);
-        return FALSE;
-    }
-
-    return TRUE;
-}
-
-BOOL FASTCALL
-ConSrvValidateConsole(PCONSOLE Console,
-                      CONSOLE_STATE ExpectedState,
-                      BOOL LockConsole)
-{
-    BOOL RetVal = FALSE;
-
-    if (!Console) return FALSE;
-
-    /*
-     * Forbid creation or deletion of consoles when
-     * checking for the existence of a console.
-     */
-    ConSrvLockConsoleListShared();
-
-    if (ConSrvValidateConsolePointer(Console))
-    {
-        RetVal = ConSrvValidateConsoleUnsafe(Console,
-                                             ExpectedState,
-                                             LockConsole);
-    }
-
-    /* Unlock the console list and return */
-    ConSrvUnlockConsoleList();
-    return RetVal;
-}
-
 
 /* PUBLIC SERVER APIS *********************************************************/
 
-CSR_API(SrvOpenConsole)
-{
-    NTSTATUS Status;
-    PCONSOLE_OPENCONSOLE OpenConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.OpenConsoleRequest;
-    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
-    PCONSOLE Console;
-
-    DWORD DesiredAccess = OpenConsoleRequest->Access;
-    DWORD ShareMode = OpenConsoleRequest->ShareMode;
-    Object_t *Object;
-
-    OpenConsoleRequest->ConsoleHandle = INVALID_HANDLE_VALUE;
-
-    Status = ConSrvGetConsole(ProcessData, &Console, TRUE);
-    if (!NT_SUCCESS(Status))
-    {
-        DPRINT1("Can't get console\n");
-        return Status;
-    }
-
-    RtlEnterCriticalSection(&ProcessData->HandleTableLock);
-
-    /*
-     * Open a handle to either the active screen buffer or the input buffer.
-     */
-    if (OpenConsoleRequest->HandleType == HANDLE_OUTPUT)
-    {
-        Object = &Console->ActiveBuffer->Header;
-    }
-    else // HANDLE_INPUT
-    {
-        Object = &Console->InputBuffer.Header;
-    }
-
-    if (((DesiredAccess & GENERIC_READ)  && Object->ExclusiveRead  != 0) ||
-        ((DesiredAccess & GENERIC_WRITE) && Object->ExclusiveWrite != 0) ||
-        (!(ShareMode & FILE_SHARE_READ)  && Object->AccessRead     != 0) ||
-        (!(ShareMode & FILE_SHARE_WRITE) && Object->AccessWrite    != 0))
-    {
-        DPRINT1("Sharing violation\n");
-        Status = STATUS_SHARING_VIOLATION;
-    }
-    else
-    {
-        Status = ConSrvInsertObject(ProcessData,
-                                    &OpenConsoleRequest->ConsoleHandle,
-                                    Object,
-                                    DesiredAccess,
-                                    OpenConsoleRequest->Inheritable,
-                                    ShareMode);
-    }
-
-    RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
-
-    ConSrvReleaseConsole(Console, TRUE);
-    return Status;
-}
-
 CSR_API(SrvAllocConsole)
 {
     NTSTATUS Status = STATUS_SUCCESS;
@@ -1020,7 +1014,7 @@ CSR_API(SrvSetConsoleMode)
     NTSTATUS Status;
     PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ConsoleModeRequest;
     DWORD ConsoleMode = ConsoleModeRequest->ConsoleMode;
-    Object_t* Object  = NULL;
+    PCONSOLE_IO_OBJECT Object  = NULL;
 
     Status = ConSrvGetObject(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
                              ConsoleModeRequest->ConsoleHandle,
@@ -1029,7 +1023,7 @@ CSR_API(SrvSetConsoleMode)
 
     Status = STATUS_SUCCESS;
 
-    if (CONIO_INPUT_BUFFER_MAGIC == Object->Type)
+    if (INPUT_BUFFER == Object->Type)
     {
         PCONSOLE_INPUT_BUFFER InputBuffer = (PCONSOLE_INPUT_BUFFER)Object;
         PCONSOLE Console = InputBuffer->Header.Console;
@@ -1067,7 +1061,7 @@ CSR_API(SrvSetConsoleMode)
         }
         InputBuffer->Mode = (ConsoleMode & CONSOLE_VALID_INPUT_MODES);
     }
-    else if (CONIO_SCREEN_BUFFER_MAGIC == Object->Type)
+    else if (SCREEN_BUFFER == Object->Type)
     {
         PCONSOLE_SCREEN_BUFFER Buffer = (PCONSOLE_SCREEN_BUFFER)Object;
 
@@ -1096,7 +1090,7 @@ CSR_API(SrvGetConsoleMode)
 {
     NTSTATUS Status;
     PCONSOLE_GETSETCONSOLEMODE ConsoleModeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ConsoleModeRequest;
-    Object_t* Object = NULL;
+    PCONSOLE_IO_OBJECT Object = NULL;
 
     Status = ConSrvGetObject(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
                              ConsoleModeRequest->ConsoleHandle,
@@ -1105,7 +1099,7 @@ CSR_API(SrvGetConsoleMode)
 
     Status = STATUS_SUCCESS;
 
-    if (CONIO_INPUT_BUFFER_MAGIC == Object->Type)
+    if (INPUT_BUFFER == Object->Type)
     {
         PCONSOLE_INPUT_BUFFER InputBuffer = (PCONSOLE_INPUT_BUFFER)Object;
         PCONSOLE Console  = InputBuffer->Header.Console;
@@ -1122,7 +1116,7 @@ CSR_API(SrvGetConsoleMode)
 
         ConsoleModeRequest->ConsoleMode = ConsoleMode;
     }
-    else if (CONIO_SCREEN_BUFFER_MAGIC == Object->Type)
+    else if (SCREEN_BUFFER == Object->Type)
     {
         PCONSOLE_SCREEN_BUFFER Buffer = (PCONSOLE_SCREEN_BUFFER)Object;
         ConsoleModeRequest->ConsoleMode = Buffer->Mode;
index 280fe6e..e7ea783 100644 (file)
@@ -2,39 +2,22 @@
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/console.h
- * PURPOSE:         Consoles Management
- * PROGRAMMERS:     Hermes Belusca-Maito
+ * PURPOSE:         Console Initialization Functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #pragma once
 
-#if 0
-/*
- * WARNING: Change the state of the console ONLY when the console is locked !
- */
-typedef enum _CONSOLE_STATE
-{
-    CONSOLE_INITIALIZING,   /* Console is initializing */
-    CONSOLE_RUNNING     ,   /* Console running */
-    CONSOLE_TERMINATING ,   /* Console about to be destroyed (but still not) */
-    CONSOLE_IN_DESTRUCTION  /* Console in destruction */
-} CONSOLE_STATE, *PCONSOLE_STATE;
-#endif
-
-
 VOID WINAPI ConSrvInitConsoleSupport(VOID);
+
 NTSTATUS WINAPI ConSrvInitConsole(OUT PCONSOLE* NewConsole,
                                   IN OUT PCONSOLE_START_INFO ConsoleStartInfo,
                                   IN PCSR_PROCESS ConsoleLeaderProcess);
 VOID WINAPI ConSrvDeleteConsole(PCONSOLE Console);
-BOOL FASTCALL ConSrvValidateConsolePointer(PCONSOLE Console);
-BOOL FASTCALL ConSrvValidateConsoleState(PCONSOLE Console,
-                                         CONSOLE_STATE ExpectedState);
-BOOL FASTCALL ConSrvValidateConsoleUnsafe(PCONSOLE Console,
-                                          CONSOLE_STATE ExpectedState,
-                                          BOOL LockConsole);
-BOOL FASTCALL ConSrvValidateConsole(PCONSOLE Console,
-                                    CONSOLE_STATE ExpectedState,
-                                    BOOL LockConsole);
+NTSTATUS FASTCALL ConSrvGetConsole(PCONSOLE_PROCESS_DATA ProcessData,
+                                   PCONSOLE* Console,
+                                   BOOL LockConsole);
+VOID FASTCALL ConSrvReleaseConsole(PCONSOLE Console,
+                                   BOOL WasConsoleLocked);
 
 /* EOF */
index 9b95b1f..2859653 100644 (file)
 #include <win/conmsg.h>
 
 
+/* Globals */
 extern HINSTANCE ConSrvDllInstance;
 extern HANDLE ConSrvHeap;
 
-/* Object type magic numbers */
-#define CONIO_INPUT_BUFFER_MAGIC    0x00000001  // -->  Input-type handles
-#define CONIO_SCREEN_BUFFER_MAGIC   0x00000002  // --> Output-type handles
-
-/* Common things to input/output/console objects */
-typedef struct Object_tt
-{
-    ULONG Type;
-    struct _CONSOLE *Console;
-    LONG AccessRead, AccessWrite;
-    LONG ExclusiveRead, ExclusiveWrite;
-    LONG HandleCount;
-} Object_t;
-
-
-typedef struct _CONSOLE_IO_HANDLE
-{
-    Object_t *Object;   /* The object on which the handle points to */
-    DWORD Access;
-    BOOL Inheritable;
-    DWORD ShareMode;
-} CONSOLE_IO_HANDLE, *PCONSOLE_IO_HANDLE;
-
+/* Opaque pointers */
+typedef struct _CONSOLE_IO_HANDLE *PCONSOLE_IO_HANDLE;
+typedef struct _CONSOLE *PCONSOLE;
 
 #define ConsoleGetPerProcessData(Process)   \
     ((PCONSOLE_PROCESS_DATA)((Process)->ServerData[CONSRV_SERVERDLL_INDEX]))
@@ -80,8 +61,8 @@ typedef struct _CONSOLE_PROCESS_DATA
     LIST_ENTRY ConsoleLink;
     PCSR_PROCESS Process;   // Process owning this structure.
     HANDLE ConsoleEvent;
-    /* PCONSOLE */ struct _CONSOLE* Console;
-    /* PCONSOLE */ struct _CONSOLE* ParentConsole;
+    PCONSOLE Console;
+    PCONSOLE ParentConsole;
 
     BOOL ConsoleApp;    // TRUE if it is a CUI app, FALSE otherwise.
 
@@ -93,129 +74,6 @@ typedef struct _CONSOLE_PROCESS_DATA
     LPTHREAD_START_ROUTINE PropDispatcher; // We hold the property dialog handler there, till all the GUI thingie moves out from CSRSS.
 } CONSOLE_PROCESS_DATA, *PCONSOLE_PROCESS_DATA;
 
-
-#if 1 // Temporarily put there.
-/*
- * WARNING: Change the state of the console ONLY when the console is locked !
- */
-typedef enum _CONSOLE_STATE
-{
-    CONSOLE_INITIALIZING,   /* Console is initializing */
-    CONSOLE_RUNNING     ,   /* Console running */
-    CONSOLE_TERMINATING ,   /* Console about to be destroyed (but still not) */
-    CONSOLE_IN_DESTRUCTION  /* Console in destruction */
-} CONSOLE_STATE, *PCONSOLE_STATE;
-#endif
-
-
-/* alias.c */
-CSR_API(SrvAddConsoleAlias);
-CSR_API(SrvGetConsoleAlias);
-CSR_API(SrvGetConsoleAliases);
-CSR_API(SrvGetConsoleAliasesLength);
-CSR_API(SrvGetConsoleAliasExes);
-CSR_API(SrvGetConsoleAliasExesLength);
-
-/* coninput.c */
-CSR_API(SrvGetConsoleInput);
-CSR_API(SrvWriteConsoleInput);
-CSR_API(SrvReadConsole);
-CSR_API(SrvFlushConsoleInputBuffer);
-CSR_API(SrvGetConsoleNumberOfInputEvents);
-
-/* conoutput.c */
-CSR_API(SrvReadConsoleOutput);
-CSR_API(SrvWriteConsoleOutput);
-CSR_API(SrvReadConsoleOutputString);
-CSR_API(SrvWriteConsoleOutputString);
-CSR_API(SrvFillConsoleOutput);
-CSR_API(SrvWriteConsole);
-CSR_API(SrvSetConsoleCursorPosition);
-CSR_API(SrvGetConsoleCursorInfo);
-CSR_API(SrvSetConsoleCursorInfo);
-CSR_API(SrvSetConsoleTextAttribute);
-CSR_API(SrvCreateConsoleScreenBuffer);
-CSR_API(SrvGetConsoleScreenBufferInfo);
-CSR_API(SrvSetConsoleActiveScreenBuffer);
-CSR_API(SrvScrollConsoleScreenBuffer);
-CSR_API(SrvSetConsoleScreenBufferSize);
-
-/* console.c */
-CSR_API(SrvOpenConsole);
-CSR_API(SrvAllocConsole);
-CSR_API(SrvAttachConsole);
-CSR_API(SrvFreeConsole);
-CSR_API(SrvSetConsoleMode);
-CSR_API(SrvGetConsoleMode);
-CSR_API(SrvSetConsoleTitle);
-CSR_API(SrvGetConsoleTitle);
-CSR_API(SrvGetConsoleHardwareState);
-CSR_API(SrvSetConsoleHardwareState);
-CSR_API(SrvGetConsoleDisplayMode);
-CSR_API(SrvSetConsoleDisplayMode);
-CSR_API(SrvGetConsoleWindow);
-CSR_API(SrvSetConsoleIcon);
-CSR_API(SrvGetConsoleCP);
-CSR_API(SrvSetConsoleCP);
-CSR_API(SrvGetConsoleProcessList);
-CSR_API(SrvGenerateConsoleCtrlEvent);
-CSR_API(SrvGetConsoleSelectionInfo);
-
-/* handle.c */
-CSR_API(SrvCloseHandle);
-CSR_API(SrvVerifyConsoleIoHandle);
-CSR_API(SrvDuplicateHandle);
-
-NTSTATUS FASTCALL ConSrvInsertObject(PCONSOLE_PROCESS_DATA ProcessData,
-                                     PHANDLE Handle,
-                                     Object_t *Object,
-                                     DWORD Access,
-                                     BOOL Inheritable,
-                                     DWORD ShareMode);
-NTSTATUS FASTCALL ConSrvRemoveObject(PCONSOLE_PROCESS_DATA ProcessData,
-                                     HANDLE Handle);
-NTSTATUS FASTCALL ConSrvGetObject(PCONSOLE_PROCESS_DATA ProcessData,
-                                  HANDLE Handle,
-                                  Object_t** Object,
-                                  PCONSOLE_IO_HANDLE* Entry OPTIONAL,
-                                  DWORD Access,
-                                  BOOL LockConsole,
-                                  ULONG Type);
-VOID FASTCALL ConSrvReleaseObject(Object_t *Object,
-                                 BOOL IsConsoleLocked);
-NTSTATUS FASTCALL ConSrvAllocateConsole(PCONSOLE_PROCESS_DATA ProcessData,
-                                        PHANDLE pInputHandle,
-                                        PHANDLE pOutputHandle,
-                                        PHANDLE pErrorHandle,
-                                        PCONSOLE_START_INFO ConsoleStartInfo);
-NTSTATUS FASTCALL ConSrvInheritConsole(PCONSOLE_PROCESS_DATA ProcessData,
-                                       struct _CONSOLE* Console,
-                                       BOOL CreateNewHandlesTable,
-                                       PHANDLE pInputHandle,
-                                       PHANDLE pOutputHandle,
-                                       PHANDLE pErrorHandle);
-NTSTATUS FASTCALL ConSrvGetConsole(PCONSOLE_PROCESS_DATA ProcessData,
-                                   struct _CONSOLE** Console,
-                                   BOOL LockConsole);
-VOID FASTCALL ConSrvReleaseConsole(struct _CONSOLE* Console,
-                                   BOOL WasConsoleLocked);
-VOID FASTCALL ConSrvRemoveConsole(PCONSOLE_PROCESS_DATA ProcessData);
-
-NTSTATUS NTAPI ConSrvNewProcess(PCSR_PROCESS SourceProcess,
-                                PCSR_PROCESS TargetProcess);
-NTSTATUS NTAPI ConSrvConnect(IN PCSR_PROCESS CsrProcess,
-                             IN OUT PVOID ConnectionInfo,
-                             IN OUT PULONG ConnectionInfoLength);
-VOID NTAPI ConSrvDisconnect(PCSR_PROCESS Process);
-
-/* lineinput.c */
-CSR_API(SrvGetConsoleCommandHistoryLength);
-CSR_API(SrvGetConsoleCommandHistory);
-CSR_API(SrvExpungeConsoleCommandHistory);
-CSR_API(SrvSetConsoleNumberOfCommands);
-CSR_API(SrvGetConsoleHistory);
-CSR_API(SrvSetConsoleHistory);
-
 #endif // __CONSRV_H__
 
 /* EOF */
index a90d50b..d5aeebc 100644 (file)
@@ -3,12 +3,12 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/consolecpl.h
  * PURPOSE:         GUI front-end settings management - Header for console.dll
- * PROGRAMMERS:     Hermes Belusca - Maito
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #pragma once
 
-#include "settings.h"
+#include "include/settings.h"
 #include "guisettings.h"
 
 /* EOF */
index 0a27672..449eada 100644 (file)
@@ -3,15 +3,14 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/frontends/gui/guisettings.c
  * PURPOSE:         GUI Terminal Front-End Settings Management
- * PROGRAMMERS:     Hermes Belusca - Maito
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
-#include "conio.h"
-#include "settings.h"
-#include "guiterm.h"
+#include "include/conio.h"
+#include "include/settings.h"
 #include "guisettings.h"
 
 #define NDEBUG
index 8695857..db890ab 100644 (file)
@@ -3,15 +3,13 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/guisettings.h
  * PURPOSE:         GUI front-end settings management
- * PROGRAMMERS:     Hermes Belusca - Maito
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  *
  * NOTE: Also used by console.dll
  */
 
 #pragma once
 
-// #include "guiconsole.h"
-
 #ifndef WM_APP
     #define WM_APP 0x8000
 #endif
index bdc2208..ee4bd8a 100644 (file)
@@ -3,15 +3,15 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/frontends/gui/guiterm.c
  * PURPOSE:         GUI Terminal Front-End
- * PROGRAMMERS:
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
-#include "conio.h"
-#include "console.h"
-#include "settings.h"
+#include "include/conio.h"
+#include "include/console.h"
+#include "include/settings.h"
 #include "guiterm.h"
 #include "guisettings.h"
 #include "resource.h"
index e3e7476..14530f1 100644 (file)
@@ -3,18 +3,18 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/frontends/gui/guiterm.h
  * PURPOSE:         GUI Terminal Front-End
- * PROGRAMMERS:
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #pragma once
 
-// #include "guisettings.h"
-
 #define CONGUI_MIN_WIDTH      10
 #define CONGUI_MIN_HEIGHT     10
 #define CONGUI_UPDATE_TIME    0
 #define CONGUI_UPDATE_TIMER   1
 
+#define CURSOR_BLINK_TIME 500
+
 NTSTATUS FASTCALL GuiInitConsole(PCONSOLE Console,
                                  /*IN*/ PCONSOLE_START_INFO ConsoleStartInfo,
                                  PCONSOLE_INFO ConsoleInfo,
index 37fff93..eb944a4 100644 (file)
@@ -3,7 +3,7 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/frontends/tui/tuiterm.c
  * PURPOSE:         TUI Terminal Front-End
- * PROGRAMMERS:
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #ifdef TUI_CONSOLE
index 8fd36a6..592e36c 100644 (file)
@@ -3,7 +3,7 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/frontends/tui/tuiterm.h
  * PURPOSE:         TUI Terminal Front-End
- * PROGRAMMERS:
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 #include "conio.h"
index 7ceedc0..e949633 100644 (file)
@@ -3,25 +3,40 @@
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/handle.c
  * PURPOSE:         Console I/O Handles functions
- * PROGRAMMERS:
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  */
 
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
+#include "include/conio.h"
 #include "conio.h"
+#include "handle.h"
+#include "include/console.h"
 #include "console.h"
+#include "conoutput.h"
 
 #define NDEBUG
 #include <debug.h>
 
 
+/* GLOBALS ********************************************************************/
+
+typedef struct _CONSOLE_IO_HANDLE
+{
+    PCONSOLE_IO_OBJECT Object;   /* The object on which the handle points to */
+    DWORD Access;
+    BOOL Inheritable;
+    DWORD ShareMode;
+} CONSOLE_IO_HANDLE, *PCONSOLE_IO_HANDLE;
+
+
 /* PRIVATE FUNCTIONS **********************************************************/
 
 static INT
 AdjustHandleCounts(PCONSOLE_IO_HANDLE Entry, INT Change)
 {
-    Object_t *Object = Entry->Object;
+    PCONSOLE_IO_OBJECT Object = Entry->Object;
 
     DPRINT("AdjustHandleCounts(0x%p, %d), Object = 0x%p, Object->HandleCount = %d, Object->Type = %lu\n", Entry, Change, Object, Object->HandleCount, Object->Type);
 
@@ -38,7 +53,7 @@ AdjustHandleCounts(PCONSOLE_IO_HANDLE Entry, INT Change)
 static VOID
 ConSrvCreateHandleEntry(PCONSOLE_IO_HANDLE Entry)
 {
-    /// LOCK /// Object_t *Object = Entry->Object;
+    /// LOCK /// PCONSOLE_IO_OBJECT Object = Entry->Object;
     /// LOCK /// EnterCriticalSection(&Object->Console->Lock);
     AdjustHandleCounts(Entry, +1);
     /// LOCK /// LeaveCriticalSection(&Object->Console->Lock);
@@ -47,7 +62,7 @@ ConSrvCreateHandleEntry(PCONSOLE_IO_HANDLE Entry)
 static VOID
 ConSrvCloseHandleEntry(PCONSOLE_IO_HANDLE Entry)
 {
-    Object_t *Object = Entry->Object;
+    PCONSOLE_IO_OBJECT Object = Entry->Object;
     if (Object != NULL)
     {
         /// LOCK /// PCONSOLE Console = Object->Console;
@@ -57,7 +72,7 @@ ConSrvCloseHandleEntry(PCONSOLE_IO_HANDLE Entry)
          * If this is a input handle, notify and dereference
          * all the waits related to this handle.
          */
-        if (Object->Type == CONIO_INPUT_BUFFER_MAGIC)
+        if (Object->Type == INPUT_BUFFER)
         {
             PCONSOLE_INPUT_BUFFER InputBuffer = (PCONSOLE_INPUT_BUFFER)Object;
 
@@ -83,7 +98,7 @@ ConSrvCloseHandleEntry(PCONSOLE_IO_HANDLE Entry)
         /* If the last handle to a screen buffer is closed, delete it... */
         if (AdjustHandleCounts(Entry, -1) == 0)
         {
-            if (Object->Type == CONIO_SCREEN_BUFFER_MAGIC)
+            if (Object->Type == SCREEN_BUFFER)
             {
                 PCONSOLE_SCREEN_BUFFER Buffer = (PCONSOLE_SCREEN_BUFFER)Object;
                 /* ...unless it's the only buffer left. Windows allows deletion
@@ -92,7 +107,7 @@ ConSrvCloseHandleEntry(PCONSOLE_IO_HANDLE Entry)
                 if (Buffer->ListEntry.Flink != Buffer->ListEntry.Blink)
                     ConioDeleteScreenBuffer(Buffer);
             }
-            else if (Object->Type == CONIO_INPUT_BUFFER_MAGIC)
+            else if (Object->Type == INPUT_BUFFER)
             {
                 DPRINT("Closing the input buffer\n");
             }
@@ -261,7 +276,7 @@ NTSTATUS
 FASTCALL
 ConSrvInsertObject(PCONSOLE_PROCESS_DATA ProcessData,
                    PHANDLE Handle,
-                   Object_t *Object,
+                   PCONSOLE_IO_OBJECT Object,
                    DWORD Access,
                    BOOL Inheritable,
                    DWORD ShareMode)
@@ -318,7 +333,7 @@ ConSrvRemoveObject(PCONSOLE_PROCESS_DATA ProcessData,
                    HANDLE Handle)
 {
     ULONG_PTR h = (ULONG_PTR)Handle >> 2;
-    Object_t *Object;
+    PCONSOLE_IO_OBJECT Object;
 
     RtlEnterCriticalSection(&ProcessData->HandleTableLock);
 
@@ -339,15 +354,15 @@ NTSTATUS
 FASTCALL
 ConSrvGetObject(PCONSOLE_PROCESS_DATA ProcessData,
                 HANDLE Handle,
-                Object_t** Object,
-                PCONSOLE_IO_HANDLE* Entry OPTIONAL,
+                PCONSOLE_IO_OBJECT* Object,
+                PVOID* Entry OPTIONAL,
                 DWORD Access,
                 BOOL LockConsole,
-                ULONG Type)
+                CONSOLE_IO_OBJECT_TYPE Type)
 {
     ULONG_PTR h = (ULONG_PTR)Handle >> 2;
     PCONSOLE_IO_HANDLE HandleEntry = NULL;
-    Object_t* ObjectEntry = NULL;
+    PCONSOLE_IO_OBJECT ObjectEntry = NULL;
 
     ASSERT(Object);
     if (Entry) *Entry = NULL;
@@ -396,7 +411,7 @@ ConSrvGetObject(PCONSOLE_PROCESS_DATA ProcessData,
 
 VOID
 FASTCALL
-ConSrvReleaseObject(Object_t *Object,
+ConSrvReleaseObject(PCONSOLE_IO_OBJECT Object,
                     BOOL IsConsoleLocked)
 {
     ConSrvReleaseConsole(Object->Console, IsConsoleLocked);
@@ -564,64 +579,72 @@ ConSrvRemoveConsole(PCONSOLE_PROCESS_DATA ProcessData)
     RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
 }
 
-NTSTATUS
-FASTCALL
-ConSrvGetConsole(PCONSOLE_PROCESS_DATA ProcessData,
-                 PCONSOLE* Console,
-                 BOOL LockConsole)
+
+/* PUBLIC SERVER APIS *********************************************************/
+
+CSR_API(SrvOpenConsole)
 {
-    NTSTATUS Status = STATUS_SUCCESS;
-    PCONSOLE ProcessConsole;
+    /*
+     * This API opens a handle to either the input buffer or to
+     * a screen-buffer of the console of the current process.
+     */
+
+    NTSTATUS Status;
+    PCONSOLE_OPENCONSOLE OpenConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.OpenConsoleRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+    PCONSOLE Console;
+
+    DWORD DesiredAccess = OpenConsoleRequest->Access;
+    DWORD ShareMode = OpenConsoleRequest->ShareMode;
+    PCONSOLE_IO_OBJECT Object;
+
+    OpenConsoleRequest->ConsoleHandle = INVALID_HANDLE_VALUE;
+
+    Status = ConSrvGetConsole(ProcessData, &Console, TRUE);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("Can't get console\n");
+        return Status;
+    }
 
     RtlEnterCriticalSection(&ProcessData->HandleTableLock);
-    ProcessConsole = ProcessData->Console;
 
-    if (ConSrvValidateConsole(ProcessConsole, CONSOLE_RUNNING, LockConsole))
+    /*
+     * Open a handle to either the active screen buffer or the input buffer.
+     */
+    if (OpenConsoleRequest->HandleType == HANDLE_OUTPUT)
+    {
+        Object = &Console->ActiveBuffer->Header;
+    }
+    else // HANDLE_INPUT
     {
-        InterlockedIncrement(&ProcessConsole->ReferenceCount);
-        *Console = ProcessConsole;
+        Object = &Console->InputBuffer.Header;
+    }
+
+    if (((DesiredAccess & GENERIC_READ)  && Object->ExclusiveRead  != 0) ||
+        ((DesiredAccess & GENERIC_WRITE) && Object->ExclusiveWrite != 0) ||
+        (!(ShareMode & FILE_SHARE_READ)  && Object->AccessRead     != 0) ||
+        (!(ShareMode & FILE_SHARE_WRITE) && Object->AccessWrite    != 0))
+    {
+        DPRINT1("Sharing violation\n");
+        Status = STATUS_SHARING_VIOLATION;
     }
     else
     {
-        *Console = NULL;
-        Status = STATUS_INVALID_HANDLE;
+        Status = ConSrvInsertObject(ProcessData,
+                                    &OpenConsoleRequest->ConsoleHandle,
+                                    Object,
+                                    DesiredAccess,
+                                    OpenConsoleRequest->Inheritable,
+                                    ShareMode);
     }
 
     RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
-    return Status;
-}
-
-VOID FASTCALL
-ConSrvReleaseConsole(PCONSOLE Console,
-                     BOOL WasConsoleLocked)
-{
-    LONG RefCount = 0;
-
-    if (!Console) return;
-    // if (Console->ReferenceCount == 0) return; // This shouldn't happen
-    ASSERT(Console->ReferenceCount > 0);
-
-    /* The console must be locked */
-    // ASSERT(Console_locked);
-
-    /*
-     * Decrement the reference count. Save the new value too,
-     * because Console->ReferenceCount might be modified after
-     * the console gets unlocked but before we check whether we
-     * can destroy it.
-     */
-    RefCount = _InterlockedDecrement(&Console->ReferenceCount);
 
-    /* Unlock the console if needed */
-    if (WasConsoleLocked) LeaveCriticalSection(&Console->Lock);
-
-    /* Delete the console if needed */
-    if (RefCount <= 0) ConSrvDeleteConsole(Console);
+    ConSrvReleaseConsole(Console, TRUE);
+    return Status;
 }
 
-
-/* PUBLIC SERVER APIS *********************************************************/
-
 CSR_API(SrvCloseHandle)
 {
     PCONSOLE_CLOSEHANDLE CloseHandleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CloseHandleRequest;
diff --git a/win32ss/user/consrv/handle.h b/win32ss/user/consrv/handle.h
new file mode 100644 (file)
index 0000000..85167ca
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/handle.h
+ * PURPOSE:         Console I/O Handles functions
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+NTSTATUS FASTCALL ConSrvInsertObject(PCONSOLE_PROCESS_DATA ProcessData,
+                                     PHANDLE Handle,
+                                     PCONSOLE_IO_OBJECT Object,
+                                     DWORD Access,
+                                     BOOL Inheritable,
+                                     DWORD ShareMode);
+NTSTATUS FASTCALL ConSrvRemoveObject(PCONSOLE_PROCESS_DATA ProcessData,
+                                     HANDLE Handle);
+NTSTATUS FASTCALL ConSrvGetObject(PCONSOLE_PROCESS_DATA ProcessData,
+                                  HANDLE Handle,
+                                  PCONSOLE_IO_OBJECT* Object,
+                                  PVOID* Entry OPTIONAL,
+                                  DWORD Access,
+                                  BOOL LockConsole,
+                                  CONSOLE_IO_OBJECT_TYPE Type);
+VOID FASTCALL ConSrvReleaseObject(PCONSOLE_IO_OBJECT Object,
+                                  BOOL IsConsoleLocked);
+
+/* EOF */
diff --git a/win32ss/user/consrv/include/conio.h b/win32ss/user/consrv/include/conio.h
new file mode 100644 (file)
index 0000000..ed75546
--- /dev/null
@@ -0,0 +1,235 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/include/conio.h
+ * PURPOSE:         Public Console I/O Interface
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+#define CSR_DEFAULT_CURSOR_SIZE 25
+
+/* Default attributes */
+#define DEFAULT_SCREEN_ATTRIB   (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
+#define DEFAULT_POPUP_ATTRIB    (FOREGROUND_BLUE | FOREGROUND_RED   | \
+                                 BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY)
+
+/* Object type magic numbers */
+typedef enum _CONSOLE_IO_OBJECT_TYPE
+{
+    INPUT_BUFFER  = 0x01,   // -->  Input-type handles
+    SCREEN_BUFFER = 0x02    // --> Output-type handles
+} CONSOLE_IO_OBJECT_TYPE;
+
+typedef struct _CONSOLE_IO_OBJECT
+{
+    CONSOLE_IO_OBJECT_TYPE Type;
+    PCONSOLE Console;
+    LONG AccessRead, AccessWrite;
+    LONG ExclusiveRead, ExclusiveWrite;
+    LONG HandleCount;
+} CONSOLE_IO_OBJECT, *PCONSOLE_IO_OBJECT;
+
+/************************************************************************
+ * Screen buffer structure represents the win32 screen buffer object.   *
+ * Internally, the portion of the buffer being shown CAN loop past the  *
+ * bottom of the virtual buffer and wrap around to the top.  Win32 does *
+ * not do this.  I decided to do this because it eliminates the need to *
+ * do a massive memcpy() to scroll the contents of the buffer up to     *
+ * scroll the screen on output, instead I just shift down the position  *
+ * to be displayed, and let it wrap around to the top again.            *
+ * The VirtualY member keeps track of the top Y coord that win32        *
+ * clients THINK is currently being displayed, because they think that  *
+ * when the display reaches the bottom of the buffer and another line   *
+ * being printed causes another line to scroll down, that the buffer IS *
+ * memcpy()'s up, and the bottom of the buffer is still displayed, but  *
+ * internally, I just wrap back to the top of the buffer.               *
+ ************************************************************************/
+
+typedef struct _CONSOLE_SCREEN_BUFFER
+{
+    CONSOLE_IO_OBJECT Header;       /* Object header */
+    LIST_ENTRY ListEntry;           /* Entry in console's list of buffers */
+
+    BYTE *Buffer; /* CHAR_INFO */   /* Pointer to screen buffer */
+
+    COORD ScreenBufferSize;         /* Size of this screen buffer */
+    COORD CursorPosition;           /* Current cursor position */
+
+    USHORT ShowX, ShowY;            /* Beginning offset for the actual display area */
+    USHORT VirtualY;                /* Top row of buffer being displayed, reported to callers */
+
+    BOOLEAN CursorBlinkOn;
+    BOOLEAN ForceCursorOff;
+    ULONG   CursorSize;
+    CONSOLE_CURSOR_INFO CursorInfo; // FIXME: Keep this member or not ??
+
+    WORD ScreenDefaultAttrib;       /* Default screen char attribute */
+    WORD PopupDefaultAttrib;        /* Default popup char attribute */
+    USHORT Mode;
+    ULONG  DisplayMode;
+} CONSOLE_SCREEN_BUFFER, *PCONSOLE_SCREEN_BUFFER;
+
+typedef struct _CONSOLE_INPUT_BUFFER
+{
+    CONSOLE_IO_OBJECT Header;       /* Object header */
+
+    ULONG InputBufferSize;          /* Size of this input buffer */
+    LIST_ENTRY InputEvents;         /* List head for input event queue */
+    HANDLE ActiveEvent;             /* Event set when an input event is added in its queue */
+    LIST_ENTRY ReadWaitQueue;       /* List head for the queue of read wait blocks */
+
+    USHORT Mode;                    /* Console Input Buffer mode flags */
+} CONSOLE_INPUT_BUFFER, *PCONSOLE_INPUT_BUFFER;
+
+typedef struct _FRONTEND_VTBL
+{
+    /*
+     * Internal interface (functions called by the console server only)
+     */
+    VOID (WINAPI *CleanupConsole)(PCONSOLE Console);
+    VOID (WINAPI *WriteStream)(PCONSOLE Console,
+                               SMALL_RECT* Block,
+                               LONG CursorStartX,
+                               LONG CursorStartY,
+                               UINT ScrolledLines,
+                               CHAR *Buffer,
+                               UINT Length);
+    VOID (WINAPI *DrawRegion)(PCONSOLE Console,
+                              SMALL_RECT* Region);
+    BOOL (WINAPI *SetCursorInfo)(PCONSOLE Console,
+                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer);
+    BOOL (WINAPI *SetScreenInfo)(PCONSOLE Console,
+                                 PCONSOLE_SCREEN_BUFFER ScreenBuffer,
+                                 UINT OldCursorX,
+                                 UINT OldCursorY);
+    BOOL (WINAPI *UpdateScreenInfo)(PCONSOLE Console,
+                                    PCONSOLE_SCREEN_BUFFER ScreenBuffer);
+    NTSTATUS (WINAPI *ResizeBuffer)(PCONSOLE Console,
+                                    PCONSOLE_SCREEN_BUFFER ScreenBuffer,
+                                    COORD Size);
+    VOID (WINAPI *ResizeTerminal)(PCONSOLE Console);
+    BOOL (WINAPI *ProcessKeyCallback)(PCONSOLE Console,
+                                      MSG* msg,
+                                      BYTE KeyStateMenu,
+                                      DWORD ShiftState,
+                                      UINT VirtualKeyCode,
+                                      BOOL Down);
+    VOID (WINAPI *RefreshInternalInfo)(PCONSOLE Console);
+
+    /*
+     * External interface (functions corresponding to the Console API)
+     */
+    VOID (WINAPI *ChangeTitle)(PCONSOLE Console);
+    BOOL (WINAPI *ChangeIcon)(PCONSOLE Console,
+                              HICON hWindowIcon);
+    HWND (WINAPI *GetConsoleWindowHandle)(PCONSOLE Console);
+
+} FRONTEND_VTBL, *PFRONTEND_VTBL;
+
+typedef struct _FRONTEND_IFACE
+{
+    PFRONTEND_VTBL Vtbl;    /* Virtual table */
+    PVOID Data;             /* Private data  */
+    PVOID OldData;          /* Reserved      */
+} FRONTEND_IFACE, *PFRONTEND_IFACE;
+
+/*
+ * WARNING: Change the state of the console ONLY when the console is locked !
+ */
+typedef enum _CONSOLE_STATE
+{
+    CONSOLE_INITIALIZING,   /* Console is initializing */
+    CONSOLE_RUNNING     ,   /* Console running */
+    CONSOLE_TERMINATING ,   /* Console about to be destroyed (but still not) */
+    CONSOLE_IN_DESTRUCTION  /* Console in destruction */
+} CONSOLE_STATE, *PCONSOLE_STATE;
+
+typedef struct _CONSOLE
+{
+    LONG ReferenceCount;                    /* Is incremented each time a handle to something in the console (a screen-buffer or the input buffer of this console) gets referenced */
+    CRITICAL_SECTION Lock;
+    CONSOLE_STATE State;                    /* State of the console */
+
+    // struct _CONSOLE *Prev, *Next;           /* Next and Prev consoles in console wheel */
+    LIST_ENTRY Entry;                       /* Entry in the list of consoles */
+    LIST_ENTRY ProcessList;                 /* List of processes owning the console. The first one is the so-called "Console Leader Process" */
+
+    FRONTEND_IFACE TermIFace;               /* Frontend-specific interface */
+
+/**************************** Input buffer and data ***************************/
+    CONSOLE_INPUT_BUFFER InputBuffer;       /* Input buffer of the console */
+
+    PWCHAR LineBuffer;                      /* Current line being input, in line buffered mode */
+    WORD LineMaxSize;                       /* Maximum size of line in characters (including CR+LF) */
+    WORD LineSize;                          /* Current size of line */
+    WORD LinePos;                           /* Current position within line */
+    BOOLEAN LineComplete;                   /* User pressed enter, ready to send back to client */
+    BOOLEAN LineUpPressed;
+    BOOLEAN LineInsertToggle;               /* Replace character over cursor instead of inserting */
+    ULONG LineWakeupMask;                   /* Bitmap of which control characters will end line input */
+
+    BOOLEAN QuickEdit;
+    BOOLEAN InsertMode;
+    UINT CodePage;
+    UINT OutputCodePage;
+
+    CONSOLE_SELECTION_INFO Selection;       /* Contains information about the selection */
+    COORD dwSelectionCursor;                /* Selection cursor position, most of the time different from Selection.dwSelectionAnchor */
+
+/******************************* Screen buffers *******************************/
+    LIST_ENTRY BufferList;                  /* List of all screen buffers for this console */
+    PCONSOLE_SCREEN_BUFFER ActiveBuffer;    /* Pointer to currently active screen buffer */
+    BYTE PauseFlags;
+    HANDLE UnpauseEvent;
+    LIST_ENTRY WriteWaitQueue;              /* List head for the queue of write wait blocks */
+
+    ULONG HardwareState;                    /* _GDI_MANAGED, _DIRECT */
+
+/**************************** Aliases and Histories ***************************/
+    struct _ALIAS_HEADER *Aliases;
+    LIST_ENTRY HistoryBuffers;
+    ULONG HistoryBufferSize;                /* Size for newly created history buffers */
+    ULONG NumberOfHistoryBuffers;           /* Maximum number of history buffers allowed */
+    BOOLEAN HistoryNoDup;                   /* Remove old duplicate history entries */
+
+/****************************** Other properties ******************************/
+    UNICODE_STRING OriginalTitle;           /* Original title of console, the one when the console leader is launched. Always NULL-terminated */
+    UNICODE_STRING Title;                   /* Title of console. Always NULL-terminated */
+
+/* SIZE */    COORD   ConsoleSize;          /* The size of the console */
+    COLORREF Colors[16];                    /* Colour palette */
+
+} CONSOLE, *PCONSOLE;
+
+/* PauseFlags values (internal only) */
+#define PAUSED_FROM_KEYBOARD  0x1
+#define PAUSED_FROM_SCROLLBAR 0x2
+#define PAUSED_FROM_SELECTION 0x4
+
+/* console.c */
+VOID FASTCALL ConioPause(PCONSOLE Console, UINT Flags);
+VOID FASTCALL ConioUnpause(PCONSOLE Console, UINT Flags);
+ULONG FASTCALL ConSrvConsoleProcessCtrlEvent(PCONSOLE Console,
+                                             ULONG ProcessGroupId,
+                                             DWORD Event);
+
+/* coninput.c */
+VOID WINAPI ConioProcessKey(PCONSOLE Console, MSG* msg);
+NTSTATUS FASTCALL ConioProcessInputEvent(PCONSOLE Console,
+                                         PINPUT_RECORD InputEvent);
+
+/* conoutput.c */
+#define ConioRectHeight(Rect) \
+    (((Rect)->Top) > ((Rect)->Bottom) ? 0 : ((Rect)->Bottom) - ((Rect)->Top) + 1)
+#define ConioRectWidth(Rect) \
+    (((Rect)->Left) > ((Rect)->Right) ? 0 : ((Rect)->Right) - ((Rect)->Left) + 1)
+
+PBYTE FASTCALL ConioCoordToPointer(PCONSOLE_SCREEN_BUFFER Buf, ULONG X, ULONG Y);
+VOID FASTCALL ConioDrawConsole(PCONSOLE Console);
+NTSTATUS FASTCALL ConioWriteConsole(PCONSOLE Console, PCONSOLE_SCREEN_BUFFER Buff,
+                                    CHAR *Buffer, DWORD Length, BOOL Attrib);
+DWORD FASTCALL ConioEffectiveCursorSize(PCONSOLE Console, DWORD Scale);
+
+/* EOF */
diff --git a/win32ss/user/consrv/include/console.h b/win32ss/user/consrv/include/console.h
new file mode 100644 (file)
index 0000000..09e4b33
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/include/console.h
+ * PURPOSE:         Public Console Management Interface
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+BOOL FASTCALL ConSrvValidateConsolePointer(PCONSOLE Console);
+BOOL FASTCALL ConSrvValidateConsoleState(PCONSOLE Console,
+                                         CONSOLE_STATE ExpectedState);
+BOOL FASTCALL ConSrvValidateConsoleUnsafe(PCONSOLE Console,
+                                          CONSOLE_STATE ExpectedState,
+                                          BOOL LockConsole);
+BOOL FASTCALL ConSrvValidateConsole(PCONSOLE Console,
+                                    CONSOLE_STATE ExpectedState,
+                                    BOOL LockConsole);
+
+/* EOF */
similarity index 93%
rename from win32ss/user/consrv/settings.h
rename to win32ss/user/consrv/include/settings.h
index 5d1f2ec..909b828 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
- * FILE:            win32ss/user/consrv/settings.h
- * PURPOSE:         Consoles settings management
- * PROGRAMMERS:     Hermes Belusca - Maito
+ * FILE:            win32ss/user/consrv/include/settings.h
+ * PURPOSE:         Public Console Settings Management Interface
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  *
  * NOTE: Adapted from existing code.
  */
index ae55e36..9c16e33 100644 (file)
@@ -9,7 +9,8 @@
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
-#include "conio.h"
+#include "api.h"
+#include "procinit.h"
 #include "console.h"
 
 #define NDEBUG
index f034dc8..fbfdd47 100644 (file)
@@ -9,6 +9,8 @@
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
+#include "console.h"
+#include "include/conio.h"
 #include "conio.h"
 
 #define NDEBUG
diff --git a/win32ss/user/consrv/procinit.h b/win32ss/user/consrv/procinit.h
new file mode 100644 (file)
index 0000000..ddbde0b
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * LICENSE:         GPL - See COPYING in the top level directory
+ * PROJECT:         ReactOS Console Server DLL
+ * FILE:            win32ss/user/consrv/procinit.h
+ * PURPOSE:         Functions for console processes initialization
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
+ */
+
+#pragma once
+
+NTSTATUS FASTCALL ConSrvAllocateConsole(PCONSOLE_PROCESS_DATA ProcessData,
+                                        PHANDLE pInputHandle,
+                                        PHANDLE pOutputHandle,
+                                        PHANDLE pErrorHandle,
+                                        PCONSOLE_START_INFO ConsoleStartInfo);
+NTSTATUS FASTCALL ConSrvInheritConsole(PCONSOLE_PROCESS_DATA ProcessData,
+                                       PCONSOLE Console,
+                                       BOOL CreateNewHandlesTable,
+                                       PHANDLE pInputHandle,
+                                       PHANDLE pOutputHandle,
+                                       PHANDLE pErrorHandle);
+VOID FASTCALL ConSrvRemoveConsole(PCONSOLE_PROCESS_DATA ProcessData);
+
+/* EOF */
index 3fcf396..fa98cbc 100644 (file)
@@ -2,8 +2,8 @@
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS Console Server DLL
  * FILE:            win32ss/user/consrv/settings.c
- * PURPOSE:         Consoles settings management
- * PROGRAMMERS:     Hermes Belusca - Maito
+ * PURPOSE:         Console settings management
+ * PROGRAMMERS:     Hermes Belusca-Maito (hermes.belusca@sfr.fr)
  *
  * NOTE: Adapted from existing code.
  */
@@ -11,8 +11,9 @@
 /* INCLUDES *******************************************************************/
 
 #include "consrv.h"
+#include "include/conio.h"
 #include "conio.h"
-#include "settings.h"
+#include "include/settings.h"
 
 #include <stdio.h> // for swprintf