Sync with trunk r63192.
[reactos.git] / dll / win32 / kernel32 / client / console / readwrite.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/readwrite.c
5 * PURPOSE: Win32 Console Client read-write functions
6 * PROGRAMMERS: Emanuele Aliberti
7 * Marty Dill
8 * Filip Navara (xnavara@volny.cz)
9 * Thomas Weidenmueller (w3seek@reactos.org)
10 * Jeffrey Morlan
11 */
12
13 /* INCLUDES *******************************************************************/
14
15 #include <k32.h>
16
17 #define NDEBUG
18 #include <debug.h>
19
20
21 /* PRIVATE FUNCTIONS **********************************************************/
22
23 /******************
24 * Read functions *
25 ******************/
26
27 static
28 BOOL
29 IntReadConsole(HANDLE hConsoleInput,
30 PVOID lpBuffer,
31 DWORD nNumberOfCharsToRead,
32 LPDWORD lpNumberOfCharsRead,
33 PCONSOLE_READCONSOLE_CONTROL pInputControl,
34 BOOL bUnicode)
35 {
36 NTSTATUS Status;
37 CONSOLE_API_MESSAGE ApiMessage;
38 PCONSOLE_READCONSOLE ReadConsoleRequest = &ApiMessage.Data.ReadConsoleRequest;
39 PCSR_CAPTURE_BUFFER CaptureBuffer;
40 ULONG CharSize;
41
42 /* Determine the needed size */
43 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
44 ReadConsoleRequest->BufferSize = nNumberOfCharsToRead * CharSize;
45
46 /* Allocate a Capture Buffer */
47 CaptureBuffer = CsrAllocateCaptureBuffer(1, ReadConsoleRequest->BufferSize);
48 if (CaptureBuffer == NULL)
49 {
50 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
51 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
52 return FALSE;
53 }
54
55 /* Allocate space in the Buffer */
56 CsrAllocateMessagePointer(CaptureBuffer,
57 ReadConsoleRequest->BufferSize,
58 (PVOID*)&ReadConsoleRequest->Buffer);
59
60 /* Set up the data to send to the Console Server */
61 ReadConsoleRequest->InputHandle = hConsoleInput;
62 ReadConsoleRequest->Unicode = bUnicode;
63 ReadConsoleRequest->NrCharactersToRead = nNumberOfCharsToRead;
64 ReadConsoleRequest->NrCharactersRead = 0;
65 ReadConsoleRequest->CtrlWakeupMask = 0;
66 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
67 {
68 /*
69 * From MSDN (ReadConsole function), the description
70 * for pInputControl says:
71 * "This parameter requires Unicode input by default.
72 * For ANSI mode, set this parameter to NULL."
73 */
74 ReadConsoleRequest->NrCharactersRead = pInputControl->nInitialChars;
75 memcpy(ReadConsoleRequest->Buffer,
76 lpBuffer,
77 pInputControl->nInitialChars * sizeof(WCHAR));
78 ReadConsoleRequest->CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
79 }
80
81 /* Call the server */
82 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
83 CaptureBuffer,
84 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsole),
85 sizeof(CONSOLE_READCONSOLE));
86
87 /* Check for success */
88 if (NT_SUCCESS(Status))
89 {
90 memcpy(lpBuffer,
91 ReadConsoleRequest->Buffer,
92 ReadConsoleRequest->NrCharactersRead * CharSize);
93
94 if (lpNumberOfCharsRead != NULL)
95 *lpNumberOfCharsRead = ReadConsoleRequest->NrCharactersRead;
96
97 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
98 pInputControl->dwControlKeyState = ReadConsoleRequest->ControlKeyState;
99 }
100 else
101 {
102 DPRINT1("CSR returned error in ReadConsole\n");
103
104 if (lpNumberOfCharsRead != NULL)
105 *lpNumberOfCharsRead = 0;
106
107 /* Error out */
108 BaseSetLastNTError(Status);
109 }
110
111 CsrFreeCaptureBuffer(CaptureBuffer);
112
113 /* Return TRUE or FALSE */
114 // return TRUE;
115 return (ReadConsoleRequest->NrCharactersRead > 0);
116 // return NT_SUCCESS(Status);
117 }
118
119
120 static
121 BOOL
122 IntGetConsoleInput(HANDLE hConsoleInput,
123 PINPUT_RECORD lpBuffer,
124 DWORD nLength,
125 LPDWORD lpNumberOfEventsRead,
126 WORD wFlags,
127 BOOLEAN bUnicode)
128 {
129 NTSTATUS Status;
130 CONSOLE_API_MESSAGE ApiMessage;
131 PCONSOLE_GETINPUT GetInputRequest = &ApiMessage.Data.GetInputRequest;
132 PCSR_CAPTURE_BUFFER CaptureBuffer;
133 ULONG Size;
134
135 if (lpBuffer == NULL)
136 {
137 SetLastError(ERROR_INVALID_PARAMETER);
138 return FALSE;
139 }
140
141 Size = nLength * sizeof(INPUT_RECORD);
142
143 DPRINT("IntGetConsoleInput: %lx %p\n", Size, lpNumberOfEventsRead);
144
145 /* Allocate a Capture Buffer */
146 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
147 if (CaptureBuffer == NULL)
148 {
149 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
150 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
151 return FALSE;
152 }
153
154 /* Allocate space in the Buffer */
155 CsrAllocateMessagePointer(CaptureBuffer,
156 Size,
157 (PVOID*)&GetInputRequest->InputRecord);
158
159 /* Set up the data to send to the Console Server */
160 GetInputRequest->InputHandle = hConsoleInput;
161 GetInputRequest->InputsRead = 0;
162 GetInputRequest->Length = nLength;
163 GetInputRequest->wFlags = wFlags;
164 GetInputRequest->Unicode = bUnicode;
165
166 /* Call the server */
167 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
168 CaptureBuffer,
169 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetConsoleInput),
170 sizeof(CONSOLE_GETINPUT));
171 DPRINT("Server returned: %x\n", Status);
172
173 /* Check for success */
174 if (NT_SUCCESS(Status))
175 {
176 /* Return the number of events read */
177 DPRINT("Events read: %lx\n", GetInputRequest->InputsRead);
178
179 if (lpNumberOfEventsRead != NULL)
180 *lpNumberOfEventsRead = GetInputRequest->InputsRead;
181
182 /* Copy into the buffer */
183 DPRINT("Copying to buffer\n");
184 RtlCopyMemory(lpBuffer,
185 GetInputRequest->InputRecord,
186 sizeof(INPUT_RECORD) * GetInputRequest->InputsRead);
187 }
188 else
189 {
190 if (lpNumberOfEventsRead != NULL)
191 *lpNumberOfEventsRead = 0;
192
193 /* Error out */
194 BaseSetLastNTError(Status);
195 }
196
197 /* Release the capture buffer */
198 CsrFreeCaptureBuffer(CaptureBuffer);
199
200 /* Return TRUE or FALSE */
201 return (GetInputRequest->InputsRead > 0);
202 // return NT_SUCCESS(Status);
203 }
204
205
206 static
207 BOOL
208 IntReadConsoleOutput(HANDLE hConsoleOutput,
209 PCHAR_INFO lpBuffer,
210 COORD dwBufferSize,
211 COORD dwBufferCoord,
212 PSMALL_RECT lpReadRegion,
213 BOOL bUnicode)
214 {
215 CONSOLE_API_MESSAGE ApiMessage;
216 PCONSOLE_READOUTPUT ReadOutputRequest = &ApiMessage.Data.ReadOutputRequest;
217 PCSR_CAPTURE_BUFFER CaptureBuffer;
218 DWORD Size, SizeX, SizeY;
219
220 if (lpBuffer == NULL)
221 {
222 SetLastError(ERROR_INVALID_PARAMETER);
223 return FALSE;
224 }
225
226 Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO);
227
228 DPRINT("IntReadConsoleOutput: %lx %p\n", Size, lpReadRegion);
229
230 /* Allocate a Capture Buffer */
231 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
232 if (CaptureBuffer == NULL)
233 {
234 DPRINT1("CsrAllocateCaptureBuffer failed with size 0x%x!\n", Size);
235 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
236 return FALSE;
237 }
238
239 /* Allocate space in the Buffer */
240 CsrAllocateMessagePointer(CaptureBuffer,
241 Size,
242 (PVOID*)&ReadOutputRequest->CharInfo);
243
244 /* Set up the data to send to the Console Server */
245 ReadOutputRequest->OutputHandle = hConsoleOutput;
246 ReadOutputRequest->Unicode = bUnicode;
247 ReadOutputRequest->BufferSize = dwBufferSize;
248 ReadOutputRequest->BufferCoord = dwBufferCoord;
249 ReadOutputRequest->ReadRegion = *lpReadRegion;
250
251 /* Call the server */
252 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
253 CaptureBuffer,
254 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutput),
255 sizeof(CONSOLE_READOUTPUT));
256 DPRINT("Server returned: %x\n", ApiMessage.Status);
257
258 /* Check for success */
259 if (NT_SUCCESS(ApiMessage.Status))
260 {
261 /* Copy into the buffer */
262 DPRINT("Copying to buffer\n");
263 SizeX = ReadOutputRequest->ReadRegion.Right -
264 ReadOutputRequest->ReadRegion.Left + 1;
265 SizeY = ReadOutputRequest->ReadRegion.Bottom -
266 ReadOutputRequest->ReadRegion.Top + 1;
267 RtlCopyMemory(lpBuffer,
268 ReadOutputRequest->CharInfo,
269 sizeof(CHAR_INFO) * SizeX * SizeY);
270 }
271 else
272 {
273 /* Error out */
274 BaseSetLastNTError(ApiMessage.Status);
275 }
276
277 /* Return the read region */
278 DPRINT("read region: %p\n", ReadOutputRequest->ReadRegion);
279 *lpReadRegion = ReadOutputRequest->ReadRegion;
280
281 /* Release the capture buffer */
282 CsrFreeCaptureBuffer(CaptureBuffer);
283
284 /* Return TRUE or FALSE */
285 return NT_SUCCESS(ApiMessage.Status);
286 }
287
288
289 static
290 BOOL
291 IntReadConsoleOutputCode(HANDLE hConsoleOutput,
292 CODE_TYPE CodeType,
293 PVOID pCode,
294 DWORD nLength,
295 COORD dwReadCoord,
296 LPDWORD lpNumberOfCodesRead)
297 {
298 NTSTATUS Status;
299 BOOL bRet = TRUE;
300 CONSOLE_API_MESSAGE ApiMessage;
301 PCONSOLE_READOUTPUTCODE ReadOutputCodeRequest = &ApiMessage.Data.ReadOutputCodeRequest;
302 PCSR_CAPTURE_BUFFER CaptureBuffer;
303 ULONG SizeBytes, CodeSize;
304 DWORD CodesRead;
305
306 /* Determine the needed size */
307 switch (CodeType)
308 {
309 case CODE_ASCII:
310 CodeSize = sizeof(CHAR);
311 break;
312
313 case CODE_UNICODE:
314 CodeSize = sizeof(WCHAR);
315 break;
316
317 case CODE_ATTRIBUTE:
318 CodeSize = sizeof(WORD);
319 break;
320
321 default:
322 SetLastError(ERROR_INVALID_PARAMETER);
323 return FALSE;
324 }
325 SizeBytes = nLength * CodeSize;
326
327 /* Allocate a Capture Buffer */
328 CaptureBuffer = CsrAllocateCaptureBuffer(1, SizeBytes);
329 if (CaptureBuffer == NULL)
330 {
331 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
332 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
333 return FALSE;
334 }
335
336 /* Allocate space in the Buffer */
337 CsrAllocateMessagePointer(CaptureBuffer,
338 SizeBytes,
339 (PVOID*)&ReadOutputCodeRequest->pCode.pCode);
340
341 /* Start reading */
342 ReadOutputCodeRequest->OutputHandle = hConsoleOutput;
343 ReadOutputCodeRequest->CodeType = CodeType;
344 ReadOutputCodeRequest->ReadCoord = dwReadCoord;
345
346 ReadOutputCodeRequest->NumCodesToRead = nLength;
347
348 /* Call the server */
349 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
350 CaptureBuffer,
351 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutputString),
352 sizeof(CONSOLE_READOUTPUTCODE));
353
354 /* Check for success */
355 if (NT_SUCCESS(Status))
356 {
357 CodesRead = ReadOutputCodeRequest->CodesRead;
358 memcpy(pCode, ReadOutputCodeRequest->pCode.pCode, CodesRead * CodeSize);
359
360 // ReadOutputCodeRequest->ReadCoord = ReadOutputCodeRequest->EndCoord;
361
362 if (lpNumberOfCodesRead != NULL)
363 *lpNumberOfCodesRead = CodesRead;
364
365 bRet = TRUE;
366 }
367 else
368 {
369 if (lpNumberOfCodesRead != NULL)
370 *lpNumberOfCodesRead = 0;
371
372 /* Error out */
373 BaseSetLastNTError(Status);
374 bRet = FALSE;
375 }
376
377 CsrFreeCaptureBuffer(CaptureBuffer);
378
379 return bRet;
380 }
381
382
383 /*******************
384 * Write functions *
385 *******************/
386
387 static
388 BOOL
389 IntWriteConsole(HANDLE hConsoleOutput,
390 PVOID lpBuffer,
391 DWORD nNumberOfCharsToWrite,
392 LPDWORD lpNumberOfCharsWritten,
393 LPVOID lpReserved,
394 BOOL bUnicode)
395 {
396 NTSTATUS Status;
397 BOOL bRet = TRUE;
398 CONSOLE_API_MESSAGE ApiMessage;
399 PCONSOLE_WRITECONSOLE WriteConsoleRequest = &ApiMessage.Data.WriteConsoleRequest;
400 PCSR_CAPTURE_BUFFER CaptureBuffer;
401 ULONG CharSize;
402
403 /* Determine the needed size */
404 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
405 WriteConsoleRequest->BufferSize = nNumberOfCharsToWrite * CharSize;
406
407 /* Allocate a Capture Buffer */
408 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteConsoleRequest->BufferSize);
409 if (CaptureBuffer == NULL)
410 {
411 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
412 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
413 return FALSE;
414 }
415
416 /* Capture the buffer to write */
417 CsrCaptureMessageBuffer(CaptureBuffer,
418 (PVOID)lpBuffer,
419 WriteConsoleRequest->BufferSize,
420 (PVOID*)&WriteConsoleRequest->Buffer);
421
422 /* Start writing */
423 WriteConsoleRequest->NrCharactersToWrite = nNumberOfCharsToWrite;
424 WriteConsoleRequest->OutputHandle = hConsoleOutput;
425 WriteConsoleRequest->Unicode = bUnicode;
426
427 /* Call the server */
428 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
429 CaptureBuffer,
430 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsole),
431 sizeof(CONSOLE_WRITECONSOLE));
432
433 /* Check for success */
434 if (NT_SUCCESS(Status))
435 {
436 if (lpNumberOfCharsWritten != NULL)
437 *lpNumberOfCharsWritten = WriteConsoleRequest->NrCharactersWritten;
438
439 bRet = TRUE;
440 }
441 else
442 {
443 if (lpNumberOfCharsWritten != NULL)
444 *lpNumberOfCharsWritten = 0;
445
446 /* Error out */
447 BaseSetLastNTError(Status);
448 bRet = FALSE;
449 }
450
451 CsrFreeCaptureBuffer(CaptureBuffer);
452
453 return bRet;
454 }
455
456
457 static
458 BOOL
459 IntWriteConsoleInput(HANDLE hConsoleInput,
460 PINPUT_RECORD lpBuffer,
461 DWORD nLength,
462 LPDWORD lpNumberOfEventsWritten,
463 BOOL bUnicode,
464 BOOL bAppendToEnd)
465 {
466 CONSOLE_API_MESSAGE ApiMessage;
467 PCONSOLE_WRITEINPUT WriteInputRequest = &ApiMessage.Data.WriteInputRequest;
468 PCSR_CAPTURE_BUFFER CaptureBuffer;
469 DWORD Size;
470
471 Size = nLength * sizeof(INPUT_RECORD);
472
473 DPRINT("IntWriteConsoleInput: %lx %p\n", Size, lpNumberOfEventsWritten);
474
475 /* Allocate a Capture Buffer */
476 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
477 if (CaptureBuffer == NULL)
478 {
479 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
480 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
481 return FALSE;
482 }
483
484 /* Capture the user buffer */
485 CsrCaptureMessageBuffer(CaptureBuffer,
486 lpBuffer,
487 Size,
488 (PVOID*)&WriteInputRequest->InputRecord);
489
490 /* Set up the data to send to the Console Server */
491 WriteInputRequest->InputHandle = hConsoleInput;
492 WriteInputRequest->Length = nLength;
493 WriteInputRequest->Unicode = bUnicode;
494 WriteInputRequest->AppendToEnd = bAppendToEnd;
495
496 /* Call the server */
497 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
498 CaptureBuffer,
499 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleInput),
500 sizeof(CONSOLE_WRITEINPUT));
501 DPRINT("Server returned: %x\n", ApiMessage.Status);
502
503 /* Check for success */
504 if (NT_SUCCESS(ApiMessage.Status))
505 {
506 /* Return the number of events read */
507 DPRINT("Events read: %lx\n", WriteInputRequest->Length);
508
509 if (lpNumberOfEventsWritten != NULL)
510 *lpNumberOfEventsWritten = WriteInputRequest->Length;
511 }
512 else
513 {
514 if (lpNumberOfEventsWritten != NULL)
515 *lpNumberOfEventsWritten = 0;
516
517 /* Error out */
518 BaseSetLastNTError(ApiMessage.Status);
519 }
520
521 /* Release the capture buffer */
522 CsrFreeCaptureBuffer(CaptureBuffer);
523
524 /* Return TRUE or FALSE */
525 return NT_SUCCESS(ApiMessage.Status);
526 }
527
528
529 static
530 BOOL
531 IntWriteConsoleOutput(HANDLE hConsoleOutput,
532 CONST CHAR_INFO *lpBuffer,
533 COORD dwBufferSize,
534 COORD dwBufferCoord,
535 PSMALL_RECT lpWriteRegion,
536 BOOL bUnicode)
537 {
538 CONSOLE_API_MESSAGE ApiMessage;
539 PCONSOLE_WRITEOUTPUT WriteOutputRequest = &ApiMessage.Data.WriteOutputRequest;
540 PCSR_CAPTURE_BUFFER CaptureBuffer;
541 ULONG Size;
542
543 if ((lpBuffer == NULL) || (lpWriteRegion == NULL))
544 {
545 SetLastError(ERROR_INVALID_PARAMETER);
546 return FALSE;
547 }
548 /*
549 if (lpWriteRegion == NULL)
550 {
551 SetLastError(ERROR_INVALID_PARAMETER);
552 return FALSE;
553 }
554 */
555
556 Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
557
558 DPRINT("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
559
560 /* Allocate a Capture Buffer */
561 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
562 if (CaptureBuffer == NULL)
563 {
564 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
565 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
566 return FALSE;
567 }
568
569 /* Capture the user buffer */
570 CsrCaptureMessageBuffer(CaptureBuffer,
571 (PVOID)lpBuffer,
572 Size,
573 (PVOID*)&WriteOutputRequest->CharInfo);
574
575 /* Set up the data to send to the Console Server */
576 WriteOutputRequest->OutputHandle = hConsoleOutput;
577 WriteOutputRequest->Unicode = bUnicode;
578 WriteOutputRequest->BufferSize = dwBufferSize;
579 WriteOutputRequest->BufferCoord = dwBufferCoord;
580 WriteOutputRequest->WriteRegion = *lpWriteRegion;
581
582 /* Call the server */
583 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
584 CaptureBuffer,
585 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutput),
586 sizeof(CONSOLE_WRITEOUTPUT));
587 DPRINT("Server returned: %x\n", ApiMessage.Status);
588
589 /* Check for success */
590 if (!NT_SUCCESS(ApiMessage.Status))
591 {
592 /* Error out */
593 BaseSetLastNTError(ApiMessage.Status);
594 }
595
596 /* Return the read region */
597 DPRINT("read region: %p\n", WriteOutputRequest->WriteRegion);
598 *lpWriteRegion = WriteOutputRequest->WriteRegion;
599
600 /* Release the capture buffer */
601 CsrFreeCaptureBuffer(CaptureBuffer);
602
603 /* Return TRUE or FALSE */
604 return NT_SUCCESS(ApiMessage.Status);
605 }
606
607
608 static
609 BOOL
610 IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
611 CODE_TYPE CodeType,
612 CONST VOID *pCode,
613 DWORD nLength,
614 COORD dwWriteCoord,
615 LPDWORD lpNumberOfCodesWritten)
616 {
617 NTSTATUS Status;
618 BOOL bRet = TRUE;
619 CONSOLE_API_MESSAGE ApiMessage;
620 PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &ApiMessage.Data.WriteOutputCodeRequest;
621 PCSR_CAPTURE_BUFFER CaptureBuffer;
622 ULONG CodeSize;
623
624 /* Determine the needed size */
625 switch (CodeType)
626 {
627 case CODE_ASCII:
628 CodeSize = sizeof(CHAR);
629 break;
630
631 case CODE_UNICODE:
632 CodeSize = sizeof(WCHAR);
633 break;
634
635 case CODE_ATTRIBUTE:
636 CodeSize = sizeof(WORD);
637 break;
638
639 default:
640 SetLastError(ERROR_INVALID_PARAMETER);
641 return FALSE;
642 }
643 WriteOutputCodeRequest->BufferSize = nLength * CodeSize;
644
645 /* Allocate a Capture Buffer */
646 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteOutputCodeRequest->BufferSize);
647 if (CaptureBuffer == NULL)
648 {
649 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
650 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
651 return FALSE;
652 }
653
654 /* Capture the buffer to write */
655 CsrCaptureMessageBuffer(CaptureBuffer,
656 (PVOID)pCode,
657 WriteOutputCodeRequest->BufferSize,
658 (PVOID*)&WriteOutputCodeRequest->pCode.pCode);
659
660 /* Start writing */
661 WriteOutputCodeRequest->OutputHandle = hConsoleOutput;
662 WriteOutputCodeRequest->CodeType = CodeType;
663 WriteOutputCodeRequest->Coord = dwWriteCoord;
664
665 WriteOutputCodeRequest->Length = (USHORT)nLength;
666
667 /* Call the server */
668 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
669 CaptureBuffer,
670 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutputString),
671 sizeof(CONSOLE_WRITEOUTPUTCODE));
672
673 /* Check for success */
674 if (NT_SUCCESS(Status))
675 {
676 // WriteOutputCodeRequest->Coord = WriteOutputCodeRequest->EndCoord;
677
678 if (lpNumberOfCodesWritten != NULL)
679 // *lpNumberOfCodesWritten = WriteOutputCodeRequest->NrCharactersWritten;
680 *lpNumberOfCodesWritten = WriteOutputCodeRequest->Length;
681
682 bRet = TRUE;
683 }
684 else
685 {
686 if (lpNumberOfCodesWritten != NULL)
687 *lpNumberOfCodesWritten = 0;
688
689 /* Error out */
690 BaseSetLastNTError(Status);
691 bRet = FALSE;
692 }
693
694 CsrFreeCaptureBuffer(CaptureBuffer);
695
696 return bRet;
697 }
698
699
700 static
701 BOOL
702 IntFillConsoleOutputCode(HANDLE hConsoleOutput,
703 CODE_TYPE CodeType,
704 PVOID pCode,
705 DWORD nLength,
706 COORD dwWriteCoord,
707 LPDWORD lpNumberOfCodesWritten)
708 {
709 NTSTATUS Status;
710 CONSOLE_API_MESSAGE ApiMessage;
711 PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &ApiMessage.Data.FillOutputRequest;
712
713 FillOutputRequest->OutputHandle = hConsoleOutput;
714 FillOutputRequest->CodeType = CodeType;
715
716 switch (CodeType)
717 {
718 case CODE_ASCII:
719 FillOutputRequest->Code.AsciiChar = *(PCHAR)pCode;
720 break;
721
722 case CODE_UNICODE:
723 FillOutputRequest->Code.UnicodeChar = *(PWCHAR)pCode;
724 break;
725
726 case CODE_ATTRIBUTE:
727 FillOutputRequest->Code.Attribute = *(PWORD)pCode;
728 break;
729
730 default:
731 SetLastError(ERROR_INVALID_PARAMETER);
732 return FALSE;
733 }
734
735 /* Set up the data to send to the Console Server */
736 FillOutputRequest->Coord = dwWriteCoord;
737 FillOutputRequest->Length = nLength;
738
739 /* Call the server */
740 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
741 NULL,
742 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFillConsoleOutput),
743 sizeof(CONSOLE_FILLOUTPUTCODE));
744
745 /* Check for success */
746 if (NT_SUCCESS(Status))
747 {
748 if (lpNumberOfCodesWritten != NULL)
749 *lpNumberOfCodesWritten = FillOutputRequest->Length;
750 // *lpNumberOfCodesWritten = Request.Data.FillOutputRequest.NrCharactersWritten;
751
752 return TRUE;
753 }
754 else
755 {
756 if (lpNumberOfCodesWritten != NULL)
757 *lpNumberOfCodesWritten = 0;
758
759 BaseSetLastNTError(Status);
760 return FALSE;
761 }
762 }
763
764
765 /* FUNCTIONS ******************************************************************/
766
767 /******************
768 * Read functions *
769 ******************/
770
771 /*--------------------------------------------------------------
772 * ReadConsoleW
773 *
774 * @implemented
775 */
776 BOOL
777 WINAPI
778 ReadConsoleW(HANDLE hConsoleInput,
779 LPVOID lpBuffer,
780 DWORD nNumberOfCharsToRead,
781 LPDWORD lpNumberOfCharsRead,
782 PCONSOLE_READCONSOLE_CONTROL pInputControl)
783 {
784 return IntReadConsole(hConsoleInput,
785 lpBuffer,
786 nNumberOfCharsToRead,
787 lpNumberOfCharsRead,
788 pInputControl,
789 TRUE);
790 }
791
792
793 /*--------------------------------------------------------------
794 * ReadConsoleA
795 *
796 * @implemented
797 */
798 BOOL
799 WINAPI
800 ReadConsoleA(HANDLE hConsoleInput,
801 LPVOID lpBuffer,
802 DWORD nNumberOfCharsToRead,
803 LPDWORD lpNumberOfCharsRead,
804 PCONSOLE_READCONSOLE_CONTROL pInputControl)
805 {
806 return IntReadConsole(hConsoleInput,
807 lpBuffer,
808 nNumberOfCharsToRead,
809 lpNumberOfCharsRead,
810 NULL,
811 FALSE);
812 }
813
814
815 /*--------------------------------------------------------------
816 * PeekConsoleInputW
817 *
818 * @implemented
819 */
820 BOOL
821 WINAPI
822 PeekConsoleInputW(HANDLE hConsoleInput,
823 PINPUT_RECORD lpBuffer,
824 DWORD nLength,
825 LPDWORD lpNumberOfEventsRead)
826 {
827 return IntGetConsoleInput(hConsoleInput,
828 lpBuffer,
829 nLength,
830 lpNumberOfEventsRead,
831 CONSOLE_READ_KEEPEVENT | CONSOLE_READ_CONTINUE,
832 TRUE);
833 }
834
835
836 /*--------------------------------------------------------------
837 * PeekConsoleInputA
838 *
839 * @implemented
840 */
841 BOOL
842 WINAPI
843 PeekConsoleInputA(HANDLE hConsoleInput,
844 PINPUT_RECORD lpBuffer,
845 DWORD nLength,
846 LPDWORD lpNumberOfEventsRead)
847 {
848 return IntGetConsoleInput(hConsoleInput,
849 lpBuffer,
850 nLength,
851 lpNumberOfEventsRead,
852 CONSOLE_READ_KEEPEVENT | CONSOLE_READ_CONTINUE,
853 FALSE);
854 }
855
856
857 /*--------------------------------------------------------------
858 * ReadConsoleInputW
859 *
860 * @implemented
861 */
862 BOOL
863 WINAPI
864 ReadConsoleInputW(HANDLE hConsoleInput,
865 PINPUT_RECORD lpBuffer,
866 DWORD nLength,
867 LPDWORD lpNumberOfEventsRead)
868 {
869 return IntGetConsoleInput(hConsoleInput,
870 lpBuffer,
871 nLength,
872 lpNumberOfEventsRead,
873 0,
874 TRUE);
875 }
876
877
878 /*--------------------------------------------------------------
879 * ReadConsoleInputA
880 *
881 * @implemented
882 */
883 BOOL
884 WINAPI
885 ReadConsoleInputA(HANDLE hConsoleInput,
886 PINPUT_RECORD lpBuffer,
887 DWORD nLength,
888 LPDWORD lpNumberOfEventsRead)
889 {
890 return IntGetConsoleInput(hConsoleInput,
891 lpBuffer,
892 nLength,
893 lpNumberOfEventsRead,
894 0,
895 FALSE);
896 }
897
898
899 /*--------------------------------------------------------------
900 * ReadConsoleInputExW
901 *
902 * @implemented
903 */
904 BOOL
905 WINAPI
906 ReadConsoleInputExW(HANDLE hConsoleInput,
907 PINPUT_RECORD lpBuffer,
908 DWORD nLength,
909 LPDWORD lpNumberOfEventsRead,
910 WORD wFlags)
911 {
912 return IntGetConsoleInput(hConsoleInput,
913 lpBuffer,
914 nLength,
915 lpNumberOfEventsRead,
916 wFlags,
917 TRUE);
918 }
919
920
921 /*--------------------------------------------------------------
922 * ReadConsoleInputExA
923 *
924 * @implemented
925 */
926 BOOL
927 WINAPI
928 ReadConsoleInputExA(HANDLE hConsoleInput,
929 PINPUT_RECORD lpBuffer,
930 DWORD nLength,
931 LPDWORD lpNumberOfEventsRead,
932 WORD wFlags)
933 {
934 return IntGetConsoleInput(hConsoleInput,
935 lpBuffer,
936 nLength,
937 lpNumberOfEventsRead,
938 wFlags,
939 FALSE);
940 }
941
942
943 /*--------------------------------------------------------------
944 * ReadConsoleOutputW
945 *
946 * @implemented
947 */
948 BOOL
949 WINAPI
950 ReadConsoleOutputW(HANDLE hConsoleOutput,
951 PCHAR_INFO lpBuffer,
952 COORD dwBufferSize,
953 COORD dwBufferCoord,
954 PSMALL_RECT lpReadRegion)
955 {
956 return IntReadConsoleOutput(hConsoleOutput,
957 lpBuffer,
958 dwBufferSize,
959 dwBufferCoord,
960 lpReadRegion,
961 TRUE);
962 }
963
964
965 /*--------------------------------------------------------------
966 * ReadConsoleOutputA
967 *
968 * @implemented
969 */
970 BOOL
971 WINAPI
972 ReadConsoleOutputA(HANDLE hConsoleOutput,
973 PCHAR_INFO lpBuffer,
974 COORD dwBufferSize,
975 COORD dwBufferCoord,
976 PSMALL_RECT lpReadRegion)
977 {
978 return IntReadConsoleOutput(hConsoleOutput,
979 lpBuffer,
980 dwBufferSize,
981 dwBufferCoord,
982 lpReadRegion,
983 FALSE);
984 }
985
986
987 /*--------------------------------------------------------------
988 * ReadConsoleOutputCharacterW
989 *
990 * @implemented
991 */
992 BOOL
993 WINAPI
994 ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
995 LPWSTR lpCharacter,
996 DWORD nLength,
997 COORD dwReadCoord,
998 LPDWORD lpNumberOfCharsRead)
999 {
1000 return IntReadConsoleOutputCode(hConsoleOutput,
1001 CODE_UNICODE,
1002 lpCharacter,
1003 nLength,
1004 dwReadCoord,
1005 lpNumberOfCharsRead);
1006 }
1007
1008
1009 /*--------------------------------------------------------------
1010 * ReadConsoleOutputCharacterA
1011 *
1012 * @implemented
1013 */
1014 BOOL
1015 WINAPI
1016 ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
1017 LPSTR lpCharacter,
1018 DWORD nLength,
1019 COORD dwReadCoord,
1020 LPDWORD lpNumberOfCharsRead)
1021 {
1022 return IntReadConsoleOutputCode(hConsoleOutput,
1023 CODE_ASCII,
1024 lpCharacter,
1025 nLength,
1026 dwReadCoord,
1027 lpNumberOfCharsRead);
1028 }
1029
1030
1031 /*--------------------------------------------------------------
1032 * ReadConsoleOutputAttribute
1033 *
1034 * @implemented
1035 */
1036 BOOL
1037 WINAPI
1038 ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
1039 LPWORD lpAttribute,
1040 DWORD nLength,
1041 COORD dwReadCoord,
1042 LPDWORD lpNumberOfAttrsRead)
1043 {
1044 return IntReadConsoleOutputCode(hConsoleOutput,
1045 CODE_ATTRIBUTE,
1046 lpAttribute,
1047 nLength,
1048 dwReadCoord,
1049 lpNumberOfAttrsRead);
1050 }
1051
1052
1053 /*******************
1054 * Write functions *
1055 *******************/
1056
1057 /*--------------------------------------------------------------
1058 * WriteConsoleW
1059 *
1060 * @implemented
1061 */
1062 BOOL
1063 WINAPI
1064 WriteConsoleW(HANDLE hConsoleOutput,
1065 CONST VOID *lpBuffer,
1066 DWORD nNumberOfCharsToWrite,
1067 LPDWORD lpNumberOfCharsWritten,
1068 LPVOID lpReserved)
1069 {
1070 return IntWriteConsole(hConsoleOutput,
1071 (PVOID)lpBuffer,
1072 nNumberOfCharsToWrite,
1073 lpNumberOfCharsWritten,
1074 lpReserved,
1075 TRUE);
1076 }
1077
1078
1079 /*--------------------------------------------------------------
1080 * WriteConsoleA
1081 *
1082 * @implemented
1083 */
1084 BOOL
1085 WINAPI
1086 WriteConsoleA(HANDLE hConsoleOutput,
1087 CONST VOID *lpBuffer,
1088 DWORD nNumberOfCharsToWrite,
1089 LPDWORD lpNumberOfCharsWritten,
1090 LPVOID lpReserved)
1091 {
1092 return IntWriteConsole(hConsoleOutput,
1093 (PVOID)lpBuffer,
1094 nNumberOfCharsToWrite,
1095 lpNumberOfCharsWritten,
1096 lpReserved,
1097 FALSE);
1098 }
1099
1100
1101 /*--------------------------------------------------------------
1102 * WriteConsoleInputW
1103 *
1104 * @implemented
1105 */
1106 BOOL
1107 WINAPI
1108 WriteConsoleInputW(HANDLE hConsoleInput,
1109 CONST INPUT_RECORD *lpBuffer,
1110 DWORD nLength,
1111 LPDWORD lpNumberOfEventsWritten)
1112 {
1113 return IntWriteConsoleInput(hConsoleInput,
1114 (PINPUT_RECORD)lpBuffer,
1115 nLength,
1116 lpNumberOfEventsWritten,
1117 TRUE,
1118 TRUE);
1119 }
1120
1121
1122 /*--------------------------------------------------------------
1123 * WriteConsoleInputA
1124 *
1125 * @implemented
1126 */
1127 BOOL
1128 WINAPI
1129 WriteConsoleInputA(HANDLE hConsoleInput,
1130 CONST INPUT_RECORD *lpBuffer,
1131 DWORD nLength,
1132 LPDWORD lpNumberOfEventsWritten)
1133 {
1134 return IntWriteConsoleInput(hConsoleInput,
1135 (PINPUT_RECORD)lpBuffer,
1136 nLength,
1137 lpNumberOfEventsWritten,
1138 FALSE,
1139 TRUE);
1140 }
1141
1142
1143 /*--------------------------------------------------------------
1144 * WriteConsoleInputVDMW
1145 *
1146 * @implemented
1147 */
1148 BOOL
1149 WINAPI
1150 WriteConsoleInputVDMW(HANDLE hConsoleInput,
1151 CONST INPUT_RECORD *lpBuffer,
1152 DWORD nLength,
1153 LPDWORD lpNumberOfEventsWritten)
1154 {
1155 return IntWriteConsoleInput(hConsoleInput,
1156 (PINPUT_RECORD)lpBuffer,
1157 nLength,
1158 lpNumberOfEventsWritten,
1159 TRUE,
1160 FALSE);
1161 }
1162
1163
1164 /*--------------------------------------------------------------
1165 * WriteConsoleInputVDMA
1166 *
1167 * @implemented
1168 */
1169 BOOL
1170 WINAPI
1171 WriteConsoleInputVDMA(HANDLE hConsoleInput,
1172 CONST INPUT_RECORD *lpBuffer,
1173 DWORD nLength,
1174 LPDWORD lpNumberOfEventsWritten)
1175 {
1176 return IntWriteConsoleInput(hConsoleInput,
1177 (PINPUT_RECORD)lpBuffer,
1178 nLength,
1179 lpNumberOfEventsWritten,
1180 FALSE,
1181 FALSE);
1182 }
1183
1184
1185 /*--------------------------------------------------------------
1186 * WriteConsoleOutputW
1187 *
1188 * @implemented
1189 */
1190 BOOL
1191 WINAPI
1192 WriteConsoleOutputW(HANDLE hConsoleOutput,
1193 CONST CHAR_INFO *lpBuffer,
1194 COORD dwBufferSize,
1195 COORD dwBufferCoord,
1196 PSMALL_RECT lpWriteRegion)
1197 {
1198 return IntWriteConsoleOutput(hConsoleOutput,
1199 lpBuffer,
1200 dwBufferSize,
1201 dwBufferCoord,
1202 lpWriteRegion,
1203 TRUE);
1204 }
1205
1206
1207 /*--------------------------------------------------------------
1208 * WriteConsoleOutputA
1209 *
1210 * @implemented
1211 */
1212 BOOL
1213 WINAPI
1214 WriteConsoleOutputA(HANDLE hConsoleOutput,
1215 CONST CHAR_INFO *lpBuffer,
1216 COORD dwBufferSize,
1217 COORD dwBufferCoord,
1218 PSMALL_RECT lpWriteRegion)
1219 {
1220 return IntWriteConsoleOutput(hConsoleOutput,
1221 lpBuffer,
1222 dwBufferSize,
1223 dwBufferCoord,
1224 lpWriteRegion,
1225 FALSE);
1226 }
1227
1228
1229 /*--------------------------------------------------------------
1230 * WriteConsoleOutputCharacterW
1231 *
1232 * @implemented
1233 */
1234 BOOL
1235 WINAPI
1236 WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
1237 LPCWSTR lpCharacter,
1238 DWORD nLength,
1239 COORD dwWriteCoord,
1240 LPDWORD lpNumberOfCharsWritten)
1241 {
1242 return IntWriteConsoleOutputCode(hConsoleOutput,
1243 CODE_UNICODE,
1244 lpCharacter,
1245 nLength,
1246 dwWriteCoord,
1247 lpNumberOfCharsWritten);
1248 }
1249
1250
1251 /*--------------------------------------------------------------
1252 * WriteConsoleOutputCharacterA
1253 *
1254 * @implemented
1255 */
1256 BOOL
1257 WINAPI
1258 WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
1259 LPCSTR lpCharacter,
1260 DWORD nLength,
1261 COORD dwWriteCoord,
1262 LPDWORD lpNumberOfCharsWritten)
1263 {
1264 return IntWriteConsoleOutputCode(hConsoleOutput,
1265 CODE_ASCII,
1266 lpCharacter,
1267 nLength,
1268 dwWriteCoord,
1269 lpNumberOfCharsWritten);
1270 }
1271
1272
1273 /*--------------------------------------------------------------
1274 * WriteConsoleOutputAttribute
1275 *
1276 * @implemented
1277 */
1278 BOOL
1279 WINAPI
1280 WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
1281 CONST WORD *lpAttribute,
1282 DWORD nLength,
1283 COORD dwWriteCoord,
1284 LPDWORD lpNumberOfAttrsWritten)
1285 {
1286 return IntWriteConsoleOutputCode(hConsoleOutput,
1287 CODE_ATTRIBUTE,
1288 lpAttribute,
1289 nLength,
1290 dwWriteCoord,
1291 lpNumberOfAttrsWritten);
1292 }
1293
1294
1295 /*--------------------------------------------------------------
1296 * FillConsoleOutputCharacterW
1297 *
1298 * @implemented
1299 */
1300 BOOL
1301 WINAPI
1302 FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1303 WCHAR cCharacter,
1304 DWORD nLength,
1305 COORD dwWriteCoord,
1306 LPDWORD lpNumberOfCharsWritten)
1307 {
1308 return IntFillConsoleOutputCode(hConsoleOutput,
1309 CODE_UNICODE,
1310 &cCharacter,
1311 nLength,
1312 dwWriteCoord,
1313 lpNumberOfCharsWritten);
1314 }
1315
1316
1317 /*--------------------------------------------------------------
1318 * FillConsoleOutputCharacterA
1319 *
1320 * @implemented
1321 */
1322 BOOL
1323 WINAPI
1324 FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
1325 CHAR cCharacter,
1326 DWORD nLength,
1327 COORD dwWriteCoord,
1328 LPDWORD lpNumberOfCharsWritten)
1329 {
1330 return IntFillConsoleOutputCode(hConsoleOutput,
1331 CODE_ASCII,
1332 &cCharacter,
1333 nLength,
1334 dwWriteCoord,
1335 lpNumberOfCharsWritten);
1336 }
1337
1338
1339 /*--------------------------------------------------------------
1340 * FillConsoleOutputAttribute
1341 *
1342 * @implemented
1343 */
1344 BOOL
1345 WINAPI
1346 FillConsoleOutputAttribute(HANDLE hConsoleOutput,
1347 WORD wAttribute,
1348 DWORD nLength,
1349 COORD dwWriteCoord,
1350 LPDWORD lpNumberOfAttrsWritten)
1351 {
1352 return IntFillConsoleOutputCode(hConsoleOutput,
1353 CODE_ATTRIBUTE,
1354 &wAttribute,
1355 nLength,
1356 dwWriteCoord,
1357 lpNumberOfAttrsWritten);
1358 }
1359
1360 /* EOF */