Sync with trunk r58033.
[reactos.git] / win32ss / user / consrv / conoutput.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: win32ss/user/consrv/conoutput.c
5 * PURPOSE: Console I/O functions
6 * PROGRAMMERS:
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include "consrv.h"
12 #include "conio.h"
13
14 #define NDEBUG
15 #include <debug.h>
16
17
18 /* GLOBALS ********************************************************************/
19
20 #define ConioInitRect(Rect, top, left, bottom, right) \
21 do { \
22 ((Rect)->Top) = top; \
23 ((Rect)->Left) = left; \
24 ((Rect)->Bottom) = bottom; \
25 ((Rect)->Right) = right; \
26 } while(0)
27
28 #define ConioIsRectEmpty(Rect) \
29 (((Rect)->Left > (Rect)->Right) || ((Rect)->Top > (Rect)->Bottom))
30
31 #define ConsoleUnicodeCharToAnsiChar(Console, dChar, sWChar) \
32 WideCharToMultiByte((Console)->OutputCodePage, 0, (sWChar), 1, (dChar), 1, NULL, NULL)
33
34 #define ConsoleAnsiCharToUnicodeChar(Console, dWChar, sChar) \
35 MultiByteToWideChar((Console)->OutputCodePage, 0, (sChar), 1, (dWChar), 1)
36
37
38 /* PRIVATE FUNCTIONS **********************************************************/
39
40 PBYTE FASTCALL
41 ConioCoordToPointer(PCSRSS_SCREEN_BUFFER Buff, ULONG X, ULONG Y)
42 {
43 return &Buff->Buffer[2 * (((Y + Buff->VirtualY) % Buff->MaxY) * Buff->MaxX + X)];
44 }
45
46 static VOID FASTCALL
47 ClearLineBuffer(PCSRSS_SCREEN_BUFFER Buff)
48 {
49 PBYTE Ptr = ConioCoordToPointer(Buff, 0, Buff->CurrentY);
50 UINT Pos;
51
52 for (Pos = 0; Pos < Buff->MaxX; Pos++)
53 {
54 /* Fill the cell */
55 *Ptr++ = ' ';
56 *Ptr++ = Buff->DefaultAttrib;
57 }
58 }
59
60 NTSTATUS FASTCALL
61 CsrInitConsoleScreenBuffer(PCSRSS_CONSOLE Console,
62 PCSRSS_SCREEN_BUFFER Buffer)
63 {
64 DPRINT("CsrInitConsoleScreenBuffer Size X %d Size Y %d\n", Buffer->MaxX, Buffer->MaxY);
65
66 Buffer->Header.Type = CONIO_SCREEN_BUFFER_MAGIC;
67 Buffer->Header.Console = Console;
68 Buffer->Header.HandleCount = 0;
69 Buffer->ShowX = 0;
70 Buffer->ShowY = 0;
71 Buffer->VirtualY = 0;
72 Buffer->Buffer = HeapAlloc(ConSrvHeap, HEAP_ZERO_MEMORY, Buffer->MaxX * Buffer->MaxY * 2);
73 if (NULL == Buffer->Buffer)
74 {
75 return STATUS_INSUFFICIENT_RESOURCES;
76 }
77 ConioInitScreenBuffer(Console, Buffer);
78 /* initialize buffer to be empty with default attributes */
79 for (Buffer->CurrentY = 0 ; Buffer->CurrentY < Buffer->MaxY; Buffer->CurrentY++)
80 {
81 ClearLineBuffer(Buffer);
82 }
83 Buffer->Mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
84 Buffer->CurrentX = 0;
85 Buffer->CurrentY = 0;
86
87 InsertHeadList(&Console->BufferList, &Buffer->ListEntry);
88 return STATUS_SUCCESS;
89 }
90
91 static VOID FASTCALL
92 ConioNextLine(PCSRSS_SCREEN_BUFFER Buff, SMALL_RECT *UpdateRect, UINT *ScrolledLines)
93 {
94 /* If we hit bottom, slide the viewable screen */
95 if (++Buff->CurrentY == Buff->MaxY)
96 {
97 Buff->CurrentY--;
98 if (++Buff->VirtualY == Buff->MaxY)
99 {
100 Buff->VirtualY = 0;
101 }
102 (*ScrolledLines)++;
103 ClearLineBuffer(Buff);
104 if (UpdateRect->Top != 0)
105 {
106 UpdateRect->Top--;
107 }
108 }
109 UpdateRect->Left = 0;
110 UpdateRect->Right = Buff->MaxX - 1;
111 UpdateRect->Bottom = Buff->CurrentY;
112 }
113
114 NTSTATUS FASTCALL
115 ConioWriteConsole(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff,
116 CHAR *Buffer, DWORD Length, BOOL Attrib)
117 {
118 UINT i;
119 PBYTE Ptr;
120 SMALL_RECT UpdateRect;
121 LONG CursorStartX, CursorStartY;
122 UINT ScrolledLines;
123
124 CursorStartX = Buff->CurrentX;
125 CursorStartY = Buff->CurrentY;
126 UpdateRect.Left = Buff->MaxX;
127 UpdateRect.Top = Buff->CurrentY;
128 UpdateRect.Right = -1;
129 UpdateRect.Bottom = Buff->CurrentY;
130 ScrolledLines = 0;
131
132 for (i = 0; i < Length; i++)
133 {
134 if (Buff->Mode & ENABLE_PROCESSED_OUTPUT)
135 {
136 /* --- LF --- */
137 if (Buffer[i] == '\n')
138 {
139 Buff->CurrentX = 0;
140 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
141 continue;
142 }
143 /* --- BS --- */
144 else if (Buffer[i] == '\b')
145 {
146 /* Only handle BS if we're not on the first pos of the first line */
147 if (0 != Buff->CurrentX || 0 != Buff->CurrentY)
148 {
149 if (0 == Buff->CurrentX)
150 {
151 /* slide virtual position up */
152 Buff->CurrentX = Buff->MaxX - 1;
153 Buff->CurrentY--;
154 UpdateRect.Top = min(UpdateRect.Top, (LONG)Buff->CurrentY);
155 }
156 else
157 {
158 Buff->CurrentX--;
159 }
160 Ptr = ConioCoordToPointer(Buff, Buff->CurrentX, Buff->CurrentY);
161 Ptr[0] = ' ';
162 Ptr[1] = Buff->DefaultAttrib;
163 UpdateRect.Left = min(UpdateRect.Left, (LONG) Buff->CurrentX);
164 UpdateRect.Right = max(UpdateRect.Right, (LONG) Buff->CurrentX);
165 }
166 continue;
167 }
168 /* --- CR --- */
169 else if (Buffer[i] == '\r')
170 {
171 Buff->CurrentX = 0;
172 UpdateRect.Left = min(UpdateRect.Left, (LONG) Buff->CurrentX);
173 UpdateRect.Right = max(UpdateRect.Right, (LONG) Buff->CurrentX);
174 continue;
175 }
176 /* --- TAB --- */
177 else if (Buffer[i] == '\t')
178 {
179 UINT EndX;
180
181 UpdateRect.Left = min(UpdateRect.Left, (LONG)Buff->CurrentX);
182 EndX = (Buff->CurrentX + 8) & ~7;
183 if (EndX > Buff->MaxX)
184 {
185 EndX = Buff->MaxX;
186 }
187 Ptr = ConioCoordToPointer(Buff, Buff->CurrentX, Buff->CurrentY);
188 while (Buff->CurrentX < EndX)
189 {
190 *Ptr++ = ' ';
191 *Ptr++ = Buff->DefaultAttrib;
192 Buff->CurrentX++;
193 }
194 UpdateRect.Right = max(UpdateRect.Right, (LONG) Buff->CurrentX - 1);
195 if (Buff->CurrentX == Buff->MaxX)
196 {
197 if (Buff->Mode & ENABLE_WRAP_AT_EOL_OUTPUT)
198 {
199 Buff->CurrentX = 0;
200 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
201 }
202 else
203 {
204 Buff->CurrentX--;
205 }
206 }
207 continue;
208 }
209 }
210 UpdateRect.Left = min(UpdateRect.Left, (LONG)Buff->CurrentX);
211 UpdateRect.Right = max(UpdateRect.Right, (LONG) Buff->CurrentX);
212 Ptr = ConioCoordToPointer(Buff, Buff->CurrentX, Buff->CurrentY);
213 Ptr[0] = Buffer[i];
214 if (Attrib)
215 {
216 Ptr[1] = Buff->DefaultAttrib;
217 }
218 Buff->CurrentX++;
219 if (Buff->CurrentX == Buff->MaxX)
220 {
221 if (Buff->Mode & ENABLE_WRAP_AT_EOL_OUTPUT)
222 {
223 Buff->CurrentX = 0;
224 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
225 }
226 else
227 {
228 Buff->CurrentX = CursorStartX;
229 }
230 }
231 }
232
233 if (!ConioIsRectEmpty(&UpdateRect) && Buff == Console->ActiveBuffer)
234 {
235 ConioWriteStream(Console, &UpdateRect, CursorStartX, CursorStartY, ScrolledLines,
236 Buffer, Length);
237 }
238
239 return STATUS_SUCCESS;
240 }
241
242 __inline BOOLEAN ConioGetIntersection(
243 SMALL_RECT *Intersection,
244 SMALL_RECT *Rect1,
245 SMALL_RECT *Rect2)
246 {
247 if (ConioIsRectEmpty(Rect1) ||
248 (ConioIsRectEmpty(Rect2)) ||
249 (Rect1->Top > Rect2->Bottom) ||
250 (Rect1->Left > Rect2->Right) ||
251 (Rect1->Bottom < Rect2->Top) ||
252 (Rect1->Right < Rect2->Left))
253 {
254 /* The rectangles do not intersect */
255 ConioInitRect(Intersection, 0, -1, 0, -1);
256 return FALSE;
257 }
258
259 ConioInitRect(Intersection,
260 max(Rect1->Top, Rect2->Top),
261 max(Rect1->Left, Rect2->Left),
262 min(Rect1->Bottom, Rect2->Bottom),
263 min(Rect1->Right, Rect2->Right));
264
265 return TRUE;
266 }
267
268 __inline BOOLEAN ConioGetUnion(
269 SMALL_RECT *Union,
270 SMALL_RECT *Rect1,
271 SMALL_RECT *Rect2)
272 {
273 if (ConioIsRectEmpty(Rect1))
274 {
275 if (ConioIsRectEmpty(Rect2))
276 {
277 ConioInitRect(Union, 0, -1, 0, -1);
278 return FALSE;
279 }
280 else
281 {
282 *Union = *Rect2;
283 }
284 }
285 else if (ConioIsRectEmpty(Rect2))
286 {
287 *Union = *Rect1;
288 }
289 else
290 {
291 ConioInitRect(Union,
292 min(Rect1->Top, Rect2->Top),
293 min(Rect1->Left, Rect2->Left),
294 max(Rect1->Bottom, Rect2->Bottom),
295 max(Rect1->Right, Rect2->Right));
296 }
297
298 return TRUE;
299 }
300
301 /*
302 * Move from one rectangle to another. We must be careful about the order that
303 * this is done, to avoid overwriting parts of the source before they are moved.
304 */
305 static VOID FASTCALL
306 ConioMoveRegion(PCSRSS_SCREEN_BUFFER ScreenBuffer,
307 SMALL_RECT *SrcRegion,
308 SMALL_RECT *DstRegion,
309 SMALL_RECT *ClipRegion,
310 WORD Fill)
311 {
312 int Width = ConioRectWidth(SrcRegion);
313 int Height = ConioRectHeight(SrcRegion);
314 int SX, SY;
315 int DX, DY;
316 int XDelta, YDelta;
317 int i, j;
318
319 SY = SrcRegion->Top;
320 DY = DstRegion->Top;
321 YDelta = 1;
322 if (SY < DY)
323 {
324 /* Moving down: work from bottom up */
325 SY = SrcRegion->Bottom;
326 DY = DstRegion->Bottom;
327 YDelta = -1;
328 }
329 for (i = 0; i < Height; i++)
330 {
331 PWORD SRow = (PWORD)ConioCoordToPointer(ScreenBuffer, 0, SY);
332 PWORD DRow = (PWORD)ConioCoordToPointer(ScreenBuffer, 0, DY);
333
334 SX = SrcRegion->Left;
335 DX = DstRegion->Left;
336 XDelta = 1;
337 if (SX < DX)
338 {
339 /* Moving right: work from right to left */
340 SX = SrcRegion->Right;
341 DX = DstRegion->Right;
342 XDelta = -1;
343 }
344 for (j = 0; j < Width; j++)
345 {
346 WORD Cell = SRow[SX];
347 if (SX >= ClipRegion->Left && SX <= ClipRegion->Right
348 && SY >= ClipRegion->Top && SY <= ClipRegion->Bottom)
349 {
350 SRow[SX] = Fill;
351 }
352 if (DX >= ClipRegion->Left && DX <= ClipRegion->Right
353 && DY >= ClipRegion->Top && DY <= ClipRegion->Bottom)
354 {
355 DRow[DX] = Cell;
356 }
357 SX += XDelta;
358 DX += XDelta;
359 }
360 SY += YDelta;
361 DY += YDelta;
362 }
363 }
364
365 VOID WINAPI
366 ConioDeleteScreenBuffer(PCSRSS_SCREEN_BUFFER Buffer)
367 {
368 PCSRSS_CONSOLE Console = Buffer->Header.Console;
369
370 RemoveEntryList(&Buffer->ListEntry);
371 if (Buffer == Console->ActiveBuffer)
372 {
373 /* Deleted active buffer; switch to most recently created */
374 Console->ActiveBuffer = NULL;
375 if (!IsListEmpty(&Console->BufferList))
376 {
377 Console->ActiveBuffer = CONTAINING_RECORD(Console->BufferList.Flink, CSRSS_SCREEN_BUFFER, ListEntry);
378 ConioDrawConsole(Console);
379 }
380 }
381
382 HeapFree(ConSrvHeap, 0, Buffer->Buffer);
383 HeapFree(ConSrvHeap, 0, Buffer);
384 }
385
386 VOID FASTCALL
387 ConioDrawConsole(PCSRSS_CONSOLE Console)
388 {
389 SMALL_RECT Region;
390
391 ConioInitRect(&Region, 0, 0, Console->Size.Y - 1, Console->Size.X - 1);
392
393 ConioDrawRegion(Console, &Region);
394 }
395
396 static VOID FASTCALL
397 ConioComputeUpdateRect(PCSRSS_SCREEN_BUFFER Buff, SMALL_RECT *UpdateRect, COORD *Start, UINT Length)
398 {
399 if (Buff->MaxX <= Start->X + Length)
400 {
401 UpdateRect->Left = 0;
402 }
403 else
404 {
405 UpdateRect->Left = Start->X;
406 }
407 if (Buff->MaxX <= Start->X + Length)
408 {
409 UpdateRect->Right = Buff->MaxX - 1;
410 }
411 else
412 {
413 UpdateRect->Right = Start->X + Length - 1;
414 }
415 UpdateRect->Top = Start->Y;
416 UpdateRect->Bottom = Start->Y+ (Start->X + Length - 1) / Buff->MaxX;
417 if (Buff->MaxY <= UpdateRect->Bottom)
418 {
419 UpdateRect->Bottom = Buff->MaxY - 1;
420 }
421 }
422
423 DWORD FASTCALL
424 ConioEffectiveCursorSize(PCSRSS_CONSOLE Console, DWORD Scale)
425 {
426 DWORD Size = (Console->ActiveBuffer->CursorInfo.dwSize * Scale + 99) / 100;
427 /* If line input in progress, perhaps adjust for insert toggle */
428 if (Console->LineBuffer && !Console->LineComplete && Console->LineInsertToggle)
429 return (Size * 2 <= Scale) ? (Size * 2) : (Size / 2);
430 return Size;
431 }
432
433 static NTSTATUS
434 DoWriteConsole(IN PCSR_API_MESSAGE ApiMessage,
435 IN PCSR_THREAD ClientThread,
436 IN BOOL CreateWaitBlock OPTIONAL);
437
438 // Wait function CSR_WAIT_FUNCTION
439 static BOOLEAN
440 WriteConsoleThread(IN PLIST_ENTRY WaitList,
441 IN PCSR_THREAD WaitThread,
442 IN PCSR_API_MESSAGE WaitApiMessage,
443 IN PVOID WaitContext,
444 IN PVOID WaitArgument1,
445 IN PVOID WaitArgument2,
446 IN ULONG WaitFlags)
447 {
448 NTSTATUS Status;
449
450 Status = DoWriteConsole(WaitApiMessage,
451 WaitThread,
452 FALSE);
453
454 if (Status != STATUS_PENDING)
455 {
456 WaitApiMessage->Status = Status;
457 }
458
459 return (Status == STATUS_PENDING ? FALSE : TRUE);
460 }
461
462 static NTSTATUS
463 DoWriteConsole(IN PCSR_API_MESSAGE ApiMessage,
464 IN PCSR_THREAD ClientThread,
465 IN BOOL CreateWaitBlock OPTIONAL)
466 {
467 NTSTATUS Status = STATUS_SUCCESS;
468 PCSRSS_WRITE_CONSOLE WriteConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleRequest;
469 PCSRSS_CONSOLE Console;
470 PCSRSS_SCREEN_BUFFER Buff;
471 PCHAR Buffer;
472 DWORD Written = 0;
473 ULONG Length;
474
475 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(ClientThread->Process), WriteConsoleRequest->ConsoleHandle, &Buff, GENERIC_WRITE);
476 if (!NT_SUCCESS(Status)) return Status;
477
478 Console = Buff->Header.Console;
479
480 // if (Console->PauseFlags & (PAUSED_FROM_KEYBOARD | PAUSED_FROM_SCROLLBAR | PAUSED_FROM_SELECTION))
481 if (Console->PauseFlags && Console->UnpauseEvent != NULL)
482 {
483 if (CreateWaitBlock)
484 {
485 if (!CsrCreateWait(&Console->WriteWaitQueue,
486 WriteConsoleThread,
487 ClientThread,
488 ApiMessage,
489 NULL,
490 NULL))
491 {
492 ConioUnlockScreenBuffer(Buff);
493 return STATUS_NO_MEMORY;
494 }
495 }
496
497 /* Wait until we un-pause the console */
498 ConioUnlockScreenBuffer(Buff);
499 return STATUS_PENDING;
500 }
501
502 if(WriteConsoleRequest->Unicode)
503 {
504 Length = WideCharToMultiByte(Console->OutputCodePage, 0,
505 (PWCHAR)WriteConsoleRequest->Buffer,
506 WriteConsoleRequest->NrCharactersToWrite,
507 NULL, 0, NULL, NULL);
508 Buffer = RtlAllocateHeap(GetProcessHeap(), 0, Length);
509 if (Buffer)
510 {
511 WideCharToMultiByte(Console->OutputCodePage, 0,
512 (PWCHAR)WriteConsoleRequest->Buffer,
513 WriteConsoleRequest->NrCharactersToWrite,
514 Buffer, Length, NULL, NULL);
515 }
516 else
517 {
518 Status = STATUS_NO_MEMORY;
519 }
520 }
521 else
522 {
523 Buffer = (PCHAR)WriteConsoleRequest->Buffer;
524 }
525
526 if (Buffer)
527 {
528 if (NT_SUCCESS(Status))
529 {
530 Status = ConioWriteConsole(Console, Buff, Buffer,
531 WriteConsoleRequest->NrCharactersToWrite, TRUE);
532 if (NT_SUCCESS(Status))
533 {
534 Written = WriteConsoleRequest->NrCharactersToWrite;
535 }
536 }
537 if (WriteConsoleRequest->Unicode)
538 {
539 RtlFreeHeap(GetProcessHeap(), 0, Buffer);
540 }
541 }
542
543 WriteConsoleRequest->NrCharactersWritten = Written;
544
545 ConioUnlockScreenBuffer(Buff);
546 return Status;
547 }
548
549
550 /* PUBLIC APIS ****************************************************************/
551
552 CSR_API(SrvReadConsoleOutput)
553 {
554 PCSRSS_READ_CONSOLE_OUTPUT ReadConsoleOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ReadConsoleOutputRequest;
555 PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
556 PCHAR_INFO CharInfo;
557 PCHAR_INFO CurCharInfo;
558 PCSRSS_SCREEN_BUFFER Buff;
559 DWORD SizeX, SizeY;
560 NTSTATUS Status;
561 COORD BufferSize;
562 COORD BufferCoord;
563 SMALL_RECT ReadRegion;
564 SMALL_RECT ScreenRect;
565 DWORD i;
566 PBYTE Ptr;
567 LONG X, Y;
568 UINT CodePage;
569
570 DPRINT("SrvReadConsoleOutput\n");
571
572 CharInfo = ReadConsoleOutputRequest->CharInfo;
573 ReadRegion = ReadConsoleOutputRequest->ReadRegion;
574 BufferSize = ReadConsoleOutputRequest->BufferSize;
575 BufferCoord = ReadConsoleOutputRequest->BufferCoord;
576
577 if (!CsrValidateMessageBuffer(ApiMessage,
578 (PVOID*)&ReadConsoleOutputRequest->CharInfo,
579 BufferSize.X * BufferSize.Y,
580 sizeof(CHAR_INFO)))
581 {
582 return STATUS_INVALID_PARAMETER;
583 }
584
585 Status = ConioLockScreenBuffer(ProcessData, ReadConsoleOutputRequest->ConsoleHandle, &Buff, GENERIC_READ);
586 if (!NT_SUCCESS(Status)) return Status;
587
588 /* FIXME: Is this correct? */
589 CodePage = ProcessData->Console->OutputCodePage;
590
591 SizeY = min(BufferSize.Y - BufferCoord.Y, ConioRectHeight(&ReadRegion));
592 SizeX = min(BufferSize.X - BufferCoord.X, ConioRectWidth(&ReadRegion));
593 ReadRegion.Bottom = ReadRegion.Top + SizeY;
594 ReadRegion.Right = ReadRegion.Left + SizeX;
595
596 ConioInitRect(&ScreenRect, 0, 0, Buff->MaxY, Buff->MaxX);
597 if (!ConioGetIntersection(&ReadRegion, &ScreenRect, &ReadRegion))
598 {
599 ConioUnlockScreenBuffer(Buff);
600 return STATUS_SUCCESS;
601 }
602
603 for (i = 0, Y = ReadRegion.Top; Y < ReadRegion.Bottom; ++i, ++Y)
604 {
605 CurCharInfo = CharInfo + (i * BufferSize.X);
606
607 Ptr = ConioCoordToPointer(Buff, ReadRegion.Left, Y);
608 for (X = ReadRegion.Left; X < ReadRegion.Right; ++X)
609 {
610 if (ReadConsoleOutputRequest->Unicode)
611 {
612 // ConsoleAnsiCharToUnicodeChar(ProcessData->Console, (PCHAR)Ptr++, &CurCharInfo->Char.UnicodeChar);
613 MultiByteToWideChar(CodePage, 0,
614 (PCHAR)Ptr++, 1,
615 &CurCharInfo->Char.UnicodeChar, 1);
616 }
617 else
618 {
619 CurCharInfo->Char.AsciiChar = *Ptr++;
620 }
621 CurCharInfo->Attributes = *Ptr++;
622 ++CurCharInfo;
623 }
624 }
625
626 ConioUnlockScreenBuffer(Buff);
627
628 ReadConsoleOutputRequest->ReadRegion.Right = ReadRegion.Left + SizeX - 1;
629 ReadConsoleOutputRequest->ReadRegion.Bottom = ReadRegion.Top + SizeY - 1;
630 ReadConsoleOutputRequest->ReadRegion.Left = ReadRegion.Left;
631 ReadConsoleOutputRequest->ReadRegion.Top = ReadRegion.Top;
632
633 return STATUS_SUCCESS;
634 }
635
636 CSR_API(SrvWriteConsole)
637 {
638 NTSTATUS Status;
639 PCSRSS_WRITE_CONSOLE WriteConsoleRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleRequest;
640
641 DPRINT("SrvWriteConsole\n");
642
643 if (!CsrValidateMessageBuffer(ApiMessage,
644 (PVOID)&WriteConsoleRequest->Buffer,
645 WriteConsoleRequest->BufferSize,
646 sizeof(BYTE)))
647 {
648 return STATUS_INVALID_PARAMETER;
649 }
650
651 Status = DoWriteConsole(ApiMessage,
652 CsrGetClientThread(),
653 TRUE);
654
655 if (Status == STATUS_PENDING)
656 *ReplyCode = CsrReplyPending;
657
658 return Status;
659 }
660
661 CSR_API(SrvWriteConsoleOutput)
662 {
663 PCSRSS_WRITE_CONSOLE_OUTPUT WriteConsoleOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleOutputRequest;
664 PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
665 SHORT i, X, Y, SizeX, SizeY;
666 PCSRSS_CONSOLE Console;
667 PCSRSS_SCREEN_BUFFER Buff;
668 SMALL_RECT ScreenBuffer;
669 CHAR_INFO* CurCharInfo;
670 SMALL_RECT WriteRegion;
671 CHAR_INFO* CharInfo;
672 COORD BufferCoord;
673 COORD BufferSize;
674 NTSTATUS Status;
675 PBYTE Ptr;
676
677 DPRINT("SrvWriteConsoleOutput\n");
678
679 BufferSize = WriteConsoleOutputRequest->BufferSize;
680 BufferCoord = WriteConsoleOutputRequest->BufferCoord;
681 CharInfo = WriteConsoleOutputRequest->CharInfo;
682
683 if (!CsrValidateMessageBuffer(ApiMessage,
684 (PVOID*)&WriteConsoleOutputRequest->CharInfo,
685 BufferSize.X * BufferSize.Y,
686 sizeof(CHAR_INFO)))
687 {
688 return STATUS_INVALID_PARAMETER;
689 }
690
691 Status = ConioLockScreenBuffer(ProcessData,
692 WriteConsoleOutputRequest->ConsoleHandle,
693 &Buff,
694 GENERIC_WRITE);
695 if (!NT_SUCCESS(Status)) return Status;
696
697 Console = Buff->Header.Console;
698
699 WriteRegion = WriteConsoleOutputRequest->WriteRegion;
700
701 SizeY = min(BufferSize.Y - BufferCoord.Y, ConioRectHeight(&WriteRegion));
702 SizeX = min(BufferSize.X - BufferCoord.X, ConioRectWidth(&WriteRegion));
703 WriteRegion.Bottom = WriteRegion.Top + SizeY - 1;
704 WriteRegion.Right = WriteRegion.Left + SizeX - 1;
705
706 /* Make sure WriteRegion is inside the screen buffer */
707 ConioInitRect(&ScreenBuffer, 0, 0, Buff->MaxY - 1, Buff->MaxX - 1);
708 if (!ConioGetIntersection(&WriteRegion, &ScreenBuffer, &WriteRegion))
709 {
710 ConioUnlockScreenBuffer(Buff);
711
712 /* It is okay to have a WriteRegion completely outside the screen buffer.
713 No data is written then. */
714 return STATUS_SUCCESS;
715 }
716
717 for (i = 0, Y = WriteRegion.Top; Y <= WriteRegion.Bottom; i++, Y++)
718 {
719 CurCharInfo = CharInfo + (i + BufferCoord.Y) * BufferSize.X + BufferCoord.X;
720 Ptr = ConioCoordToPointer(Buff, WriteRegion.Left, Y);
721 for (X = WriteRegion.Left; X <= WriteRegion.Right; X++)
722 {
723 CHAR AsciiChar;
724 if (WriteConsoleOutputRequest->Unicode)
725 {
726 ConsoleUnicodeCharToAnsiChar(Console, &AsciiChar, &CurCharInfo->Char.UnicodeChar);
727 }
728 else
729 {
730 AsciiChar = CurCharInfo->Char.AsciiChar;
731 }
732 *Ptr++ = AsciiChar;
733 *Ptr++ = CurCharInfo->Attributes;
734 CurCharInfo++;
735 }
736 }
737
738 ConioDrawRegion(Console, &WriteRegion);
739
740 ConioUnlockScreenBuffer(Buff);
741
742 WriteConsoleOutputRequest->WriteRegion.Right = WriteRegion.Left + SizeX - 1;
743 WriteConsoleOutputRequest->WriteRegion.Bottom = WriteRegion.Top + SizeY - 1;
744 WriteConsoleOutputRequest->WriteRegion.Left = WriteRegion.Left;
745 WriteConsoleOutputRequest->WriteRegion.Top = WriteRegion.Top;
746
747 return STATUS_SUCCESS;
748 }
749
750 CSR_API(SrvReadConsoleOutputString)
751 {
752 NTSTATUS Status;
753 PCSRSS_READ_CONSOLE_OUTPUT_CODE ReadConsoleOutputCodeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ReadConsoleOutputCodeRequest;
754 PCSRSS_CONSOLE Console;
755 PCSRSS_SCREEN_BUFFER Buff;
756 USHORT CodeType;
757 DWORD Xpos, Ypos;
758 PVOID ReadBuffer;
759 DWORD i;
760 ULONG CodeSize;
761 BYTE Code;
762
763 DPRINT("SrvReadConsoleOutputString\n");
764
765 CodeType = ReadConsoleOutputCodeRequest->CodeType;
766 switch (CodeType)
767 {
768 case CODE_ASCII:
769 CodeSize = sizeof(CHAR);
770 break;
771
772 case CODE_UNICODE:
773 CodeSize = sizeof(WCHAR);
774 break;
775
776 case CODE_ATTRIBUTE:
777 CodeSize = sizeof(WORD);
778 break;
779
780 default:
781 return STATUS_INVALID_PARAMETER;
782 }
783
784 if (!CsrValidateMessageBuffer(ApiMessage,
785 (PVOID*)&ReadConsoleOutputCodeRequest->pCode.pCode,
786 ReadConsoleOutputCodeRequest->NumCodesToRead,
787 CodeSize))
788 {
789 return STATUS_INVALID_PARAMETER;
790 }
791
792 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), ReadConsoleOutputCodeRequest->ConsoleHandle, &Buff, GENERIC_READ);
793 if (!NT_SUCCESS(Status)) return Status;
794
795 Console = Buff->Header.Console;
796
797 ReadBuffer = ReadConsoleOutputCodeRequest->pCode.pCode;
798 Xpos = ReadConsoleOutputCodeRequest->ReadCoord.X;
799 Ypos = (ReadConsoleOutputCodeRequest->ReadCoord.Y + Buff->VirtualY) % Buff->MaxY;
800
801 /*
802 * MSDN (ReadConsoleOutputAttribute and ReadConsoleOutputCharacter) :
803 *
804 * If the number of attributes (resp. characters) to be read from extends
805 * beyond the end of the specified screen buffer row, attributes (resp.
806 * characters) are read from the next row. If the number of attributes
807 * (resp. characters) to be read from extends beyond the end of the console
808 * screen buffer, attributes (resp. characters) up to the end of the console
809 * screen buffer are read.
810 *
811 * TODO: Do NOT loop up to NumCodesToRead, but stop before
812 * if we are going to overflow...
813 */
814 for (i = 0; i < ReadConsoleOutputCodeRequest->NumCodesToRead; ++i)
815 {
816 Code = Buff->Buffer[2 * (Xpos + Ypos * Buff->MaxX) + (CodeType == CODE_ATTRIBUTE ? 1 : 0)];
817
818 switch (CodeType)
819 {
820 case CODE_UNICODE:
821 ConsoleAnsiCharToUnicodeChar(Console, (PWCHAR)ReadBuffer, (PCHAR)&Code);
822 break;
823
824 case CODE_ASCII:
825 *(PCHAR)ReadBuffer = (CHAR)Code;
826 break;
827
828 case CODE_ATTRIBUTE:
829 *(PWORD)ReadBuffer = (WORD)Code;
830 break;
831 }
832 ReadBuffer = (PVOID)((ULONG_PTR)ReadBuffer + CodeSize);
833
834 Xpos++;
835
836 if (Xpos == Buff->MaxX)
837 {
838 Xpos = 0;
839 Ypos++;
840
841 if (Ypos == Buff->MaxY)
842 {
843 Ypos = 0;
844 }
845 }
846 }
847
848 switch (CodeType)
849 {
850 case CODE_UNICODE:
851 *(PWCHAR)ReadBuffer = 0;
852 break;
853
854 case CODE_ASCII:
855 *(PCHAR)ReadBuffer = 0;
856 break;
857
858 case CODE_ATTRIBUTE:
859 *(PWORD)ReadBuffer = 0;
860 break;
861 }
862
863 ReadConsoleOutputCodeRequest->EndCoord.X = Xpos;
864 ReadConsoleOutputCodeRequest->EndCoord.Y = (Ypos - Buff->VirtualY + Buff->MaxY) % Buff->MaxY;
865
866 ConioUnlockScreenBuffer(Buff);
867
868 ReadConsoleOutputCodeRequest->CodesRead = (DWORD)((ULONG_PTR)ReadBuffer - (ULONG_PTR)ReadConsoleOutputCodeRequest->pCode.pCode) / CodeSize;
869 // <= ReadConsoleOutputCodeRequest->NumCodesToRead
870
871 return STATUS_SUCCESS;
872 }
873
874 CSR_API(SrvWriteConsoleOutputString)
875 {
876 NTSTATUS Status;
877 PCSRSS_WRITE_CONSOLE_OUTPUT_CODE WriteConsoleOutputCodeRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.WriteConsoleOutputCodeRequest;
878 PCSRSS_CONSOLE Console;
879 PCSRSS_SCREEN_BUFFER Buff;
880 USHORT CodeType;
881 PBYTE Buffer; // PUCHAR
882 PCHAR String, tmpString = NULL;
883 DWORD X, Y, Length; // , Written = 0;
884 ULONG CodeSize;
885 SMALL_RECT UpdateRect;
886
887 DPRINT("SrvWriteConsoleOutputString\n");
888
889 CodeType = WriteConsoleOutputCodeRequest->CodeType;
890 switch (CodeType)
891 {
892 case CODE_ASCII:
893 CodeSize = sizeof(CHAR);
894 break;
895
896 case CODE_UNICODE:
897 CodeSize = sizeof(WCHAR);
898 break;
899
900 case CODE_ATTRIBUTE:
901 CodeSize = sizeof(WORD);
902 break;
903
904 default:
905 return STATUS_INVALID_PARAMETER;
906 }
907
908 if (!CsrValidateMessageBuffer(ApiMessage,
909 (PVOID*)&WriteConsoleOutputCodeRequest->pCode.pCode,
910 WriteConsoleOutputCodeRequest->Length,
911 CodeSize))
912 {
913 return STATUS_INVALID_PARAMETER;
914 }
915
916 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process),
917 WriteConsoleOutputCodeRequest->ConsoleHandle,
918 &Buff,
919 GENERIC_WRITE);
920 if (!NT_SUCCESS(Status)) return Status;
921
922 Console = Buff->Header.Console;
923
924 switch (CodeType)
925 {
926 case CODE_UNICODE:
927 {
928 Length = WideCharToMultiByte(Console->OutputCodePage, 0,
929 (PWCHAR)WriteConsoleOutputCodeRequest->pCode.UnicodeChar,
930 WriteConsoleOutputCodeRequest->Length,
931 NULL, 0, NULL, NULL);
932 tmpString = String = RtlAllocateHeap(GetProcessHeap(), 0, Length);
933 if (String)
934 {
935 WideCharToMultiByte(Console->OutputCodePage, 0,
936 (PWCHAR)WriteConsoleOutputCodeRequest->pCode.UnicodeChar,
937 WriteConsoleOutputCodeRequest->Length,
938 String, Length, NULL, NULL);
939 }
940 else
941 {
942 Status = STATUS_NO_MEMORY;
943 }
944
945 break;
946 }
947
948 case CODE_ASCII:
949 String = (PCHAR)WriteConsoleOutputCodeRequest->pCode.AsciiChar;
950 break;
951
952 case CODE_ATTRIBUTE:
953 default:
954 // *(ReadBuffer++) = Code;
955 String = (PCHAR)WriteConsoleOutputCodeRequest->pCode.Attribute;
956 break;
957 }
958
959 if (String && NT_SUCCESS(Status))
960 {
961 X = WriteConsoleOutputCodeRequest->Coord.X;
962 Y = (WriteConsoleOutputCodeRequest->Coord.Y + Buff->VirtualY) % Buff->MaxY;
963 Length = WriteConsoleOutputCodeRequest->Length;
964 Buffer = &Buff->Buffer[2 * (Y * Buff->MaxX + X) + (CodeType == CODE_ATTRIBUTE ? 1 : 0)];
965
966 while (Length--)
967 {
968 *Buffer = *String++;
969 // ReadBuffer = (PVOID)((ULONG_PTR)ReadBuffer + CodeSize);
970 String = (PCHAR)((ULONG_PTR)String + CodeSize);
971 // Written++;
972 Buffer += 2;
973 if (++X == Buff->MaxX)
974 {
975 if (++Y == Buff->MaxY)
976 {
977 Y = 0;
978 Buffer = Buff->Buffer + (CodeType == CODE_ATTRIBUTE ? 1 : 0);
979 }
980 X = 0;
981 }
982 }
983
984 if (Buff == Console->ActiveBuffer)
985 {
986 ConioComputeUpdateRect(Buff, &UpdateRect, &WriteConsoleOutputCodeRequest->Coord,
987 WriteConsoleOutputCodeRequest->Length);
988 ConioDrawRegion(Console, &UpdateRect);
989 }
990
991 WriteConsoleOutputCodeRequest->EndCoord.X = X;
992 WriteConsoleOutputCodeRequest->EndCoord.Y = (Y + Buff->MaxY - Buff->VirtualY) % Buff->MaxY;
993 }
994
995 if (tmpString)
996 {
997 RtlFreeHeap(GetProcessHeap(), 0, tmpString);
998 }
999
1000 ConioUnlockScreenBuffer(Buff);
1001
1002 // WriteConsoleOutputCodeRequest->NrCharactersWritten = Written;
1003 return Status;
1004 }
1005
1006 CSR_API(SrvFillConsoleOutput)
1007 {
1008 NTSTATUS Status;
1009 PCSRSS_FILL_OUTPUT FillOutputRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.FillOutputRequest;
1010 PCSRSS_CONSOLE Console;
1011 PCSRSS_SCREEN_BUFFER Buff;
1012 DWORD X, Y, Length; // , Written = 0;
1013 USHORT CodeType;
1014 BYTE Code;
1015 PBYTE Buffer;
1016 SMALL_RECT UpdateRect;
1017
1018 DPRINT("SrvFillConsoleOutput\n");
1019
1020 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), FillOutputRequest->ConsoleHandle, &Buff, GENERIC_WRITE);
1021 if (!NT_SUCCESS(Status)) return Status;
1022
1023 Console = Buff->Header.Console;
1024
1025 CodeType = FillOutputRequest->CodeType;
1026
1027 X = FillOutputRequest->Coord.X;
1028 Y = (FillOutputRequest->Coord.Y + Buff->VirtualY) % Buff->MaxY;
1029 Length = FillOutputRequest->Length;
1030 Buffer = &Buff->Buffer[2 * (Y * Buff->MaxX + X) + (CodeType == CODE_ATTRIBUTE ? 1 : 0)];
1031
1032 switch (CodeType)
1033 {
1034 case CODE_ASCII:
1035 Code = (BYTE)FillOutputRequest->Code.AsciiChar;
1036 break;
1037
1038 case CODE_UNICODE:
1039 ConsoleUnicodeCharToAnsiChar(Console, (PCHAR)&Code, &FillOutputRequest->Code.UnicodeChar);
1040 break;
1041
1042 case CODE_ATTRIBUTE:
1043 Code = (BYTE)FillOutputRequest->Code.Attribute;
1044 break;
1045
1046 default:
1047 ConioUnlockScreenBuffer(Buff);
1048 return STATUS_INVALID_PARAMETER;
1049 }
1050
1051 while (Length--)
1052 {
1053 *Buffer = Code;
1054 Buffer += 2;
1055 // Written++;
1056 if (++X == Buff->MaxX)
1057 {
1058 if (++Y == Buff->MaxY)
1059 {
1060 Y = 0;
1061 Buffer = Buff->Buffer + (CodeType == CODE_ATTRIBUTE ? 1 : 0);
1062 }
1063 X = 0;
1064 }
1065 }
1066
1067 if (Buff == Console->ActiveBuffer)
1068 {
1069 ConioComputeUpdateRect(Buff, &UpdateRect, &FillOutputRequest->Coord,
1070 FillOutputRequest->Length);
1071 ConioDrawRegion(Console, &UpdateRect);
1072 }
1073
1074 ConioUnlockScreenBuffer(Buff);
1075 /*
1076 Length = FillOutputRequest->Length;
1077 FillOutputRequest->NrCharactersWritten = Length;
1078 */
1079 return STATUS_SUCCESS;
1080 }
1081
1082 CSR_API(SrvGetConsoleCursorInfo)
1083 {
1084 NTSTATUS Status;
1085 PCSRSS_CURSOR_INFO CursorInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CursorInfoRequest;
1086 PCSRSS_SCREEN_BUFFER Buff;
1087
1088 DPRINT("SrvGetConsoleCursorInfo\n");
1089
1090 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), CursorInfoRequest->ConsoleHandle, &Buff, GENERIC_READ);
1091 if (!NT_SUCCESS(Status)) return Status;
1092
1093 CursorInfoRequest->Info.bVisible = Buff->CursorInfo.bVisible;
1094 CursorInfoRequest->Info.dwSize = Buff->CursorInfo.dwSize;
1095 ConioUnlockScreenBuffer(Buff);
1096
1097 return STATUS_SUCCESS;
1098 }
1099
1100 CSR_API(SrvSetConsoleCursorInfo)
1101 {
1102 NTSTATUS Status;
1103 PCSRSS_CURSOR_INFO CursorInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CursorInfoRequest;
1104 PCSRSS_CONSOLE Console;
1105 PCSRSS_SCREEN_BUFFER Buff;
1106 DWORD Size;
1107 BOOL Visible;
1108
1109 DPRINT("SrvSetConsoleCursorInfo\n");
1110
1111 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), CursorInfoRequest->ConsoleHandle, &Buff, GENERIC_WRITE);
1112 if (!NT_SUCCESS(Status)) return Status;
1113
1114 Console = Buff->Header.Console;
1115
1116 Size = CursorInfoRequest->Info.dwSize;
1117 Visible = CursorInfoRequest->Info.bVisible;
1118 if (Size < 1)
1119 {
1120 Size = 1;
1121 }
1122 if (100 < Size)
1123 {
1124 Size = 100;
1125 }
1126
1127 if ( (Size != Buff->CursorInfo.dwSize) ||
1128 (Visible && ! Buff->CursorInfo.bVisible) ||
1129 (! Visible && Buff->CursorInfo.bVisible) )
1130 {
1131 Buff->CursorInfo.dwSize = Size;
1132 Buff->CursorInfo.bVisible = Visible;
1133
1134 if (!ConioSetCursorInfo(Console, Buff))
1135 {
1136 ConioUnlockScreenBuffer(Buff);
1137 return STATUS_UNSUCCESSFUL;
1138 }
1139 }
1140
1141 ConioUnlockScreenBuffer(Buff);
1142
1143 return STATUS_SUCCESS;
1144 }
1145
1146 CSR_API(SrvSetConsoleCursorPosition)
1147 {
1148 NTSTATUS Status;
1149 PCSRSS_SET_CURSOR_POSITION SetCursorPositionRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetCursorPositionRequest;
1150 PCSRSS_CONSOLE Console;
1151 PCSRSS_SCREEN_BUFFER Buff;
1152 LONG OldCursorX, OldCursorY;
1153 LONG NewCursorX, NewCursorY;
1154
1155 DPRINT("SrvSetConsoleCursorPosition\n");
1156
1157 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), SetCursorPositionRequest->ConsoleHandle, &Buff, GENERIC_WRITE);
1158 if (!NT_SUCCESS(Status)) return Status;
1159
1160 Console = Buff->Header.Console;
1161
1162 NewCursorX = SetCursorPositionRequest->Position.X;
1163 NewCursorY = SetCursorPositionRequest->Position.Y;
1164 if ( NewCursorX < 0 || NewCursorX >= Buff->MaxX ||
1165 NewCursorY < 0 || NewCursorY >= Buff->MaxY )
1166 {
1167 ConioUnlockScreenBuffer(Buff);
1168 return STATUS_INVALID_PARAMETER;
1169 }
1170 OldCursorX = Buff->CurrentX;
1171 OldCursorY = Buff->CurrentY;
1172 Buff->CurrentX = NewCursorX;
1173 Buff->CurrentY = NewCursorY;
1174 if (Buff == Console->ActiveBuffer)
1175 {
1176 if (! ConioSetScreenInfo(Console, Buff, OldCursorX, OldCursorY))
1177 {
1178 ConioUnlockScreenBuffer(Buff);
1179 return STATUS_UNSUCCESSFUL;
1180 }
1181 }
1182
1183 ConioUnlockScreenBuffer(Buff);
1184
1185 return STATUS_SUCCESS;
1186 }
1187
1188 CSR_API(SrvSetConsoleTextAttribute)
1189 {
1190 NTSTATUS Status;
1191 PCSRSS_SET_ATTRIB SetAttribRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetAttribRequest;
1192 PCSRSS_CONSOLE Console;
1193 PCSRSS_SCREEN_BUFFER Buff;
1194
1195 DPRINT("SrvSetConsoleTextAttribute\n");
1196
1197 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), SetAttribRequest->ConsoleHandle, &Buff, GENERIC_WRITE);
1198 if (!NT_SUCCESS(Status)) return Status;
1199
1200 Console = Buff->Header.Console;
1201
1202 Buff->DefaultAttrib = SetAttribRequest->Attrib;
1203 if (Buff == Console->ActiveBuffer)
1204 {
1205 if (!ConioUpdateScreenInfo(Console, Buff))
1206 {
1207 ConioUnlockScreenBuffer(Buff);
1208 return STATUS_UNSUCCESSFUL;
1209 }
1210 }
1211
1212 ConioUnlockScreenBuffer(Buff);
1213
1214 return STATUS_SUCCESS;
1215 }
1216
1217 CSR_API(SrvCreateConsoleScreenBuffer)
1218 {
1219 NTSTATUS Status;
1220 PCSRSS_CREATE_SCREEN_BUFFER CreateScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.CreateScreenBufferRequest;
1221 PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
1222 PCSRSS_CONSOLE Console;
1223 PCSRSS_SCREEN_BUFFER Buff;
1224
1225 DPRINT("SrvCreateConsoleScreenBuffer\n");
1226
1227 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
1228
1229 Status = ConioConsoleFromProcessData(ProcessData, &Console);
1230 if (!NT_SUCCESS(Status))
1231 {
1232 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
1233 return Status;
1234 }
1235
1236 Buff = HeapAlloc(ConSrvHeap, HEAP_ZERO_MEMORY, sizeof(CSRSS_SCREEN_BUFFER));
1237 if (Buff != NULL)
1238 {
1239 if (Console->ActiveBuffer)
1240 {
1241 Buff->MaxX = Console->ActiveBuffer->MaxX;
1242 Buff->MaxY = Console->ActiveBuffer->MaxY;
1243 Buff->CursorInfo.bVisible = Console->ActiveBuffer->CursorInfo.bVisible;
1244 Buff->CursorInfo.dwSize = Console->ActiveBuffer->CursorInfo.dwSize;
1245 }
1246 else
1247 {
1248 Buff->CursorInfo.bVisible = TRUE;
1249 Buff->CursorInfo.dwSize = CSR_DEFAULT_CURSOR_SIZE;
1250 }
1251
1252 if (Buff->MaxX == 0)
1253 {
1254 Buff->MaxX = 80;
1255 }
1256
1257 if (Buff->MaxY == 0)
1258 {
1259 Buff->MaxY = 25;
1260 }
1261
1262 Status = CsrInitConsoleScreenBuffer(Console, Buff);
1263 if (NT_SUCCESS(Status))
1264 {
1265 Status = Win32CsrInsertObject(ProcessData,
1266 &CreateScreenBufferRequest->OutputHandle,
1267 &Buff->Header,
1268 CreateScreenBufferRequest->Access,
1269 CreateScreenBufferRequest->Inheritable,
1270 CreateScreenBufferRequest->ShareMode);
1271 }
1272 }
1273 else
1274 {
1275 Status = STATUS_INSUFFICIENT_RESOURCES;
1276 }
1277
1278 ConioUnlockConsole(Console);
1279
1280 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
1281
1282 return Status;
1283 }
1284
1285 CSR_API(SrvGetConsoleScreenBufferInfo)
1286 {
1287 NTSTATUS Status;
1288 PCSRSS_SCREEN_BUFFER_INFO ScreenBufferInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ScreenBufferInfoRequest;
1289 PCSRSS_CONSOLE Console;
1290 PCSRSS_SCREEN_BUFFER Buff;
1291 PCONSOLE_SCREEN_BUFFER_INFO pInfo = &ScreenBufferInfoRequest->Info;
1292
1293 DPRINT("SrvGetConsoleScreenBufferInfo\n");
1294
1295 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), ScreenBufferInfoRequest->ConsoleHandle, &Buff, GENERIC_READ);
1296 if (!NT_SUCCESS(Status)) return Status;
1297
1298 Console = Buff->Header.Console;
1299
1300 pInfo->dwSize.X = Buff->MaxX;
1301 pInfo->dwSize.Y = Buff->MaxY;
1302 pInfo->dwCursorPosition.X = Buff->CurrentX;
1303 pInfo->dwCursorPosition.Y = Buff->CurrentY;
1304 pInfo->wAttributes = Buff->DefaultAttrib;
1305 pInfo->srWindow.Left = Buff->ShowX;
1306 pInfo->srWindow.Right = Buff->ShowX + Console->Size.X - 1;
1307 pInfo->srWindow.Top = Buff->ShowY;
1308 pInfo->srWindow.Bottom = Buff->ShowY + Console->Size.Y - 1;
1309 pInfo->dwMaximumWindowSize.X = Buff->MaxX;
1310 pInfo->dwMaximumWindowSize.Y = Buff->MaxY;
1311
1312 ConioUnlockScreenBuffer(Buff);
1313
1314 return STATUS_SUCCESS;
1315 }
1316
1317 CSR_API(SrvSetConsoleActiveScreenBuffer)
1318 {
1319 NTSTATUS Status;
1320 PCSRSS_SET_SCREEN_BUFFER SetScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetScreenBufferRequest;
1321 PCSRSS_CONSOLE Console;
1322 PCSRSS_SCREEN_BUFFER Buff;
1323
1324 DPRINT("SrvSetConsoleActiveScreenBuffer\n");
1325
1326 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), SetScreenBufferRequest->OutputHandle, &Buff, GENERIC_WRITE);
1327 if (!NT_SUCCESS(Status)) return Status;
1328
1329 Console = Buff->Header.Console;
1330
1331 if (Buff == Console->ActiveBuffer)
1332 {
1333 ConioUnlockScreenBuffer(Buff);
1334 return STATUS_SUCCESS;
1335 }
1336
1337 /* If old buffer has no handles, it's now unreferenced */
1338 if (Console->ActiveBuffer->Header.HandleCount == 0)
1339 {
1340 ConioDeleteScreenBuffer(Console->ActiveBuffer);
1341 }
1342
1343 /* Tie console to new buffer */
1344 Console->ActiveBuffer = Buff;
1345
1346 /* Redraw the console */
1347 ConioDrawConsole(Console);
1348
1349 ConioUnlockScreenBuffer(Buff);
1350
1351 return STATUS_SUCCESS;
1352 }
1353
1354 CSR_API(SrvScrollConsoleScreenBuffer)
1355 {
1356 PCSRSS_SCROLL_CONSOLE_SCREEN_BUFFER ScrollConsoleScreenBufferRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ScrollConsoleScreenBufferRequest;
1357 PCSRSS_CONSOLE Console;
1358 PCSRSS_SCREEN_BUFFER Buff;
1359 SMALL_RECT ScreenBuffer;
1360 SMALL_RECT SrcRegion;
1361 SMALL_RECT DstRegion;
1362 SMALL_RECT UpdateRegion;
1363 SMALL_RECT ScrollRectangle;
1364 SMALL_RECT ClipRectangle;
1365 NTSTATUS Status;
1366 HANDLE ConsoleHandle;
1367 BOOLEAN UseClipRectangle;
1368 COORD DestinationOrigin;
1369 CHAR_INFO Fill;
1370 CHAR FillChar;
1371
1372 DPRINT("SrvScrollConsoleScreenBuffer\n");
1373
1374 ConsoleHandle = ScrollConsoleScreenBufferRequest->ConsoleHandle;
1375 UseClipRectangle = ScrollConsoleScreenBufferRequest->UseClipRectangle;
1376 DestinationOrigin = ScrollConsoleScreenBufferRequest->DestinationOrigin;
1377 Fill = ScrollConsoleScreenBufferRequest->Fill;
1378
1379 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), ConsoleHandle, &Buff, GENERIC_WRITE);
1380 if (!NT_SUCCESS(Status)) return Status;
1381
1382 Console = Buff->Header.Console;
1383
1384 ScrollRectangle = ScrollConsoleScreenBufferRequest->ScrollRectangle;
1385
1386 /* Make sure source rectangle is inside the screen buffer */
1387 ConioInitRect(&ScreenBuffer, 0, 0, Buff->MaxY - 1, Buff->MaxX - 1);
1388 if (! ConioGetIntersection(&SrcRegion, &ScreenBuffer, &ScrollRectangle))
1389 {
1390 ConioUnlockScreenBuffer(Buff);
1391 return STATUS_SUCCESS;
1392 }
1393
1394 /* If the source was clipped on the left or top, adjust the destination accordingly */
1395 if (ScrollRectangle.Left < 0)
1396 {
1397 DestinationOrigin.X -= ScrollRectangle.Left;
1398 }
1399 if (ScrollRectangle.Top < 0)
1400 {
1401 DestinationOrigin.Y -= ScrollRectangle.Top;
1402 }
1403
1404 if (UseClipRectangle)
1405 {
1406 ClipRectangle = ScrollConsoleScreenBufferRequest->ClipRectangle;
1407 if (!ConioGetIntersection(&ClipRectangle, &ClipRectangle, &ScreenBuffer))
1408 {
1409 ConioUnlockScreenBuffer(Buff);
1410 return STATUS_SUCCESS;
1411 }
1412 }
1413 else
1414 {
1415 ClipRectangle = ScreenBuffer;
1416 }
1417
1418 ConioInitRect(&DstRegion,
1419 DestinationOrigin.Y,
1420 DestinationOrigin.X,
1421 DestinationOrigin.Y + ConioRectHeight(&SrcRegion) - 1,
1422 DestinationOrigin.X + ConioRectWidth(&SrcRegion) - 1);
1423
1424 if (ScrollConsoleScreenBufferRequest->Unicode)
1425 ConsoleUnicodeCharToAnsiChar(Console, &FillChar, &Fill.Char.UnicodeChar);
1426 else
1427 FillChar = Fill.Char.AsciiChar;
1428
1429 ConioMoveRegion(Buff, &SrcRegion, &DstRegion, &ClipRectangle, Fill.Attributes << 8 | (BYTE)FillChar);
1430
1431 if (Buff == Console->ActiveBuffer)
1432 {
1433 ConioGetUnion(&UpdateRegion, &SrcRegion, &DstRegion);
1434 if (ConioGetIntersection(&UpdateRegion, &UpdateRegion, &ClipRectangle))
1435 {
1436 /* Draw update region */
1437 ConioDrawRegion(Console, &UpdateRegion);
1438 }
1439 }
1440
1441 ConioUnlockScreenBuffer(Buff);
1442
1443 return STATUS_SUCCESS;
1444 }
1445
1446 CSR_API(SrvSetConsoleScreenBufferSize)
1447 {
1448 NTSTATUS Status;
1449 PCSRSS_SET_SCREEN_BUFFER_SIZE SetScreenBufferSize = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetScreenBufferSize;
1450 PCSRSS_SCREEN_BUFFER Buff;
1451
1452 Status = ConioLockScreenBuffer(ConsoleGetPerProcessData(CsrGetClientThread()->Process), SetScreenBufferSize->OutputHandle, &Buff, GENERIC_WRITE);
1453 if (!NT_SUCCESS(Status)) return Status;
1454
1455 Status = ConioResizeBuffer(Buff->Header.Console, Buff, SetScreenBufferSize->Size);
1456 ConioUnlockScreenBuffer(Buff);
1457
1458 return Status;
1459 }
1460
1461 /* EOF */