[KERNEL32][CONSRV]
[reactos.git] / win32ss / user / winsrv / consrv / conoutput.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: win32ss/user/winsrv/consrv/conoutput.c
5 * PURPOSE: General Console Output Functions
6 * PROGRAMMERS: Jeffrey Morlan
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include "consrv.h"
13
14 #define NDEBUG
15 #include <debug.h>
16
17 /* PUBLIC SERVER APIS *********************************************************/
18
19 NTSTATUS NTAPI
20 ConDrvInvalidateBitMapRect(IN PCONSOLE Console,
21 IN PCONSOLE_SCREEN_BUFFER Buffer,
22 IN PSMALL_RECT Region);
23 CSR_API(SrvInvalidateBitMapRect)
24 {
25 NTSTATUS Status;
26 PCONSOLE_INVALIDATEDIBITS InvalidateDIBitsRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.InvalidateDIBitsRequest;
27 PCONSOLE_SCREEN_BUFFER Buffer;
28
29 DPRINT("SrvInvalidateBitMapRect\n");
30
31 Status = ConSrvGetScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
32 InvalidateDIBitsRequest->OutputHandle,
33 &Buffer, GENERIC_READ, TRUE);
34 if (!NT_SUCCESS(Status)) return Status;
35
36 Status = ConDrvInvalidateBitMapRect(Buffer->Header.Console,
37 Buffer,
38 &InvalidateDIBitsRequest->Region);
39
40 ConSrvReleaseScreenBuffer(Buffer, TRUE);
41 return Status;
42 }
43
44 NTSTATUS NTAPI
45 ConDrvSetConsolePalette(IN PCONSOLE Console,
46 // IN PGRAPHICS_SCREEN_BUFFER Buffer,
47 IN PCONSOLE_SCREEN_BUFFER Buffer,
48 IN HPALETTE PaletteHandle,
49 IN UINT PaletteUsage);
50 CSR_API(SrvSetConsolePalette)
51 {
52 NTSTATUS Status;
53 PCONSOLE_SETPALETTE SetPaletteRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetPaletteRequest;
54 // PGRAPHICS_SCREEN_BUFFER Buffer;
55 PCONSOLE_SCREEN_BUFFER Buffer;
56
57 DPRINT("SrvSetConsolePalette\n");
58
59 // NOTE: Tests show that this function is used only for graphics screen buffers
60 // and otherwise it returns FALSE + sets last error to invalid handle.
61 // I think it's ridiculous, because if you are in text mode, simulating
62 // a change of VGA palette via DAC registers (done by a call to SetConsolePalette)
63 // cannot be done... So I allow it in ReactOS !
64 /*
65 Status = ConSrvGetGraphicsBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
66 SetPaletteRequest->OutputHandle,
67 &Buffer, GENERIC_WRITE, TRUE);
68 */
69 Status = ConSrvGetScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
70 SetPaletteRequest->OutputHandle,
71 &Buffer, GENERIC_WRITE, TRUE);
72 if (!NT_SUCCESS(Status)) return Status;
73
74 /*
75 * Make the palette handle public, so that it can be
76 * used by other threads calling GDI functions on it.
77 * Indeed, the palette handle comes from a console app
78 * calling ourselves, running in CSRSS.
79 */
80 NtUserConsoleControl(ConsoleMakePalettePublic,
81 &SetPaletteRequest->PaletteHandle,
82 sizeof(SetPaletteRequest->PaletteHandle));
83
84 Status = ConDrvSetConsolePalette(Buffer->Header.Console,
85 Buffer,
86 SetPaletteRequest->PaletteHandle,
87 SetPaletteRequest->Usage);
88
89 ConSrvReleaseScreenBuffer(Buffer, TRUE);
90 return Status;
91 }
92
93 NTSTATUS NTAPI
94 ConDrvGetConsoleCursorInfo(IN PCONSOLE Console,
95 IN PTEXTMODE_SCREEN_BUFFER Buffer,
96 OUT PCONSOLE_CURSOR_INFO CursorInfo);
97 CSR_API(SrvGetConsoleCursorInfo)
98 {
99 NTSTATUS Status;
100 PCONSOLE_GETSETCURSORINFO CursorInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CursorInfoRequest;
101 PTEXTMODE_SCREEN_BUFFER Buffer;
102
103 DPRINT("SrvGetConsoleCursorInfo\n");
104
105 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
106 CursorInfoRequest->OutputHandle,
107 &Buffer, GENERIC_READ, TRUE);
108 if (!NT_SUCCESS(Status)) return Status;
109
110 Status = ConDrvGetConsoleCursorInfo(Buffer->Header.Console,
111 Buffer,
112 &CursorInfoRequest->Info);
113
114 ConSrvReleaseScreenBuffer(Buffer, TRUE);
115 return Status;
116 }
117
118 NTSTATUS NTAPI
119 ConDrvSetConsoleCursorInfo(IN PCONSOLE Console,
120 IN PTEXTMODE_SCREEN_BUFFER Buffer,
121 IN PCONSOLE_CURSOR_INFO CursorInfo);
122 CSR_API(SrvSetConsoleCursorInfo)
123 {
124 NTSTATUS Status;
125 PCONSOLE_GETSETCURSORINFO CursorInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CursorInfoRequest;
126 PTEXTMODE_SCREEN_BUFFER Buffer;
127
128 DPRINT("SrvSetConsoleCursorInfo\n");
129
130 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
131 CursorInfoRequest->OutputHandle,
132 &Buffer, GENERIC_WRITE, TRUE);
133 if (!NT_SUCCESS(Status)) return Status;
134
135 Status = ConDrvSetConsoleCursorInfo(Buffer->Header.Console,
136 Buffer,
137 &CursorInfoRequest->Info);
138
139 ConSrvReleaseScreenBuffer(Buffer, TRUE);
140 return Status;
141 }
142
143 NTSTATUS NTAPI
144 ConDrvSetConsoleCursorPosition(IN PCONSOLE Console,
145 IN PTEXTMODE_SCREEN_BUFFER Buffer,
146 IN PCOORD Position);
147 CSR_API(SrvSetConsoleCursorPosition)
148 {
149 NTSTATUS Status;
150 PCONSOLE_SETCURSORPOSITION SetCursorPositionRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetCursorPositionRequest;
151 PTEXTMODE_SCREEN_BUFFER Buffer;
152
153 DPRINT("SrvSetConsoleCursorPosition\n");
154
155 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
156 SetCursorPositionRequest->OutputHandle,
157 &Buffer, GENERIC_WRITE, TRUE);
158 if (!NT_SUCCESS(Status)) return Status;
159
160 Status = ConDrvSetConsoleCursorPosition(Buffer->Header.Console,
161 Buffer,
162 &SetCursorPositionRequest->Position);
163
164 ConSrvReleaseScreenBuffer(Buffer, TRUE);
165 return Status;
166 }
167
168 CSR_API(SrvCreateConsoleScreenBuffer)
169 {
170 NTSTATUS Status = STATUS_INVALID_PARAMETER;
171 PCONSOLE_CREATESCREENBUFFER CreateScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CreateScreenBufferRequest;
172 PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
173 PCONSOLE Console;
174 PCONSOLE_SCREEN_BUFFER Buff;
175
176 PVOID ScreenBufferInfo = NULL;
177 TEXTMODE_BUFFER_INFO TextModeInfo = {{80, 25},
178 DEFAULT_SCREEN_ATTRIB,
179 DEFAULT_POPUP_ATTRIB ,
180 TRUE,
181 CSR_DEFAULT_CURSOR_SIZE};
182 GRAPHICS_BUFFER_INFO GraphicsInfo;
183 GraphicsInfo.Info = CreateScreenBufferRequest->GraphicsBufferInfo; // HACK for MSVC
184
185 DPRINT("SrvCreateConsoleScreenBuffer\n");
186
187 Status = ConSrvGetConsole(ProcessData, &Console, TRUE);
188 if (!NT_SUCCESS(Status)) return Status;
189
190 if (CreateScreenBufferRequest->ScreenBufferType == CONSOLE_TEXTMODE_BUFFER)
191 {
192 ScreenBufferInfo = &TextModeInfo;
193
194 /*
195 if (Console->ActiveBuffer)
196 {
197 TextModeInfo.ScreenBufferSize = Console->ActiveBuffer->ScreenBufferSize;
198 if (TextModeInfo.ScreenBufferSize.X == 0) TextModeInfo.ScreenBufferSize.X = 80;
199 if (TextModeInfo.ScreenBufferSize.Y == 0) TextModeInfo.ScreenBufferSize.Y = 25;
200
201 TextModeInfo.ScreenAttrib = Console->ActiveBuffer->ScreenBuffer.TextBuffer.ScreenDefaultAttrib;
202 TextModeInfo.PopupAttrib = Console->ActiveBuffer->ScreenBuffer.TextBuffer.PopupDefaultAttrib;
203
204 TextModeInfo.IsCursorVisible = Console->ActiveBuffer->CursorInfo.bVisible;
205 TextModeInfo.CursorSize = Console->ActiveBuffer->CursorInfo.dwSize;
206 }
207 */
208
209 /*
210 * This is Windows' behaviour
211 */
212
213 /* Use the current console size. Regularize it if needed. */
214 TextModeInfo.ScreenBufferSize = Console->ConsoleSize;
215 if (TextModeInfo.ScreenBufferSize.X == 0) TextModeInfo.ScreenBufferSize.X = 1;
216 if (TextModeInfo.ScreenBufferSize.Y == 0) TextModeInfo.ScreenBufferSize.Y = 1;
217
218 /* If we have an active screen buffer, use its attributes as the new ones */
219 if (Console->ActiveBuffer && GetType(Console->ActiveBuffer) == TEXTMODE_BUFFER)
220 {
221 PTEXTMODE_SCREEN_BUFFER Buffer = (PTEXTMODE_SCREEN_BUFFER)Console->ActiveBuffer;
222
223 TextModeInfo.ScreenAttrib = Buffer->ScreenDefaultAttrib;
224 TextModeInfo.PopupAttrib = Buffer->PopupDefaultAttrib;
225
226 TextModeInfo.IsCursorVisible = Buffer->CursorInfo.bVisible;
227 TextModeInfo.CursorSize = Buffer->CursorInfo.dwSize;
228 }
229 }
230 else if (CreateScreenBufferRequest->ScreenBufferType == CONSOLE_GRAPHICS_BUFFER)
231 {
232 /* Get infos from the graphics buffer information structure */
233 if (!CsrValidateMessageBuffer(ApiMessage,
234 (PVOID*)&CreateScreenBufferRequest->GraphicsBufferInfo.lpBitMapInfo,
235 CreateScreenBufferRequest->GraphicsBufferInfo.dwBitMapInfoLength,
236 sizeof(BYTE)))
237 {
238 Status = STATUS_INVALID_PARAMETER;
239 goto Quit;
240 }
241
242 ScreenBufferInfo = &GraphicsInfo;
243
244 /* Initialize shared variables */
245 // CreateScreenBufferRequest->GraphicsBufferInfo.hMutex
246 CreateScreenBufferRequest->hMutex = GraphicsInfo.Info.hMutex = INVALID_HANDLE_VALUE;
247 // CreateScreenBufferRequest->GraphicsBufferInfo.lpBitMap
248 CreateScreenBufferRequest->lpBitMap = GraphicsInfo.Info.lpBitMap = NULL;
249
250 /* A graphics screen buffer is never inheritable */
251 CreateScreenBufferRequest->InheritHandle = FALSE;
252 }
253
254 Status = ConDrvCreateScreenBuffer(&Buff,
255 Console,
256 CreateScreenBufferRequest->ScreenBufferType,
257 ScreenBufferInfo);
258 if (!NT_SUCCESS(Status)) goto Quit;
259
260 /* Insert the new handle inside the process handles table */
261 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
262
263 Status = ConSrvInsertObject(ProcessData,
264 &CreateScreenBufferRequest->OutputHandle,
265 &Buff->Header,
266 CreateScreenBufferRequest->DesiredAccess,
267 CreateScreenBufferRequest->InheritHandle,
268 CreateScreenBufferRequest->ShareMode);
269
270 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
271
272 if (!NT_SUCCESS(Status)) goto Quit;
273
274 if (CreateScreenBufferRequest->ScreenBufferType == CONSOLE_GRAPHICS_BUFFER)
275 {
276 PGRAPHICS_SCREEN_BUFFER Buffer = (PGRAPHICS_SCREEN_BUFFER)Buff;
277 /*
278 * Initialize the graphics buffer information structure
279 * and give it back to the client.
280 */
281 // CreateScreenBufferRequest->GraphicsBufferInfo.hMutex
282 CreateScreenBufferRequest->hMutex = Buffer->ClientMutex;
283 // CreateScreenBufferRequest->GraphicsBufferInfo.lpBitMap
284 CreateScreenBufferRequest->lpBitMap = Buffer->ClientBitMap;
285 }
286
287 Quit:
288 ConSrvReleaseConsole(Console, TRUE);
289 return Status;
290 }
291
292 NTSTATUS NTAPI
293 ConDrvSetConsoleActiveScreenBuffer(IN PCONSOLE Console,
294 IN PCONSOLE_SCREEN_BUFFER Buffer);
295 CSR_API(SrvSetConsoleActiveScreenBuffer)
296 {
297 NTSTATUS Status;
298 PCONSOLE_SETACTIVESCREENBUFFER SetScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetScreenBufferRequest;
299 PCONSOLE_SCREEN_BUFFER Buffer;
300
301 DPRINT("SrvSetConsoleActiveScreenBuffer\n");
302
303 Status = ConSrvGetScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
304 SetScreenBufferRequest->OutputHandle,
305 &Buffer, GENERIC_WRITE, TRUE);
306 if (!NT_SUCCESS(Status)) return Status;
307
308 Status = ConDrvSetConsoleActiveScreenBuffer(Buffer->Header.Console,
309 Buffer);
310
311 ConSrvReleaseScreenBuffer(Buffer, TRUE);
312 return Status;
313 }
314
315
316 /* CSR THREADS FOR WriteConsole ***********************************************/
317
318 static NTSTATUS
319 DoWriteConsole(IN PCSR_API_MESSAGE ApiMessage,
320 IN PCSR_THREAD ClientThread,
321 IN BOOLEAN CreateWaitBlock OPTIONAL);
322
323 // Wait function CSR_WAIT_FUNCTION
324 static BOOLEAN
325 NTAPI
326 WriteConsoleThread(IN PLIST_ENTRY WaitList,
327 IN PCSR_THREAD WaitThread,
328 IN PCSR_API_MESSAGE WaitApiMessage,
329 IN PVOID WaitContext,
330 IN PVOID WaitArgument1,
331 IN PVOID WaitArgument2,
332 IN ULONG WaitFlags)
333 {
334 NTSTATUS Status;
335
336 DPRINT("WriteConsoleThread - WaitContext = 0x%p, WaitArgument1 = 0x%p, WaitArgument2 = 0x%p, WaitFlags = %lu\n", WaitContext, WaitArgument1, WaitArgument2, WaitFlags);
337
338 /*
339 * If we are notified of the process termination via a call
340 * to CsrNotifyWaitBlock triggered by CsrDestroyProcess or
341 * CsrDestroyThread, just return.
342 */
343 if (WaitFlags & CsrProcessTerminating)
344 {
345 Status = STATUS_THREAD_IS_TERMINATING;
346 goto Quit;
347 }
348
349 Status = DoWriteConsole(WaitApiMessage, WaitThread, FALSE);
350
351 Quit:
352 if (Status != STATUS_PENDING)
353 {
354 WaitApiMessage->Status = Status;
355 }
356
357 return (Status == STATUS_PENDING ? FALSE : TRUE);
358 }
359
360 NTSTATUS NTAPI
361 ConDrvWriteConsole(IN PCONSOLE Console,
362 IN PTEXTMODE_SCREEN_BUFFER ScreenBuffer,
363 IN BOOLEAN Unicode,
364 IN PVOID StringBuffer,
365 IN ULONG NumCharsToWrite,
366 OUT PULONG NumCharsWritten OPTIONAL);
367 static NTSTATUS
368 DoWriteConsole(IN PCSR_API_MESSAGE ApiMessage,
369 IN PCSR_THREAD ClientThread,
370 IN BOOLEAN CreateWaitBlock OPTIONAL)
371 {
372 NTSTATUS Status;
373 PCONSOLE_WRITECONSOLE WriteConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleRequest;
374 PTEXTMODE_SCREEN_BUFFER ScreenBuffer;
375
376 PVOID Buffer;
377 ULONG NrCharactersWritten = 0;
378 ULONG CharSize = (WriteConsoleRequest->Unicode ? sizeof(WCHAR) : sizeof(CHAR));
379
380 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(ClientThread->Process),
381 WriteConsoleRequest->OutputHandle,
382 &ScreenBuffer, GENERIC_WRITE, FALSE);
383 if (!NT_SUCCESS(Status)) return Status;
384
385 /*
386 * For optimization purposes, Windows (and hence ReactOS, too, for
387 * compatibility reasons) uses a static buffer if no more than eighty
388 * bytes are written. Otherwise a new buffer is used.
389 * The client-side expects that we know this behaviour.
390 */
391 if (WriteConsoleRequest->UsingStaticBuffer &&
392 WriteConsoleRequest->NumBytes <= sizeof(WriteConsoleRequest->StaticBuffer))
393 {
394 /*
395 * Adjust the internal pointer, because its old value points to
396 * the static buffer in the original ApiMessage structure.
397 */
398 // WriteConsoleRequest->Buffer = WriteConsoleRequest->StaticBuffer;
399 Buffer = WriteConsoleRequest->StaticBuffer;
400 }
401 else
402 {
403 Buffer = WriteConsoleRequest->Buffer;
404 }
405
406 DPRINT("Calling ConDrvWriteConsole\n");
407 Status = ConDrvWriteConsole(ScreenBuffer->Header.Console,
408 ScreenBuffer,
409 WriteConsoleRequest->Unicode,
410 Buffer,
411 WriteConsoleRequest->NumBytes / CharSize, // NrCharactersToWrite
412 &NrCharactersWritten);
413 DPRINT("ConDrvWriteConsole returned (%d ; Status = 0x%08x)\n",
414 NrCharactersWritten, Status);
415
416 if (Status == STATUS_PENDING)
417 {
418 if (CreateWaitBlock)
419 {
420 if (!CsrCreateWait(&ScreenBuffer->Header.Console->WriteWaitQueue,
421 WriteConsoleThread,
422 ClientThread,
423 ApiMessage,
424 NULL))
425 {
426 /* Fail */
427 Status = STATUS_NO_MEMORY;
428 goto Quit;
429 }
430 }
431
432 /* Wait until we un-pause the console */
433 // Status = STATUS_PENDING;
434 }
435 else
436 {
437 /* We read all what we wanted. Set the number of bytes written. */
438 WriteConsoleRequest->NumBytes = NrCharactersWritten * CharSize;
439 }
440
441 Quit:
442 ConSrvReleaseScreenBuffer(ScreenBuffer, FALSE);
443 return Status;
444 }
445
446
447 /* TEXT OUTPUT APIS ***********************************************************/
448
449 NTSTATUS NTAPI
450 ConDrvReadConsoleOutput(IN PCONSOLE Console,
451 IN PTEXTMODE_SCREEN_BUFFER Buffer,
452 IN BOOLEAN Unicode,
453 OUT PCHAR_INFO CharInfo/*Buffer*/,
454 IN PCOORD BufferSize,
455 IN PCOORD BufferCoord,
456 IN OUT PSMALL_RECT ReadRegion);
457 CSR_API(SrvReadConsoleOutput)
458 {
459 NTSTATUS Status;
460 PCONSOLE_READOUTPUT ReadOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ReadOutputRequest;
461 PTEXTMODE_SCREEN_BUFFER Buffer;
462
463 DPRINT("SrvReadConsoleOutput\n");
464
465 if (!CsrValidateMessageBuffer(ApiMessage,
466 (PVOID*)&ReadOutputRequest->CharInfo,
467 ReadOutputRequest->BufferSize.X * ReadOutputRequest->BufferSize.Y,
468 sizeof(CHAR_INFO)))
469 {
470 return STATUS_INVALID_PARAMETER;
471 }
472
473 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
474 ReadOutputRequest->OutputHandle,
475 &Buffer, GENERIC_READ, TRUE);
476 if (!NT_SUCCESS(Status)) return Status;
477
478 Status = ConDrvReadConsoleOutput(Buffer->Header.Console,
479 Buffer,
480 ReadOutputRequest->Unicode,
481 ReadOutputRequest->CharInfo,
482 &ReadOutputRequest->BufferSize,
483 &ReadOutputRequest->BufferCoord,
484 &ReadOutputRequest->ReadRegion);
485
486 ConSrvReleaseScreenBuffer(Buffer, TRUE);
487 return Status;
488 }
489
490 NTSTATUS NTAPI
491 ConDrvWriteConsoleOutput(IN PCONSOLE Console,
492 IN PTEXTMODE_SCREEN_BUFFER Buffer,
493 IN BOOLEAN Unicode,
494 IN PCHAR_INFO CharInfo/*Buffer*/,
495 IN PCOORD BufferSize,
496 IN PCOORD BufferCoord,
497 IN OUT PSMALL_RECT WriteRegion);
498 CSR_API(SrvWriteConsoleOutput)
499 {
500 NTSTATUS Status;
501 PCONSOLE_WRITEOUTPUT WriteOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteOutputRequest;
502 PTEXTMODE_SCREEN_BUFFER Buffer;
503
504 DPRINT("SrvWriteConsoleOutput\n");
505
506 if (!CsrValidateMessageBuffer(ApiMessage,
507 (PVOID*)&WriteOutputRequest->CharInfo,
508 WriteOutputRequest->BufferSize.X * WriteOutputRequest->BufferSize.Y,
509 sizeof(CHAR_INFO)))
510 {
511 return STATUS_INVALID_PARAMETER;
512 }
513
514 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
515 WriteOutputRequest->OutputHandle,
516 &Buffer, GENERIC_WRITE, TRUE);
517 if (!NT_SUCCESS(Status)) return Status;
518
519 Status = ConDrvWriteConsoleOutput(Buffer->Header.Console,
520 Buffer,
521 WriteOutputRequest->Unicode,
522 WriteOutputRequest->CharInfo,
523 &WriteOutputRequest->BufferSize,
524 &WriteOutputRequest->BufferCoord,
525 &WriteOutputRequest->WriteRegion);
526
527 ConSrvReleaseScreenBuffer(Buffer, TRUE);
528 return Status;
529 }
530
531 CSR_API(SrvWriteConsole)
532 {
533 NTSTATUS Status;
534 PCONSOLE_WRITECONSOLE WriteConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleRequest;
535
536 DPRINT("SrvWriteConsole\n");
537
538 /*
539 * For optimization purposes, Windows (and hence ReactOS, too, for
540 * compatibility reasons) uses a static buffer if no more than eighty
541 * bytes are written. Otherwise a new buffer is used.
542 * The client-side expects that we know this behaviour.
543 */
544 if (WriteConsoleRequest->UsingStaticBuffer &&
545 WriteConsoleRequest->NumBytes <= sizeof(WriteConsoleRequest->StaticBuffer))
546 {
547 /*
548 * Adjust the internal pointer, because its old value points to
549 * the static buffer in the original ApiMessage structure.
550 */
551 // WriteConsoleRequest->Buffer = WriteConsoleRequest->StaticBuffer;
552 }
553 else
554 {
555 if (!CsrValidateMessageBuffer(ApiMessage,
556 (PVOID)&WriteConsoleRequest->Buffer,
557 WriteConsoleRequest->NumBytes,
558 sizeof(BYTE)))
559 {
560 return STATUS_INVALID_PARAMETER;
561 }
562 }
563
564 Status = DoWriteConsole(ApiMessage, CsrGetClientThread(), TRUE);
565
566 if (Status == STATUS_PENDING) *ReplyCode = CsrReplyPending;
567
568 return Status;
569 }
570
571 NTSTATUS NTAPI
572 ConDrvReadConsoleOutputString(IN PCONSOLE Console,
573 IN PTEXTMODE_SCREEN_BUFFER Buffer,
574 IN CODE_TYPE CodeType,
575 OUT PVOID StringBuffer,
576 IN ULONG NumCodesToRead,
577 IN PCOORD ReadCoord,
578 // OUT PCOORD EndCoord,
579 OUT PULONG NumCodesRead OPTIONAL);
580 CSR_API(SrvReadConsoleOutputString)
581 {
582 NTSTATUS Status;
583 PCONSOLE_READOUTPUTCODE ReadOutputCodeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ReadOutputCodeRequest;
584 PTEXTMODE_SCREEN_BUFFER Buffer;
585 ULONG CodeSize;
586
587 PVOID pCode;
588
589 DPRINT("SrvReadConsoleOutputString\n");
590
591 switch (ReadOutputCodeRequest->CodeType)
592 {
593 case CODE_ASCII:
594 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
595 break;
596
597 case CODE_UNICODE:
598 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
599 break;
600
601 case CODE_ATTRIBUTE:
602 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
603 break;
604
605 default:
606 return STATUS_INVALID_PARAMETER;
607 }
608
609 /*
610 * For optimization purposes, Windows (and hence ReactOS, too, for
611 * compatibility reasons) uses a static buffer if no more than eighty
612 * bytes are read. Otherwise a new buffer is used.
613 * The client-side expects that we know this behaviour.
614 */
615 if (ReadOutputCodeRequest->NumCodes * CodeSize <= sizeof(ReadOutputCodeRequest->CodeStaticBuffer))
616 {
617 /*
618 * Adjust the internal pointer, because its old value points to
619 * the static buffer in the original ApiMessage structure.
620 */
621 // ReadOutputCodeRequest->pCode = ReadOutputCodeRequest->CodeStaticBuffer;
622 pCode = ReadOutputCodeRequest->CodeStaticBuffer;
623 }
624 else
625 {
626 if (!CsrValidateMessageBuffer(ApiMessage,
627 (PVOID*)&ReadOutputCodeRequest->pCode,
628 ReadOutputCodeRequest->NumCodes,
629 CodeSize))
630 {
631 return STATUS_INVALID_PARAMETER;
632 }
633
634 pCode = ReadOutputCodeRequest->pCode;
635 }
636
637 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
638 ReadOutputCodeRequest->OutputHandle,
639 &Buffer, GENERIC_READ, TRUE);
640 if (!NT_SUCCESS(Status))
641 {
642 ReadOutputCodeRequest->NumCodes = 0;
643 return Status;
644 }
645
646 Status = ConDrvReadConsoleOutputString(Buffer->Header.Console,
647 Buffer,
648 ReadOutputCodeRequest->CodeType,
649 pCode,
650 ReadOutputCodeRequest->NumCodes,
651 &ReadOutputCodeRequest->Coord,
652 // &ReadOutputCodeRequest->EndCoord,
653 &ReadOutputCodeRequest->NumCodes);
654
655 ConSrvReleaseScreenBuffer(Buffer, TRUE);
656 return Status;
657 }
658
659 NTSTATUS NTAPI
660 ConDrvWriteConsoleOutputString(IN PCONSOLE Console,
661 IN PTEXTMODE_SCREEN_BUFFER Buffer,
662 IN CODE_TYPE CodeType,
663 IN PVOID StringBuffer,
664 IN ULONG NumCodesToWrite,
665 IN PCOORD WriteCoord,
666 // OUT PCOORD EndCoord,
667 OUT PULONG NumCodesWritten OPTIONAL);
668 CSR_API(SrvWriteConsoleOutputString)
669 {
670 NTSTATUS Status;
671 PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteOutputCodeRequest;
672 PTEXTMODE_SCREEN_BUFFER Buffer;
673 ULONG CodeSize;
674
675 PVOID pCode;
676
677 DPRINT("SrvWriteConsoleOutputString\n");
678
679 switch (WriteOutputCodeRequest->CodeType)
680 {
681 case CODE_ASCII:
682 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
683 break;
684
685 case CODE_UNICODE:
686 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
687 break;
688
689 case CODE_ATTRIBUTE:
690 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
691 break;
692
693 default:
694 return STATUS_INVALID_PARAMETER;
695 }
696
697 /*
698 * For optimization purposes, Windows (and hence ReactOS, too, for
699 * compatibility reasons) uses a static buffer if no more than eighty
700 * bytes are written. Otherwise a new buffer is used.
701 * The client-side expects that we know this behaviour.
702 */
703 if (WriteOutputCodeRequest->NumCodes * CodeSize <= sizeof(WriteOutputCodeRequest->CodeStaticBuffer))
704 {
705 /*
706 * Adjust the internal pointer, because its old value points to
707 * the static buffer in the original ApiMessage structure.
708 */
709 // WriteOutputCodeRequest->pCode = WriteOutputCodeRequest->CodeStaticBuffer;
710 pCode = WriteOutputCodeRequest->CodeStaticBuffer;
711 }
712 else
713 {
714 if (!CsrValidateMessageBuffer(ApiMessage,
715 (PVOID*)&WriteOutputCodeRequest->pCode,
716 WriteOutputCodeRequest->NumCodes,
717 CodeSize))
718 {
719 return STATUS_INVALID_PARAMETER;
720 }
721
722 pCode = WriteOutputCodeRequest->pCode;
723 }
724
725 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
726 WriteOutputCodeRequest->OutputHandle,
727 &Buffer, GENERIC_WRITE, TRUE);
728 if (!NT_SUCCESS(Status))
729 {
730 WriteOutputCodeRequest->NumCodes = 0;
731 return Status;
732 }
733
734 Status = ConDrvWriteConsoleOutputString(Buffer->Header.Console,
735 Buffer,
736 WriteOutputCodeRequest->CodeType,
737 pCode,
738 WriteOutputCodeRequest->NumCodes,
739 &WriteOutputCodeRequest->Coord,
740 // &WriteOutputCodeRequest->EndCoord,
741 &WriteOutputCodeRequest->NumCodes);
742
743 ConSrvReleaseScreenBuffer(Buffer, TRUE);
744 return Status;
745 }
746
747 NTSTATUS NTAPI
748 ConDrvFillConsoleOutput(IN PCONSOLE Console,
749 IN PTEXTMODE_SCREEN_BUFFER Buffer,
750 IN CODE_TYPE CodeType,
751 IN CODE_ELEMENT Code,
752 IN ULONG NumCodesToWrite,
753 IN PCOORD WriteCoord,
754 OUT PULONG NumCodesWritten OPTIONAL);
755 CSR_API(SrvFillConsoleOutput)
756 {
757 NTSTATUS Status;
758 PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.FillOutputRequest;
759 PTEXTMODE_SCREEN_BUFFER Buffer;
760 CODE_TYPE CodeType = FillOutputRequest->CodeType;
761
762 DPRINT("SrvFillConsoleOutput\n");
763
764 if ( (CodeType != CODE_ASCII ) &&
765 (CodeType != CODE_UNICODE ) &&
766 (CodeType != CODE_ATTRIBUTE) )
767 {
768 return STATUS_INVALID_PARAMETER;
769 }
770
771 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
772 FillOutputRequest->OutputHandle,
773 &Buffer, GENERIC_WRITE, TRUE);
774 if (!NT_SUCCESS(Status))
775 {
776 FillOutputRequest->NumCodes = 0;
777 return Status;
778 }
779
780 Status = ConDrvFillConsoleOutput(Buffer->Header.Console,
781 Buffer,
782 CodeType,
783 FillOutputRequest->Code,
784 FillOutputRequest->NumCodes,
785 &FillOutputRequest->WriteCoord,
786 &FillOutputRequest->NumCodes);
787
788 ConSrvReleaseScreenBuffer(Buffer, TRUE);
789 return Status;
790 }
791
792 NTSTATUS NTAPI
793 ConDrvGetConsoleScreenBufferInfo(IN PCONSOLE Console,
794 IN PTEXTMODE_SCREEN_BUFFER Buffer,
795 OUT PCOORD ScreenBufferSize,
796 OUT PCOORD CursorPosition,
797 OUT PCOORD ViewOrigin,
798 OUT PCOORD ViewSize,
799 OUT PCOORD MaximumViewSize,
800 OUT PWORD Attributes);
801 CSR_API(SrvGetConsoleScreenBufferInfo)
802 {
803 NTSTATUS Status;
804 PCONSOLE_GETSCREENBUFFERINFO ScreenBufferInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ScreenBufferInfoRequest;
805 PTEXTMODE_SCREEN_BUFFER Buffer;
806
807 DPRINT("SrvGetConsoleScreenBufferInfo\n");
808
809 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
810 ScreenBufferInfoRequest->OutputHandle,
811 &Buffer, GENERIC_READ, TRUE);
812 if (!NT_SUCCESS(Status)) return Status;
813
814 Status = ConDrvGetConsoleScreenBufferInfo(Buffer->Header.Console,
815 Buffer,
816 &ScreenBufferInfoRequest->ScreenBufferSize,
817 &ScreenBufferInfoRequest->CursorPosition,
818 &ScreenBufferInfoRequest->ViewOrigin,
819 &ScreenBufferInfoRequest->ViewSize,
820 &ScreenBufferInfoRequest->MaximumViewSize,
821 &ScreenBufferInfoRequest->Attributes);
822
823 ConSrvReleaseScreenBuffer(Buffer, TRUE);
824 return Status;
825 }
826
827 NTSTATUS NTAPI
828 ConDrvSetConsoleTextAttribute(IN PCONSOLE Console,
829 IN PTEXTMODE_SCREEN_BUFFER Buffer,
830 IN WORD Attributes);
831 CSR_API(SrvSetConsoleTextAttribute)
832 {
833 NTSTATUS Status;
834 PCONSOLE_SETTEXTATTRIB SetTextAttribRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetTextAttribRequest;
835 PTEXTMODE_SCREEN_BUFFER Buffer;
836
837 DPRINT("SrvSetConsoleTextAttribute\n");
838
839 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
840 SetTextAttribRequest->OutputHandle,
841 &Buffer, GENERIC_WRITE, TRUE);
842 if (!NT_SUCCESS(Status)) return Status;
843
844 Status = ConDrvSetConsoleTextAttribute(Buffer->Header.Console,
845 Buffer,
846 SetTextAttribRequest->Attributes);
847
848 ConSrvReleaseScreenBuffer(Buffer, TRUE);
849 return Status;
850 }
851
852 NTSTATUS NTAPI
853 ConDrvSetConsoleScreenBufferSize(IN PCONSOLE Console,
854 IN PTEXTMODE_SCREEN_BUFFER Buffer,
855 IN PCOORD Size);
856 CSR_API(SrvSetConsoleScreenBufferSize)
857 {
858 NTSTATUS Status;
859 PCONSOLE_SETSCREENBUFFERSIZE SetScreenBufferSizeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetScreenBufferSizeRequest;
860 PTEXTMODE_SCREEN_BUFFER Buffer;
861
862 DPRINT("SrvSetConsoleScreenBufferSize\n");
863
864 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
865 SetScreenBufferSizeRequest->OutputHandle,
866 &Buffer, GENERIC_WRITE, TRUE);
867 if (!NT_SUCCESS(Status)) return Status;
868
869 Status = ConDrvSetConsoleScreenBufferSize(Buffer->Header.Console,
870 Buffer,
871 &SetScreenBufferSizeRequest->Size);
872
873 ConSrvReleaseScreenBuffer(Buffer, TRUE);
874 return Status;
875 }
876
877 NTSTATUS NTAPI
878 ConDrvScrollConsoleScreenBuffer(IN PCONSOLE Console,
879 IN PTEXTMODE_SCREEN_BUFFER Buffer,
880 IN BOOLEAN Unicode,
881 IN PSMALL_RECT ScrollRectangle,
882 IN BOOLEAN UseClipRectangle,
883 IN PSMALL_RECT ClipRectangle OPTIONAL,
884 IN PCOORD DestinationOrigin,
885 IN CHAR_INFO FillChar);
886 CSR_API(SrvScrollConsoleScreenBuffer)
887 {
888 NTSTATUS Status;
889 PCONSOLE_SCROLLSCREENBUFFER ScrollScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ScrollScreenBufferRequest;
890 PTEXTMODE_SCREEN_BUFFER Buffer;
891
892 DPRINT("SrvScrollConsoleScreenBuffer\n");
893
894 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
895 ScrollScreenBufferRequest->OutputHandle,
896 &Buffer, GENERIC_WRITE, TRUE);
897 if (!NT_SUCCESS(Status)) return Status;
898
899 Status = ConDrvScrollConsoleScreenBuffer(Buffer->Header.Console,
900 Buffer,
901 ScrollScreenBufferRequest->Unicode,
902 &ScrollScreenBufferRequest->ScrollRectangle,
903 ScrollScreenBufferRequest->UseClipRectangle,
904 &ScrollScreenBufferRequest->ClipRectangle,
905 &ScrollScreenBufferRequest->DestinationOrigin,
906 ScrollScreenBufferRequest->Fill);
907
908 ConSrvReleaseScreenBuffer(Buffer, TRUE);
909 return Status;
910 }
911
912 NTSTATUS NTAPI
913 ConDrvSetConsoleWindowInfo(IN PCONSOLE Console,
914 IN PTEXTMODE_SCREEN_BUFFER Buffer,
915 IN BOOLEAN Absolute,
916 IN PSMALL_RECT WindowRect);
917 CSR_API(SrvSetConsoleWindowInfo)
918 {
919 NTSTATUS Status;
920 PCONSOLE_SETWINDOWINFO SetWindowInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetWindowInfoRequest;
921 // PCONSOLE_SCREEN_BUFFER Buffer;
922 PTEXTMODE_SCREEN_BUFFER Buffer;
923
924 DPRINT1("SrvSetConsoleWindowInfo(0x%08x, %d, {L%d, T%d, R%d, B%d}) called\n",
925 SetWindowInfoRequest->OutputHandle, SetWindowInfoRequest->Absolute,
926 SetWindowInfoRequest->WindowRect.Left ,
927 SetWindowInfoRequest->WindowRect.Top ,
928 SetWindowInfoRequest->WindowRect.Right,
929 SetWindowInfoRequest->WindowRect.Bottom);
930
931 // ConSrvGetScreenBuffer
932 Status = ConSrvGetTextModeBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
933 SetWindowInfoRequest->OutputHandle,
934 &Buffer, GENERIC_READ, TRUE);
935 if (!NT_SUCCESS(Status)) return Status;
936
937 Status = ConDrvSetConsoleWindowInfo(Buffer->Header.Console,
938 Buffer,
939 SetWindowInfoRequest->Absolute,
940 &SetWindowInfoRequest->WindowRect);
941
942 ConSrvReleaseScreenBuffer(Buffer, TRUE);
943 return Status;
944 }
945
946 /* EOF */