[CONSRV] Keep a count of input events in the console queue + code simplifications.

- Update the documentation of members of CONSOLE_INPUT_BUFFER.
- Simplify ConDrvGetConsoleNumberOfInputEvents().
- Simplify ConDrvFlushConsoleInputBuffer().
- Use also GetConsoleInputBufferMode() more often.
This commit is contained in:
Hermès Bélusca-Maïto 2020-02-22 23:19:45 +01:00
parent 570eba2a52
commit 90d795b0bf
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
7 changed files with 22 additions and 33 deletions

View file

@ -156,6 +156,7 @@ AddInputEvents(PCONSOLE Console,
/* Append the event to the beginning of the queue */
InsertHeadList(&Console->InputBuffer.InputEvents, &ConInRec->ListEntry);
}
_InterlockedIncrement((PLONG)&Console->InputBuffer.NumberOfEvents);
// return STATUS_SUCCESS;
Status = STATUS_SUCCESS;
@ -170,14 +171,16 @@ Done:
}
static VOID
PurgeInputBuffer(PCONSOLE Console)
PurgeInputBuffer(IN PCONSOLE_INPUT_BUFFER InputBuffer)
{
PLIST_ENTRY CurrentEntry;
ConsoleInput* Event;
while (!IsListEmpty(&Console->InputBuffer.InputEvents))
/* Discard all entries in the input event queue */
_InterlockedExchange((PLONG)&InputBuffer->NumberOfEvents, 0);
while (!IsListEmpty(&InputBuffer->InputEvents))
{
CurrentEntry = RemoveHeadList(&Console->InputBuffer.InputEvents);
CurrentEntry = RemoveHeadList(&InputBuffer->InputEvents);
Event = CONTAINING_RECORD(CurrentEntry, ConsoleInput, ListEntry);
ConsoleFreeHeap(Event);
}
@ -206,6 +209,7 @@ ConDrvInitInputBuffer(IN PCONSOLE Console,
return Status;
Console->InputBuffer.InputBufferSize = InputBufferSize;
Console->InputBuffer.NumberOfEvents = 0;
InitializeListHead(&Console->InputBuffer.InputEvents);
Console->InputBuffer.Mode = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT;
@ -216,7 +220,7 @@ ConDrvInitInputBuffer(IN PCONSOLE Console,
VOID NTAPI
ConDrvDeinitInputBuffer(IN PCONSOLE Console)
{
PurgeInputBuffer(Console);
PurgeInputBuffer(&Console->InputBuffer);
CloseHandle(Console->InputBuffer.ActiveEvent);
}
@ -303,6 +307,7 @@ ConDrvGetConsoleInput(IN PCONSOLE Console,
/* Remove the events from the queue if needed */
if (!KeepEvents)
{
_InterlockedDecrement((PLONG)&InputBuffer->NumberOfEvents);
RemoveEntryList(&Input->ListEntry);
ConsoleFreeHeap(Input);
}
@ -352,9 +357,6 @@ NTSTATUS NTAPI
ConDrvFlushConsoleInputBuffer(IN PCONSOLE Console,
IN PCONSOLE_INPUT_BUFFER InputBuffer)
{
PLIST_ENTRY CurrentEntry;
ConsoleInput* Event;
if (Console == NULL || InputBuffer == NULL)
return STATUS_INVALID_PARAMETER;
@ -362,12 +364,7 @@ ConDrvFlushConsoleInputBuffer(IN PCONSOLE Console,
ASSERT(Console == InputBuffer->Header.Console);
/* Discard all entries in the input event queue */
while (!IsListEmpty(&InputBuffer->InputEvents))
{
CurrentEntry = RemoveHeadList(&InputBuffer->InputEvents);
Event = CONTAINING_RECORD(CurrentEntry, ConsoleInput, ListEntry);
ConsoleFreeHeap(Event);
}
PurgeInputBuffer(InputBuffer);
ResetEvent(InputBuffer->ActiveEvent);
return STATUS_SUCCESS;
@ -378,24 +375,13 @@ ConDrvGetConsoleNumberOfInputEvents(IN PCONSOLE Console,
IN PCONSOLE_INPUT_BUFFER InputBuffer,
OUT PULONG NumberOfEvents)
{
PLIST_ENTRY CurrentInput;
if (Console == NULL || InputBuffer == NULL || NumberOfEvents == NULL)
return STATUS_INVALID_PARAMETER;
/* Validity check */
ASSERT(Console == InputBuffer->Header.Console);
*NumberOfEvents = 0;
/* If there are any events ... */
CurrentInput = InputBuffer->InputEvents.Flink;
while (CurrentInput != &InputBuffer->InputEvents)
{
CurrentInput = CurrentInput->Flink;
(*NumberOfEvents)++;
}
*NumberOfEvents = InputBuffer->NumberOfEvents;
return STATUS_SUCCESS;
}

View file

@ -284,7 +284,7 @@ ConSrvGetConsole(IN PCONSOLE_PROCESS_DATA ProcessData,
CONSOLE_RUNNING,
LockConsole))
{
InterlockedIncrement(&GrabConsole->ReferenceCount);
_InterlockedIncrement(&GrabConsole->ReferenceCount);
*Console = GrabConsole;
Status = STATUS_SUCCESS;
}

View file

@ -1711,7 +1711,7 @@ OnMouse(PGUI_CONSOLE_DATA GuiData, UINT msg, WPARAM wParam, LPARAM lParam)
break;
}
}
else if (Console->InputBuffer.Mode & ENABLE_MOUSE_INPUT)
else if (GetConsoleInputBufferMode(Console) & ENABLE_MOUSE_INPUT)
{
INPUT_RECORD er;
WORD wKeyState = GET_KEYSTATE_WPARAM(wParam);

View file

@ -173,7 +173,7 @@ GuiConsoleInputThread(PVOID Param)
ASSERT(NewWindow == GuiData->hWindow);
InterlockedIncrement(&WindowCount);
_InterlockedIncrement(&WindowCount);
//
// FIXME: TODO: Move everything there into conwnd.c!OnNcCreate()
@ -238,7 +238,7 @@ GuiConsoleInputThread(PVOID Param)
NtSetEvent(GuiData->hGuiTermEvent, NULL);
if (InterlockedDecrement(&WindowCount) == 0)
if (_InterlockedDecrement(&WindowCount) == 0)
{
DPRINT("CONSRV: Going to quit the Input Thread 0x%p\n", InputThreadId);
goto Quit;

View file

@ -125,7 +125,7 @@ ConioProcessKey(PCONSRV_CONSOLE Console, MSG* msg)
if (Fake) return;
/* Process Ctrl-C and Ctrl-Break */
if ( Console->InputBuffer.Mode & ENABLE_PROCESSED_INPUT &&
if ( (GetConsoleInputBufferMode(Console) & ENABLE_PROCESSED_INPUT) &&
Down && (VirtualKeyCode == VK_PAUSE || VirtualKeyCode == 'C') &&
(ShiftState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) || KeyState[VK_CONTROL] & 0x80) )
{

View file

@ -361,6 +361,7 @@ ConSrvTermReadStream(IN OUT PTERMINAL This,
while (!Console->LineComplete && !IsListEmpty(&InputBuffer->InputEvents))
{
/* Remove an input event from the queue */
_InterlockedDecrement((PLONG)&InputBuffer->NumberOfEvents);
CurrentEntry = RemoveHeadList(&InputBuffer->InputEvents);
if (IsListEmpty(&InputBuffer->InputEvents))
{
@ -425,6 +426,7 @@ ConSrvTermReadStream(IN OUT PTERMINAL This,
while (i < NumCharsToRead && !IsListEmpty(&InputBuffer->InputEvents))
{
/* Remove an input event from the queue */
_InterlockedDecrement((PLONG)&InputBuffer->NumberOfEvents);
CurrentEntry = RemoveHeadList(&InputBuffer->InputEvents);
if (IsListEmpty(&InputBuffer->InputEvents))
{

View file

@ -189,9 +189,10 @@ typedef struct _CONSOLE_INPUT_BUFFER
{
CONSOLE_IO_OBJECT Header; /* Object header - MUST BE IN FIRST PLACE */
ULONG InputBufferSize; /* Size of this input buffer -- UNUSED!! */
LIST_ENTRY InputEvents; /* List head for input event queue */
HANDLE ActiveEvent; /* Event set when an input event is added in its queue */
ULONG InputBufferSize; /* Size of this input buffer (maximum number of events) -- UNUSED!! */
ULONG NumberOfEvents; /* Current number of events in the queue */
LIST_ENTRY InputEvents; /* Input events queue list head */
HANDLE ActiveEvent; /* Event set when an input event is added to the queue */
USHORT Mode; /* Input buffer modes */
} CONSOLE_INPUT_BUFFER, *PCONSOLE_INPUT_BUFFER;