2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Driver DLL
4 * FILE: win32ss/user/winsrv/consrv/condrv/coninput.c
5 * PURPOSE: Console Input functions
6 * PROGRAMMERS: Jeffrey Morlan
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 /* INCLUDES *******************************************************************/
17 /* GLOBALS ********************************************************************/
21 * "The lpMultiByteStr and lpWideCharStr pointers must not be the same.
22 * If they are the same, the function fails, and GetLastError returns
23 * ERROR_INVALID_PARAMETER."
25 #define ConsoleInputUnicodeCharToAnsiChar(Console, dChar, sWChar) \
26 ASSERT((ULONG_PTR)dChar != (ULONG_PTR)sWChar); \
27 WideCharToMultiByte((Console)->InputCodePage, 0, (sWChar), 1, (dChar), 1, NULL, NULL)
29 #define ConsoleInputAnsiCharToUnicodeChar(Console, dWChar, sChar) \
30 ASSERT((ULONG_PTR)dWChar != (ULONG_PTR)sChar); \
31 MultiByteToWideChar((Console)->InputCodePage, 0, (sChar), 1, (dWChar), 1)
33 typedef struct ConsoleInput_t
36 INPUT_RECORD InputEvent
;
40 /* PRIVATE FUNCTIONS **********************************************************/
43 ConioInputEventToAnsi(PCONSOLE Console
, PINPUT_RECORD InputEvent
)
45 if (InputEvent
->EventType
== KEY_EVENT
)
47 WCHAR UnicodeChar
= InputEvent
->Event
.KeyEvent
.uChar
.UnicodeChar
;
48 InputEvent
->Event
.KeyEvent
.uChar
.UnicodeChar
= 0;
49 ConsoleInputUnicodeCharToAnsiChar(Console
,
50 &InputEvent
->Event
.KeyEvent
.uChar
.AsciiChar
,
56 ConioInputEventToUnicode(PCONSOLE Console
, PINPUT_RECORD InputEvent
)
58 if (InputEvent
->EventType
== KEY_EVENT
)
60 CHAR AsciiChar
= InputEvent
->Event
.KeyEvent
.uChar
.AsciiChar
;
61 InputEvent
->Event
.KeyEvent
.uChar
.AsciiChar
= 0;
62 ConsoleInputAnsiCharToUnicodeChar(Console
,
63 &InputEvent
->Event
.KeyEvent
.uChar
.UnicodeChar
,
70 ConDrvAddInputEvents(PCONSOLE Console
,
71 PINPUT_RECORD InputRecords
, // InputEvent
72 ULONG NumEventsToWrite
,
73 PULONG NumEventsWritten
,
76 NTSTATUS Status
= STATUS_SUCCESS
;
78 BOOLEAN SetWaitEvent
= FALSE
;
80 if (NumEventsWritten
) *NumEventsWritten
= 0;
83 * When adding many single events, in the case of repeated mouse move or
84 * key down events, we try to coalesce them so that we do not saturate
85 * too quickly the input buffer.
87 if (NumEventsToWrite
== 1 && !IsListEmpty(&Console
->InputBuffer
.InputEvents
))
89 PINPUT_RECORD InputRecord
= InputRecords
; // Only one element
90 PINPUT_RECORD LastInputRecord
;
91 ConsoleInput
* ConInRec
; // Input
93 /* Get the "next" event of the input buffer */
96 /* Get the tail element */
97 ConInRec
= CONTAINING_RECORD(Console
->InputBuffer
.InputEvents
.Blink
,
98 ConsoleInput
, ListEntry
);
102 /* Get the head element */
103 ConInRec
= CONTAINING_RECORD(Console
->InputBuffer
.InputEvents
.Flink
,
104 ConsoleInput
, ListEntry
);
106 LastInputRecord
= &ConInRec
->InputEvent
;
108 if (InputRecord
->EventType
== MOUSE_EVENT
&&
109 InputRecord
->Event
.MouseEvent
.dwEventFlags
== MOUSE_MOVED
)
111 if (LastInputRecord
->EventType
== MOUSE_EVENT
&&
112 LastInputRecord
->Event
.MouseEvent
.dwEventFlags
== MOUSE_MOVED
)
114 /* Update the mouse position */
115 LastInputRecord
->Event
.MouseEvent
.dwMousePosition
.X
=
116 InputRecord
->Event
.MouseEvent
.dwMousePosition
.X
;
117 LastInputRecord
->Event
.MouseEvent
.dwMousePosition
.Y
=
118 InputRecord
->Event
.MouseEvent
.dwMousePosition
.Y
;
121 // return STATUS_SUCCESS;
122 Status
= STATUS_SUCCESS
;
125 else if (InputRecord
->EventType
== KEY_EVENT
&&
126 InputRecord
->Event
.KeyEvent
.bKeyDown
)
128 if (LastInputRecord
->EventType
== KEY_EVENT
&&
129 LastInputRecord
->Event
.KeyEvent
.bKeyDown
&&
130 (LastInputRecord
->Event
.KeyEvent
.wVirtualScanCode
== // Same scancode
131 InputRecord
->Event
.KeyEvent
.wVirtualScanCode
) &&
132 (LastInputRecord
->Event
.KeyEvent
.uChar
.UnicodeChar
== // Same character
133 InputRecord
->Event
.KeyEvent
.uChar
.UnicodeChar
) &&
134 (LastInputRecord
->Event
.KeyEvent
.dwControlKeyState
== // Same Ctrl/Alt/Shift state
135 InputRecord
->Event
.KeyEvent
.dwControlKeyState
) )
137 /* Update the repeat count */
138 LastInputRecord
->Event
.KeyEvent
.wRepeatCount
+=
139 InputRecord
->Event
.KeyEvent
.wRepeatCount
;
142 // return STATUS_SUCCESS;
143 Status
= STATUS_SUCCESS
;
148 /* If we coalesced the only one element, we can quit */
149 if (i
== 1 && Status
== STATUS_SUCCESS
/* && NumEventsToWrite == 1 */)
153 * No event coalesced, add them in the usual way.
158 /* Go to the beginning of the list */
159 // InputRecords = InputRecords;
163 /* Go to the end of the list */
164 InputRecords
= &InputRecords
[NumEventsToWrite
- 1];
167 /* Set the event if the list is going to be non-empty */
168 if (IsListEmpty(&Console
->InputBuffer
.InputEvents
))
171 for (i
= 0; i
< NumEventsToWrite
&& NT_SUCCESS(Status
); ++i
)
173 PINPUT_RECORD InputRecord
;
174 ConsoleInput
* ConInRec
;
178 /* Select the event and go to the next one */
179 InputRecord
= InputRecords
++;
183 /* Select the event and go to the previous one */
184 InputRecord
= InputRecords
--;
187 /* Add event to the queue */
188 ConInRec
= ConsoleAllocHeap(0, sizeof(ConsoleInput
));
189 if (ConInRec
== NULL
)
191 // return STATUS_INSUFFICIENT_RESOURCES;
192 Status
= STATUS_INSUFFICIENT_RESOURCES
;
196 ConInRec
->InputEvent
= *InputRecord
;
200 /* Append the event to the end of the queue */
201 InsertTailList(&Console
->InputBuffer
.InputEvents
, &ConInRec
->ListEntry
);
205 /* Append the event to the beginning of the queue */
206 InsertHeadList(&Console
->InputBuffer
.InputEvents
, &ConInRec
->ListEntry
);
209 // return STATUS_SUCCESS;
210 Status
= STATUS_SUCCESS
;
213 if (SetWaitEvent
) SetEvent(Console
->InputBuffer
.ActiveEvent
);
216 if (NumEventsWritten
) *NumEventsWritten
= i
;
223 PreprocessInput(PCONSOLE Console
,
224 PINPUT_RECORD InputEvent
,
225 ULONG NumEventsToWrite
);
227 PostprocessInput(PCONSOLE Console
);
230 ConioAddInputEvents(PCONSOLE Console
,
231 PINPUT_RECORD InputRecords
, // InputEvent
232 ULONG NumEventsToWrite
,
233 PULONG NumEventsWritten
,
236 NTSTATUS Status
= STATUS_SUCCESS
;
238 if (NumEventsWritten
) *NumEventsWritten
= 0;
241 * This pre-processing code MUST be IN consrv ONLY!!
243 NumEventsToWrite
= PreprocessInput(Console
, InputRecords
, NumEventsToWrite
);
244 if (NumEventsToWrite
== 0) return STATUS_SUCCESS
;
246 Status
= ConDrvAddInputEvents(Console
,
253 * This post-processing code MUST be IN consrv ONLY!!
255 // if (NT_SUCCESS(Status))
256 if (Status
== STATUS_SUCCESS
) PostprocessInput(Console
);
261 /* Move elsewhere...*/
263 ConioProcessInputEvent(PCONSOLE Console
,
264 PINPUT_RECORD InputEvent
)
266 ULONG NumEventsWritten
;
267 return ConioAddInputEvents(Console
,
276 PurgeInputBuffer(PCONSOLE Console
)
278 PLIST_ENTRY CurrentEntry
;
281 while (!IsListEmpty(&Console
->InputBuffer
.InputEvents
))
283 CurrentEntry
= RemoveHeadList(&Console
->InputBuffer
.InputEvents
);
284 Event
= CONTAINING_RECORD(CurrentEntry
, ConsoleInput
, ListEntry
);
285 ConsoleFreeHeap(Event
);
288 CloseHandle(Console
->InputBuffer
.ActiveEvent
);
292 /* PUBLIC DRIVER APIS *********************************************************/
295 ConDrvReadConsole(IN PCONSOLE Console
,
296 IN PCONSOLE_INPUT_BUFFER InputBuffer
,
297 /**/IN PUNICODE_STRING ExeName
/**/OPTIONAL
/**/,/**/
300 IN OUT PCONSOLE_READCONSOLE_CONTROL ReadControl
,
301 IN ULONG NumCharsToRead
,
302 OUT PULONG NumCharsRead OPTIONAL
)
304 // STATUS_PENDING : Wait if more to read ; STATUS_SUCCESS : Don't wait.
305 // NTSTATUS Status; = STATUS_PENDING;
307 if (Console
== NULL
|| InputBuffer
== NULL
|| /* Buffer == NULL || */
308 ReadControl
== NULL
|| ReadControl
->nLength
!= sizeof(CONSOLE_READCONSOLE_CONTROL
))
310 return STATUS_INVALID_PARAMETER
;
313 /* Validity checks */
314 ASSERT(Console
== InputBuffer
->Header
.Console
);
315 ASSERT((Buffer
!= NULL
) || (Buffer
== NULL
&& NumCharsToRead
== 0));
317 /* Call the line-discipline */
318 return TermReadStream(Console
,
328 ConDrvGetConsoleInput(IN PCONSOLE Console
,
329 IN PCONSOLE_INPUT_BUFFER InputBuffer
,
330 IN BOOLEAN KeepEvents
,
331 IN BOOLEAN WaitForMoreEvents
,
333 OUT PINPUT_RECORD InputRecord
,
334 IN ULONG NumEventsToRead
,
335 OUT PULONG NumEventsRead OPTIONAL
)
337 PLIST_ENTRY CurrentInput
;
341 if (Console
== NULL
|| InputBuffer
== NULL
/* || InputRecord == NULL */)
342 return STATUS_INVALID_PARAMETER
;
344 /* Validity checks */
345 ASSERT(Console
== InputBuffer
->Header
.Console
);
346 ASSERT((InputRecord
!= NULL
) || (InputRecord
== NULL
&& NumEventsToRead
== 0));
348 if (NumEventsRead
) *NumEventsRead
= 0;
350 if (IsListEmpty(&InputBuffer
->InputEvents
))
353 * No input is available. Wait for more input if requested,
354 * otherwise, we don't wait, so we return success.
356 return (WaitForMoreEvents
? STATUS_PENDING
: STATUS_SUCCESS
);
359 /* Only get input if there is any */
360 CurrentInput
= InputBuffer
->InputEvents
.Flink
;
362 while ((CurrentInput
!= &InputBuffer
->InputEvents
) && (i
< NumEventsToRead
))
364 Input
= CONTAINING_RECORD(CurrentInput
, ConsoleInput
, ListEntry
);
366 *InputRecord
= Input
->InputEvent
;
370 CurrentInput
= CurrentInput
->Flink
;
372 /* Remove the events from the queue if needed */
375 RemoveEntryList(&Input
->ListEntry
);
376 ConsoleFreeHeap(Input
);
380 if (NumEventsRead
) *NumEventsRead
= i
;
382 /* Now translate everything to ANSI */
387 ConioInputEventToAnsi(InputBuffer
->Header
.Console
, --InputRecord
);
391 if (IsListEmpty(&InputBuffer
->InputEvents
))
393 ResetEvent(InputBuffer
->ActiveEvent
);
396 /* We read all the inputs available, we return success */
397 return STATUS_SUCCESS
;
401 ConDrvWriteConsoleInput(IN PCONSOLE Console
,
402 IN PCONSOLE_INPUT_BUFFER InputBuffer
,
404 IN BOOLEAN AppendToEnd
,
405 IN PINPUT_RECORD InputRecord
,
406 IN ULONG NumEventsToWrite
,
407 OUT PULONG NumEventsWritten OPTIONAL
)
409 NTSTATUS Status
= STATUS_SUCCESS
;
412 if (Console
== NULL
|| InputBuffer
== NULL
/* || InputRecord == NULL */)
413 return STATUS_INVALID_PARAMETER
;
415 /* Validity checks */
416 ASSERT(Console
== InputBuffer
->Header
.Console
);
417 ASSERT((InputRecord
!= NULL
) || (InputRecord
== NULL
&& NumEventsToWrite
== 0));
419 /* First translate everything to UNICODE */
422 for (i
= 0; i
< NumEventsToWrite
; ++i
)
424 ConioInputEventToUnicode(Console
, &InputRecord
[i
]);
428 /* Now, add the events */
429 // if (NumEventsWritten) *NumEventsWritten = 0;
430 // ConDrvAddInputEvents
431 Status
= ConioAddInputEvents(Console
,
436 // if (NumEventsWritten) *NumEventsWritten = i;
442 ConDrvFlushConsoleInputBuffer(IN PCONSOLE Console
,
443 IN PCONSOLE_INPUT_BUFFER InputBuffer
)
445 PLIST_ENTRY CurrentEntry
;
448 if (Console
== NULL
|| InputBuffer
== NULL
)
449 return STATUS_INVALID_PARAMETER
;
452 ASSERT(Console
== InputBuffer
->Header
.Console
);
454 /* Discard all entries in the input event queue */
455 while (!IsListEmpty(&InputBuffer
->InputEvents
))
457 CurrentEntry
= RemoveHeadList(&InputBuffer
->InputEvents
);
458 Event
= CONTAINING_RECORD(CurrentEntry
, ConsoleInput
, ListEntry
);
459 ConsoleFreeHeap(Event
);
461 ResetEvent(InputBuffer
->ActiveEvent
);
463 return STATUS_SUCCESS
;
467 ConDrvGetConsoleNumberOfInputEvents(IN PCONSOLE Console
,
468 IN PCONSOLE_INPUT_BUFFER InputBuffer
,
469 OUT PULONG NumberOfEvents
)
471 PLIST_ENTRY CurrentInput
;
473 if (Console
== NULL
|| InputBuffer
== NULL
|| NumberOfEvents
== NULL
)
474 return STATUS_INVALID_PARAMETER
;
477 ASSERT(Console
== InputBuffer
->Header
.Console
);
481 /* If there are any events ... */
482 CurrentInput
= InputBuffer
->InputEvents
.Flink
;
483 while (CurrentInput
!= &InputBuffer
->InputEvents
)
485 CurrentInput
= CurrentInput
->Flink
;
489 return STATUS_SUCCESS
;