a2c8b9c1561fb0689944d4367e05866f488ff7ee
[reactos.git] / reactos / 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 BOOL bRead,
124 PINPUT_RECORD lpBuffer,
125 DWORD nLength,
126 LPDWORD lpNumberOfEventsRead,
127 BOOL 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->Unicode = bUnicode;
162 GetInputRequest->bRead = bRead;
163 GetInputRequest->InputsRead = 0;
164 GetInputRequest->Length = nLength;
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 {
465 CONSOLE_API_MESSAGE ApiMessage;
466 PCONSOLE_WRITEINPUT WriteInputRequest = &ApiMessage.Data.WriteInputRequest;
467 PCSR_CAPTURE_BUFFER CaptureBuffer;
468 DWORD Size;
469
470 Size = nLength * sizeof(INPUT_RECORD);
471
472 DPRINT("IntWriteConsoleInput: %lx %p\n", Size, lpNumberOfEventsWritten);
473
474 /* Allocate a Capture Buffer */
475 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
476 if (CaptureBuffer == NULL)
477 {
478 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
479 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
480 return FALSE;
481 }
482
483 /* Capture the user buffer */
484 CsrCaptureMessageBuffer(CaptureBuffer,
485 lpBuffer,
486 Size,
487 (PVOID*)&WriteInputRequest->InputRecord);
488
489 /* Set up the data to send to the Console Server */
490 WriteInputRequest->InputHandle = hConsoleInput;
491 WriteInputRequest->Unicode = bUnicode;
492 WriteInputRequest->Length = nLength;
493
494 /* Call the server */
495 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
496 CaptureBuffer,
497 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleInput),
498 sizeof(CONSOLE_WRITEINPUT));
499 DPRINT("Server returned: %x\n", ApiMessage.Status);
500
501 /* Check for success */
502 if (NT_SUCCESS(ApiMessage.Status))
503 {
504 /* Return the number of events read */
505 DPRINT("Events read: %lx\n", WriteInputRequest->Length);
506
507 if (lpNumberOfEventsWritten != NULL)
508 *lpNumberOfEventsWritten = WriteInputRequest->Length;
509 }
510 else
511 {
512 if (lpNumberOfEventsWritten != NULL)
513 *lpNumberOfEventsWritten = 0;
514
515 /* Error out */
516 BaseSetLastNTError(ApiMessage.Status);
517 }
518
519 /* Release the capture buffer */
520 CsrFreeCaptureBuffer(CaptureBuffer);
521
522 /* Return TRUE or FALSE */
523 return NT_SUCCESS(ApiMessage.Status);
524 }
525
526
527 static
528 BOOL
529 IntWriteConsoleOutput(HANDLE hConsoleOutput,
530 CONST CHAR_INFO *lpBuffer,
531 COORD dwBufferSize,
532 COORD dwBufferCoord,
533 PSMALL_RECT lpWriteRegion,
534 BOOL bUnicode)
535 {
536 CONSOLE_API_MESSAGE ApiMessage;
537 PCONSOLE_WRITEOUTPUT WriteOutputRequest = &ApiMessage.Data.WriteOutputRequest;
538 PCSR_CAPTURE_BUFFER CaptureBuffer;
539 ULONG Size;
540
541 if ((lpBuffer == NULL) || (lpWriteRegion == NULL))
542 {
543 SetLastError(ERROR_INVALID_PARAMETER);
544 return FALSE;
545 }
546 /*
547 if (lpWriteRegion == NULL)
548 {
549 SetLastError(ERROR_INVALID_PARAMETER);
550 return FALSE;
551 }
552 */
553
554 Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
555
556 DPRINT("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
557
558 /* Allocate a Capture Buffer */
559 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
560 if (CaptureBuffer == NULL)
561 {
562 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
563 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
564 return FALSE;
565 }
566
567 /* Capture the user buffer */
568 CsrCaptureMessageBuffer(CaptureBuffer,
569 (PVOID)lpBuffer,
570 Size,
571 (PVOID*)&WriteOutputRequest->CharInfo);
572
573 /* Set up the data to send to the Console Server */
574 WriteOutputRequest->OutputHandle = hConsoleOutput;
575 WriteOutputRequest->Unicode = bUnicode;
576 WriteOutputRequest->BufferSize = dwBufferSize;
577 WriteOutputRequest->BufferCoord = dwBufferCoord;
578 WriteOutputRequest->WriteRegion = *lpWriteRegion;
579
580 /* Call the server */
581 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
582 CaptureBuffer,
583 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutput),
584 sizeof(CONSOLE_WRITEOUTPUT));
585 DPRINT("Server returned: %x\n", ApiMessage.Status);
586
587 /* Check for success */
588 if (!NT_SUCCESS(ApiMessage.Status))
589 {
590 /* Error out */
591 BaseSetLastNTError(ApiMessage.Status);
592 }
593
594 /* Return the read region */
595 DPRINT("read region: %p\n", WriteOutputRequest->WriteRegion);
596 *lpWriteRegion = WriteOutputRequest->WriteRegion;
597
598 /* Release the capture buffer */
599 CsrFreeCaptureBuffer(CaptureBuffer);
600
601 /* Return TRUE or FALSE */
602 return NT_SUCCESS(ApiMessage.Status);
603 }
604
605
606 static
607 BOOL
608 IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
609 CODE_TYPE CodeType,
610 CONST VOID *pCode,
611 DWORD nLength,
612 COORD dwWriteCoord,
613 LPDWORD lpNumberOfCodesWritten)
614 {
615 NTSTATUS Status;
616 BOOL bRet = TRUE;
617 CONSOLE_API_MESSAGE ApiMessage;
618 PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &ApiMessage.Data.WriteOutputCodeRequest;
619 PCSR_CAPTURE_BUFFER CaptureBuffer;
620 ULONG CodeSize;
621
622 /* Determine the needed size */
623 switch (CodeType)
624 {
625 case CODE_ASCII:
626 CodeSize = sizeof(CHAR);
627 break;
628
629 case CODE_UNICODE:
630 CodeSize = sizeof(WCHAR);
631 break;
632
633 case CODE_ATTRIBUTE:
634 CodeSize = sizeof(WORD);
635 break;
636
637 default:
638 SetLastError(ERROR_INVALID_PARAMETER);
639 return FALSE;
640 }
641 WriteOutputCodeRequest->BufferSize = nLength * CodeSize;
642
643 /* Allocate a Capture Buffer */
644 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteOutputCodeRequest->BufferSize);
645 if (CaptureBuffer == NULL)
646 {
647 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
648 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
649 return FALSE;
650 }
651
652 /* Capture the buffer to write */
653 CsrCaptureMessageBuffer(CaptureBuffer,
654 (PVOID)pCode,
655 WriteOutputCodeRequest->BufferSize,
656 (PVOID*)&WriteOutputCodeRequest->pCode.pCode);
657
658 /* Start writing */
659 WriteOutputCodeRequest->OutputHandle = hConsoleOutput;
660 WriteOutputCodeRequest->CodeType = CodeType;
661 WriteOutputCodeRequest->Coord = dwWriteCoord;
662
663 WriteOutputCodeRequest->Length = (USHORT)nLength;
664
665 /* Call the server */
666 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
667 CaptureBuffer,
668 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutputString),
669 sizeof(CONSOLE_WRITEOUTPUTCODE));
670
671 /* Check for success */
672 if (NT_SUCCESS(Status))
673 {
674 // WriteOutputCodeRequest->Coord = WriteOutputCodeRequest->EndCoord;
675
676 if (lpNumberOfCodesWritten != NULL)
677 // *lpNumberOfCodesWritten = WriteOutputCodeRequest->NrCharactersWritten;
678 *lpNumberOfCodesWritten = WriteOutputCodeRequest->Length;
679
680 bRet = TRUE;
681 }
682 else
683 {
684 if (lpNumberOfCodesWritten != NULL)
685 *lpNumberOfCodesWritten = 0;
686
687 /* Error out */
688 BaseSetLastNTError(Status);
689 bRet = FALSE;
690 }
691
692 CsrFreeCaptureBuffer(CaptureBuffer);
693
694 return bRet;
695 }
696
697
698 static
699 BOOL
700 IntFillConsoleOutputCode(HANDLE hConsoleOutput,
701 CODE_TYPE CodeType,
702 PVOID pCode,
703 DWORD nLength,
704 COORD dwWriteCoord,
705 LPDWORD lpNumberOfCodesWritten)
706 {
707 NTSTATUS Status;
708 CONSOLE_API_MESSAGE ApiMessage;
709 PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &ApiMessage.Data.FillOutputRequest;
710
711 FillOutputRequest->OutputHandle = hConsoleOutput;
712 FillOutputRequest->CodeType = CodeType;
713
714 switch (CodeType)
715 {
716 case CODE_ASCII:
717 FillOutputRequest->Code.AsciiChar = *(PCHAR)pCode;
718 break;
719
720 case CODE_UNICODE:
721 FillOutputRequest->Code.UnicodeChar = *(PWCHAR)pCode;
722 break;
723
724 case CODE_ATTRIBUTE:
725 FillOutputRequest->Code.Attribute = *(PWORD)pCode;
726 break;
727
728 default:
729 SetLastError(ERROR_INVALID_PARAMETER);
730 return FALSE;
731 }
732
733 /* Set up the data to send to the Console Server */
734 FillOutputRequest->Coord = dwWriteCoord;
735 FillOutputRequest->Length = nLength;
736
737 /* Call the server */
738 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
739 NULL,
740 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFillConsoleOutput),
741 sizeof(CONSOLE_FILLOUTPUTCODE));
742
743 /* Check for success */
744 if (NT_SUCCESS(Status))
745 {
746 if (lpNumberOfCodesWritten != NULL)
747 *lpNumberOfCodesWritten = FillOutputRequest->Length;
748 // *lpNumberOfCodesWritten = Request.Data.FillOutputRequest.NrCharactersWritten;
749
750 return TRUE;
751 }
752 else
753 {
754 if (lpNumberOfCodesWritten != NULL)
755 *lpNumberOfCodesWritten = 0;
756
757 BaseSetLastNTError(Status);
758 return FALSE;
759 }
760 }
761
762
763 /* FUNCTIONS ******************************************************************/
764
765 /******************
766 * Read functions *
767 ******************/
768
769 /*--------------------------------------------------------------
770 * ReadConsoleW
771 *
772 * @implemented
773 */
774 BOOL
775 WINAPI
776 ReadConsoleW(HANDLE hConsoleInput,
777 LPVOID lpBuffer,
778 DWORD nNumberOfCharsToRead,
779 LPDWORD lpNumberOfCharsRead,
780 PCONSOLE_READCONSOLE_CONTROL pInputControl)
781 {
782 return IntReadConsole(hConsoleInput,
783 lpBuffer,
784 nNumberOfCharsToRead,
785 lpNumberOfCharsRead,
786 pInputControl,
787 TRUE);
788 }
789
790
791 /*--------------------------------------------------------------
792 * ReadConsoleA
793 *
794 * @implemented
795 */
796 BOOL
797 WINAPI
798 ReadConsoleA(HANDLE hConsoleInput,
799 LPVOID lpBuffer,
800 DWORD nNumberOfCharsToRead,
801 LPDWORD lpNumberOfCharsRead,
802 PCONSOLE_READCONSOLE_CONTROL pInputControl)
803 {
804 return IntReadConsole(hConsoleInput,
805 lpBuffer,
806 nNumberOfCharsToRead,
807 lpNumberOfCharsRead,
808 NULL,
809 FALSE);
810 }
811
812
813 /*--------------------------------------------------------------
814 * PeekConsoleInputW
815 *
816 * @implemented
817 */
818 BOOL
819 WINAPI
820 PeekConsoleInputW(HANDLE hConsoleInput,
821 PINPUT_RECORD lpBuffer,
822 DWORD nLength,
823 LPDWORD lpNumberOfEventsRead)
824 {
825 return IntGetConsoleInput(hConsoleInput,
826 FALSE,
827 lpBuffer,
828 nLength,
829 lpNumberOfEventsRead,
830 TRUE);
831 }
832
833
834 /*--------------------------------------------------------------
835 * PeekConsoleInputA
836 *
837 * @implemented
838 */
839 BOOL
840 WINAPI
841 PeekConsoleInputA(HANDLE hConsoleInput,
842 PINPUT_RECORD lpBuffer,
843 DWORD nLength,
844 LPDWORD lpNumberOfEventsRead)
845 {
846 return IntGetConsoleInput(hConsoleInput,
847 FALSE,
848 lpBuffer,
849 nLength,
850 lpNumberOfEventsRead,
851 FALSE);
852 }
853
854
855 /*--------------------------------------------------------------
856 * ReadConsoleInputW
857 *
858 * @implemented
859 */
860 BOOL
861 WINAPI
862 ReadConsoleInputW(HANDLE hConsoleInput,
863 PINPUT_RECORD lpBuffer,
864 DWORD nLength,
865 LPDWORD lpNumberOfEventsRead)
866 {
867 return IntGetConsoleInput(hConsoleInput,
868 TRUE,
869 lpBuffer,
870 nLength,
871 lpNumberOfEventsRead,
872 TRUE);
873 }
874
875
876 /*--------------------------------------------------------------
877 * ReadConsoleInputA
878 *
879 * @implemented
880 */
881 BOOL
882 WINAPI
883 ReadConsoleInputA(HANDLE hConsoleInput,
884 PINPUT_RECORD lpBuffer,
885 DWORD nLength,
886 LPDWORD lpNumberOfEventsRead)
887 {
888 return IntGetConsoleInput(hConsoleInput,
889 TRUE,
890 lpBuffer,
891 nLength,
892 lpNumberOfEventsRead,
893 FALSE);
894 }
895
896
897 BOOL
898 WINAPI
899 ReadConsoleInputExW(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
900 {
901 STUB;
902 return FALSE;
903 }
904
905
906 BOOL
907 WINAPI
908 ReadConsoleInputExA(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
909 {
910 STUB;
911 return FALSE;
912 }
913
914
915 /*--------------------------------------------------------------
916 * ReadConsoleOutputW
917 *
918 * @implemented
919 */
920 BOOL
921 WINAPI
922 ReadConsoleOutputW(HANDLE hConsoleOutput,
923 PCHAR_INFO lpBuffer,
924 COORD dwBufferSize,
925 COORD dwBufferCoord,
926 PSMALL_RECT lpReadRegion)
927 {
928 return IntReadConsoleOutput(hConsoleOutput,
929 lpBuffer,
930 dwBufferSize,
931 dwBufferCoord,
932 lpReadRegion,
933 TRUE);
934 }
935
936
937 /*--------------------------------------------------------------
938 * ReadConsoleOutputA
939 *
940 * @implemented
941 */
942 BOOL
943 WINAPI
944 ReadConsoleOutputA(HANDLE hConsoleOutput,
945 PCHAR_INFO lpBuffer,
946 COORD dwBufferSize,
947 COORD dwBufferCoord,
948 PSMALL_RECT lpReadRegion)
949 {
950 return IntReadConsoleOutput(hConsoleOutput,
951 lpBuffer,
952 dwBufferSize,
953 dwBufferCoord,
954 lpReadRegion,
955 FALSE);
956 }
957
958
959 /*--------------------------------------------------------------
960 * ReadConsoleOutputCharacterW
961 *
962 * @implemented
963 */
964 BOOL
965 WINAPI
966 ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
967 LPWSTR lpCharacter,
968 DWORD nLength,
969 COORD dwReadCoord,
970 LPDWORD lpNumberOfCharsRead)
971 {
972 return IntReadConsoleOutputCode(hConsoleOutput,
973 CODE_UNICODE,
974 lpCharacter,
975 nLength,
976 dwReadCoord,
977 lpNumberOfCharsRead);
978 }
979
980
981 /*--------------------------------------------------------------
982 * ReadConsoleOutputCharacterA
983 *
984 * @implemented
985 */
986 BOOL
987 WINAPI
988 ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
989 LPSTR lpCharacter,
990 DWORD nLength,
991 COORD dwReadCoord,
992 LPDWORD lpNumberOfCharsRead)
993 {
994 return IntReadConsoleOutputCode(hConsoleOutput,
995 CODE_ASCII,
996 lpCharacter,
997 nLength,
998 dwReadCoord,
999 lpNumberOfCharsRead);
1000 }
1001
1002
1003 /*--------------------------------------------------------------
1004 * ReadConsoleOutputAttribute
1005 *
1006 * @implemented
1007 */
1008 BOOL
1009 WINAPI
1010 ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
1011 LPWORD lpAttribute,
1012 DWORD nLength,
1013 COORD dwReadCoord,
1014 LPDWORD lpNumberOfAttrsRead)
1015 {
1016 return IntReadConsoleOutputCode(hConsoleOutput,
1017 CODE_ATTRIBUTE,
1018 lpAttribute,
1019 nLength,
1020 dwReadCoord,
1021 lpNumberOfAttrsRead);
1022 }
1023
1024
1025 /*******************
1026 * Write functions *
1027 *******************/
1028
1029 /*--------------------------------------------------------------
1030 * WriteConsoleW
1031 *
1032 * @implemented
1033 */
1034 BOOL
1035 WINAPI
1036 WriteConsoleW(HANDLE hConsoleOutput,
1037 CONST VOID *lpBuffer,
1038 DWORD nNumberOfCharsToWrite,
1039 LPDWORD lpNumberOfCharsWritten,
1040 LPVOID lpReserved)
1041 {
1042 return IntWriteConsole(hConsoleOutput,
1043 (PVOID)lpBuffer,
1044 nNumberOfCharsToWrite,
1045 lpNumberOfCharsWritten,
1046 lpReserved,
1047 TRUE);
1048 }
1049
1050
1051 /*--------------------------------------------------------------
1052 * WriteConsoleA
1053 *
1054 * @implemented
1055 */
1056 BOOL
1057 WINAPI
1058 WriteConsoleA(HANDLE hConsoleOutput,
1059 CONST VOID *lpBuffer,
1060 DWORD nNumberOfCharsToWrite,
1061 LPDWORD lpNumberOfCharsWritten,
1062 LPVOID lpReserved)
1063 {
1064 return IntWriteConsole(hConsoleOutput,
1065 (PVOID)lpBuffer,
1066 nNumberOfCharsToWrite,
1067 lpNumberOfCharsWritten,
1068 lpReserved,
1069 FALSE);
1070 }
1071
1072
1073 /*--------------------------------------------------------------
1074 * WriteConsoleInputW
1075 *
1076 * @implemented
1077 */
1078 BOOL
1079 WINAPI
1080 WriteConsoleInputW(HANDLE hConsoleInput,
1081 CONST INPUT_RECORD *lpBuffer,
1082 DWORD nLength,
1083 LPDWORD lpNumberOfEventsWritten)
1084 {
1085 return IntWriteConsoleInput(hConsoleInput,
1086 (PINPUT_RECORD)lpBuffer,
1087 nLength,
1088 lpNumberOfEventsWritten,
1089 TRUE);
1090 }
1091
1092
1093 /*--------------------------------------------------------------
1094 * WriteConsoleInputA
1095 *
1096 * @implemented
1097 */
1098 BOOL
1099 WINAPI
1100 WriteConsoleInputA(HANDLE hConsoleInput,
1101 CONST INPUT_RECORD *lpBuffer,
1102 DWORD nLength,
1103 LPDWORD lpNumberOfEventsWritten)
1104 {
1105 return IntWriteConsoleInput(hConsoleInput,
1106 (PINPUT_RECORD)lpBuffer,
1107 nLength,
1108 lpNumberOfEventsWritten,
1109 FALSE);
1110 }
1111
1112
1113 /*--------------------------------------------------------------
1114 * WriteConsoleOutputW
1115 *
1116 * @implemented
1117 */
1118 BOOL
1119 WINAPI
1120 WriteConsoleOutputW(HANDLE hConsoleOutput,
1121 CONST CHAR_INFO *lpBuffer,
1122 COORD dwBufferSize,
1123 COORD dwBufferCoord,
1124 PSMALL_RECT lpWriteRegion)
1125 {
1126 return IntWriteConsoleOutput(hConsoleOutput,
1127 lpBuffer,
1128 dwBufferSize,
1129 dwBufferCoord,
1130 lpWriteRegion,
1131 TRUE);
1132 }
1133
1134
1135 /*--------------------------------------------------------------
1136 * WriteConsoleOutputA
1137 *
1138 * @implemented
1139 */
1140 BOOL
1141 WINAPI
1142 WriteConsoleOutputA(HANDLE hConsoleOutput,
1143 CONST CHAR_INFO *lpBuffer,
1144 COORD dwBufferSize,
1145 COORD dwBufferCoord,
1146 PSMALL_RECT lpWriteRegion)
1147 {
1148 return IntWriteConsoleOutput(hConsoleOutput,
1149 lpBuffer,
1150 dwBufferSize,
1151 dwBufferCoord,
1152 lpWriteRegion,
1153 FALSE);
1154 }
1155
1156
1157 /*--------------------------------------------------------------
1158 * WriteConsoleOutputCharacterW
1159 *
1160 * @implemented
1161 */
1162 BOOL
1163 WINAPI
1164 WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
1165 LPCWSTR lpCharacter,
1166 DWORD nLength,
1167 COORD dwWriteCoord,
1168 LPDWORD lpNumberOfCharsWritten)
1169 {
1170 return IntWriteConsoleOutputCode(hConsoleOutput,
1171 CODE_UNICODE,
1172 lpCharacter,
1173 nLength,
1174 dwWriteCoord,
1175 lpNumberOfCharsWritten);
1176 }
1177
1178
1179 /*--------------------------------------------------------------
1180 * WriteConsoleOutputCharacterA
1181 *
1182 * @implemented
1183 */
1184 BOOL
1185 WINAPI
1186 WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
1187 LPCSTR lpCharacter,
1188 DWORD nLength,
1189 COORD dwWriteCoord,
1190 LPDWORD lpNumberOfCharsWritten)
1191 {
1192 return IntWriteConsoleOutputCode(hConsoleOutput,
1193 CODE_ASCII,
1194 lpCharacter,
1195 nLength,
1196 dwWriteCoord,
1197 lpNumberOfCharsWritten);
1198 }
1199
1200
1201 /*--------------------------------------------------------------
1202 * WriteConsoleOutputAttribute
1203 *
1204 * @implemented
1205 */
1206 BOOL
1207 WINAPI
1208 WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
1209 CONST WORD *lpAttribute,
1210 DWORD nLength,
1211 COORD dwWriteCoord,
1212 LPDWORD lpNumberOfAttrsWritten)
1213 {
1214 return IntWriteConsoleOutputCode(hConsoleOutput,
1215 CODE_ATTRIBUTE,
1216 lpAttribute,
1217 nLength,
1218 dwWriteCoord,
1219 lpNumberOfAttrsWritten);
1220 }
1221
1222
1223 /*--------------------------------------------------------------
1224 * FillConsoleOutputCharacterW
1225 *
1226 * @implemented
1227 */
1228 BOOL
1229 WINAPI
1230 FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1231 WCHAR cCharacter,
1232 DWORD nLength,
1233 COORD dwWriteCoord,
1234 LPDWORD lpNumberOfCharsWritten)
1235 {
1236 return IntFillConsoleOutputCode(hConsoleOutput,
1237 CODE_UNICODE,
1238 &cCharacter,
1239 nLength,
1240 dwWriteCoord,
1241 lpNumberOfCharsWritten);
1242 }
1243
1244
1245 /*--------------------------------------------------------------
1246 * FillConsoleOutputCharacterA
1247 *
1248 * @implemented
1249 */
1250 BOOL
1251 WINAPI
1252 FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
1253 CHAR cCharacter,
1254 DWORD nLength,
1255 COORD dwWriteCoord,
1256 LPDWORD lpNumberOfCharsWritten)
1257 {
1258 return IntFillConsoleOutputCode(hConsoleOutput,
1259 CODE_ASCII,
1260 &cCharacter,
1261 nLength,
1262 dwWriteCoord,
1263 lpNumberOfCharsWritten);
1264 }
1265
1266
1267 /*--------------------------------------------------------------
1268 * FillConsoleOutputAttribute
1269 *
1270 * @implemented
1271 */
1272 BOOL
1273 WINAPI
1274 FillConsoleOutputAttribute(HANDLE hConsoleOutput,
1275 WORD wAttribute,
1276 DWORD nLength,
1277 COORD dwWriteCoord,
1278 LPDWORD lpNumberOfAttrsWritten)
1279 {
1280 return IntFillConsoleOutputCode(hConsoleOutput,
1281 CODE_ATTRIBUTE,
1282 &wAttribute,
1283 nLength,
1284 dwWriteCoord,
1285 lpNumberOfAttrsWritten);
1286 }
1287
1288 /* EOF */