Simplify code.
[reactos.git] / win32ss / user / winsrv / consrv / condrv / text.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Driver DLL
4 * FILE: win32ss/user/winsrv/consrv/condrv/text.c
5 * PURPOSE: Console Output Functions for text-mode screen-buffers
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 /* GLOBALS ********************************************************************/
18
19 #define TAB_WIDTH 8
20
21 /* PRIVATE FUNCTIONS **********************************************************/
22
23 CONSOLE_IO_OBJECT_TYPE
24 TEXTMODE_BUFFER_GetType(PCONSOLE_SCREEN_BUFFER This)
25 {
26 // return This->Header.Type;
27 return TEXTMODE_BUFFER;
28 }
29
30 static CONSOLE_SCREEN_BUFFER_VTBL TextVtbl =
31 {
32 TEXTMODE_BUFFER_GetType,
33 };
34
35
36 static VOID
37 ClearLineBuffer(PTEXTMODE_SCREEN_BUFFER Buff);
38
39
40 NTSTATUS
41 CONSOLE_SCREEN_BUFFER_Initialize(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
42 IN OUT PCONSOLE Console,
43 IN SIZE_T Size);
44 VOID
45 CONSOLE_SCREEN_BUFFER_Destroy(IN OUT PCONSOLE_SCREEN_BUFFER Buffer);
46
47
48 NTSTATUS
49 TEXTMODE_BUFFER_Initialize(OUT PCONSOLE_SCREEN_BUFFER* Buffer,
50 IN OUT PCONSOLE Console,
51 IN PTEXTMODE_BUFFER_INFO TextModeInfo)
52 {
53 NTSTATUS Status = STATUS_SUCCESS;
54 PTEXTMODE_SCREEN_BUFFER NewBuffer = NULL;
55
56 if (Console == NULL || Buffer == NULL || TextModeInfo == NULL)
57 return STATUS_INVALID_PARAMETER;
58
59 *Buffer = NULL;
60
61 Status = CONSOLE_SCREEN_BUFFER_Initialize((PCONSOLE_SCREEN_BUFFER*)&NewBuffer,
62 Console,
63 sizeof(TEXTMODE_SCREEN_BUFFER));
64 if (!NT_SUCCESS(Status)) return Status;
65 NewBuffer->Header.Type = TEXTMODE_BUFFER;
66 NewBuffer->Vtbl = &TextVtbl;
67
68 NewBuffer->Buffer = ConsoleAllocHeap(HEAP_ZERO_MEMORY,
69 TextModeInfo->ScreenBufferSize.X *
70 TextModeInfo->ScreenBufferSize.Y *
71 sizeof(CHAR_INFO));
72 if (NewBuffer->Buffer == NULL)
73 {
74 CONSOLE_SCREEN_BUFFER_Destroy((PCONSOLE_SCREEN_BUFFER)NewBuffer);
75 return STATUS_INSUFFICIENT_RESOURCES;
76 }
77
78 NewBuffer->ScreenBufferSize = NewBuffer->OldScreenBufferSize
79 = TextModeInfo->ScreenBufferSize;
80 NewBuffer->ViewSize = NewBuffer->OldViewSize
81 = Console->ConsoleSize;
82
83 NewBuffer->ViewOrigin.X = NewBuffer->ViewOrigin.Y = 0;
84 NewBuffer->VirtualY = 0;
85
86 NewBuffer->CursorBlinkOn = NewBuffer->ForceCursorOff = FALSE;
87 NewBuffer->CursorInfo.bVisible = (TextModeInfo->IsCursorVisible && (TextModeInfo->CursorSize != 0));
88 NewBuffer->CursorInfo.dwSize = min(max(TextModeInfo->CursorSize, 0), 100);
89
90 NewBuffer->ScreenDefaultAttrib = TextModeInfo->ScreenAttrib;
91 NewBuffer->PopupDefaultAttrib = TextModeInfo->PopupAttrib;
92
93 /* Initialize buffer to be empty with default attributes */
94 for (NewBuffer->CursorPosition.Y = 0 ; NewBuffer->CursorPosition.Y < NewBuffer->ScreenBufferSize.Y; NewBuffer->CursorPosition.Y++)
95 {
96 ClearLineBuffer(NewBuffer);
97 }
98 NewBuffer->CursorPosition.X = NewBuffer->CursorPosition.Y = 0;
99
100 NewBuffer->Mode = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
101
102 *Buffer = (PCONSOLE_SCREEN_BUFFER)NewBuffer;
103 return STATUS_SUCCESS;
104 }
105
106 VOID
107 TEXTMODE_BUFFER_Destroy(IN OUT PCONSOLE_SCREEN_BUFFER Buffer)
108 {
109 PTEXTMODE_SCREEN_BUFFER Buff = (PTEXTMODE_SCREEN_BUFFER)Buffer;
110
111 /*
112 * IMPORTANT !! Reinitialize the type so that we don't enter a recursive
113 * infinite loop when calling CONSOLE_SCREEN_BUFFER_Destroy.
114 */
115 Buffer->Header.Type = SCREEN_BUFFER;
116
117 ConsoleFreeHeap(Buff->Buffer);
118
119 CONSOLE_SCREEN_BUFFER_Destroy(Buffer);
120 }
121
122
123 PCHAR_INFO
124 ConioCoordToPointer(PTEXTMODE_SCREEN_BUFFER Buff, ULONG X, ULONG Y)
125 {
126 return &Buff->Buffer[((Y + Buff->VirtualY) % Buff->ScreenBufferSize.Y) * Buff->ScreenBufferSize.X + X];
127 }
128
129 static VOID
130 ClearLineBuffer(PTEXTMODE_SCREEN_BUFFER Buff)
131 {
132 PCHAR_INFO Ptr = ConioCoordToPointer(Buff, 0, Buff->CursorPosition.Y);
133 SHORT Pos;
134
135 for (Pos = 0; Pos < Buff->ScreenBufferSize.X; Pos++, Ptr++)
136 {
137 /* Fill the cell */
138 Ptr->Char.UnicodeChar = L' ';
139 Ptr->Attributes = Buff->ScreenDefaultAttrib;
140 }
141 }
142
143 static __inline BOOLEAN
144 ConioGetIntersection(OUT PSMALL_RECT Intersection,
145 IN PSMALL_RECT Rect1,
146 IN PSMALL_RECT Rect2)
147 {
148 if ( ConioIsRectEmpty(Rect1) ||
149 ConioIsRectEmpty(Rect2) ||
150 (Rect1->Top > Rect2->Bottom) ||
151 (Rect1->Left > Rect2->Right) ||
152 (Rect1->Bottom < Rect2->Top) ||
153 (Rect1->Right < Rect2->Left) )
154 {
155 /* The rectangles do not intersect */
156 ConioInitRect(Intersection, 0, -1, 0, -1);
157 return FALSE;
158 }
159
160 ConioInitRect(Intersection,
161 max(Rect1->Top , Rect2->Top ),
162 max(Rect1->Left , Rect2->Left ),
163 min(Rect1->Bottom, Rect2->Bottom),
164 min(Rect1->Right , Rect2->Right ));
165
166 return TRUE;
167 }
168
169 static __inline BOOLEAN
170 ConioGetUnion(OUT PSMALL_RECT Union,
171 IN PSMALL_RECT Rect1,
172 IN PSMALL_RECT Rect2)
173 {
174 if (ConioIsRectEmpty(Rect1))
175 {
176 if (ConioIsRectEmpty(Rect2))
177 {
178 ConioInitRect(Union, 0, -1, 0, -1);
179 return FALSE;
180 }
181 else
182 {
183 *Union = *Rect2;
184 }
185 }
186 else if (ConioIsRectEmpty(Rect2))
187 {
188 *Union = *Rect1;
189 }
190 else
191 {
192 ConioInitRect(Union,
193 min(Rect1->Top , Rect2->Top ),
194 min(Rect1->Left , Rect2->Left ),
195 max(Rect1->Bottom, Rect2->Bottom),
196 max(Rect1->Right , Rect2->Right ));
197 }
198
199 return TRUE;
200 }
201
202 static VOID
203 ConioComputeUpdateRect(IN PTEXTMODE_SCREEN_BUFFER Buff,
204 IN OUT PSMALL_RECT UpdateRect,
205 IN PCOORD Start,
206 IN UINT Length)
207 {
208 if (Buff->ScreenBufferSize.X <= Start->X + Length)
209 {
210 UpdateRect->Left = 0;
211 }
212 else
213 {
214 UpdateRect->Left = Start->X;
215 }
216 if (Buff->ScreenBufferSize.X <= Start->X + Length)
217 {
218 UpdateRect->Right = Buff->ScreenBufferSize.X - 1;
219 }
220 else
221 {
222 UpdateRect->Right = Start->X + Length - 1;
223 }
224 UpdateRect->Top = Start->Y;
225 UpdateRect->Bottom = Start->Y + (Start->X + Length - 1) / Buff->ScreenBufferSize.X;
226 if (Buff->ScreenBufferSize.Y <= UpdateRect->Bottom)
227 {
228 UpdateRect->Bottom = Buff->ScreenBufferSize.Y - 1;
229 }
230 }
231
232 /*
233 * Move from one rectangle to another. We must be careful about the order that
234 * this is done, to avoid overwriting parts of the source before they are moved.
235 */
236 static VOID
237 ConioMoveRegion(PTEXTMODE_SCREEN_BUFFER ScreenBuffer,
238 PSMALL_RECT SrcRegion,
239 PSMALL_RECT DstRegion,
240 PSMALL_RECT ClipRegion,
241 CHAR_INFO FillChar)
242 {
243 int Width = ConioRectWidth(SrcRegion);
244 int Height = ConioRectHeight(SrcRegion);
245 int SX, SY;
246 int DX, DY;
247 int XDelta, YDelta;
248 int i, j;
249
250 SY = SrcRegion->Top;
251 DY = DstRegion->Top;
252 YDelta = 1;
253 if (SY < DY)
254 {
255 /* Moving down: work from bottom up */
256 SY = SrcRegion->Bottom;
257 DY = DstRegion->Bottom;
258 YDelta = -1;
259 }
260 for (i = 0; i < Height; i++)
261 {
262 PCHAR_INFO SRow = ConioCoordToPointer(ScreenBuffer, 0, SY);
263 PCHAR_INFO DRow = ConioCoordToPointer(ScreenBuffer, 0, DY);
264
265 SX = SrcRegion->Left;
266 DX = DstRegion->Left;
267 XDelta = 1;
268 if (SX < DX)
269 {
270 /* Moving right: work from right to left */
271 SX = SrcRegion->Right;
272 DX = DstRegion->Right;
273 XDelta = -1;
274 }
275 for (j = 0; j < Width; j++)
276 {
277 CHAR_INFO Cell = SRow[SX];
278 if (SX >= ClipRegion->Left && SX <= ClipRegion->Right &&
279 SY >= ClipRegion->Top && SY <= ClipRegion->Bottom)
280 {
281 SRow[SX] = FillChar;
282 }
283 if (DX >= ClipRegion->Left && DX <= ClipRegion->Right &&
284 DY >= ClipRegion->Top && DY <= ClipRegion->Bottom)
285 {
286 DRow[DX] = Cell;
287 }
288 SX += XDelta;
289 DX += XDelta;
290 }
291 SY += YDelta;
292 DY += YDelta;
293 }
294 }
295
296 DWORD
297 ConioEffectiveCursorSize(PCONSOLE Console, DWORD Scale)
298 {
299 DWORD Size = (Console->ActiveBuffer->CursorInfo.dwSize * Scale + 99) / 100;
300 /* If line input in progress, perhaps adjust for insert toggle */
301 if (Console->LineBuffer && !Console->LineComplete && (Console->InsertMode ? !Console->LineInsertToggle : Console->LineInsertToggle))
302 return (Size * 2 <= Scale) ? (Size * 2) : (Size / 2);
303 return Size;
304 }
305
306 NTSTATUS
307 ConioResizeBuffer(PCONSOLE Console,
308 PTEXTMODE_SCREEN_BUFFER ScreenBuffer,
309 COORD Size)
310 {
311 PCHAR_INFO Buffer;
312 DWORD Offset = 0;
313 PCHAR_INFO ptr;
314 WORD CurrentAttribute;
315 USHORT CurrentY;
316 PCHAR_INFO OldBuffer;
317 DWORD i;
318 DWORD diff;
319
320 /* Buffer size is not allowed to be smaller than the view size */
321 if (Size.X < ScreenBuffer->ViewSize.X || Size.Y < ScreenBuffer->ViewSize.Y)
322 return STATUS_INVALID_PARAMETER;
323
324 if (Size.X == ScreenBuffer->ScreenBufferSize.X && Size.Y == ScreenBuffer->ScreenBufferSize.Y)
325 {
326 // FIXME: Trigger a buffer resize event ??
327 return STATUS_SUCCESS;
328 }
329
330 if (Console->FixedSize)
331 {
332 /*
333 * The console is in fixed-size mode, so we cannot resize anything
334 * at the moment. However, keep those settings somewhere so that
335 * we can try to set them up when we will be allowed to do so.
336 */
337 ScreenBuffer->OldScreenBufferSize = Size;
338 return STATUS_NOT_SUPPORTED; // STATUS_SUCCESS
339 }
340
341 Buffer = ConsoleAllocHeap(HEAP_ZERO_MEMORY, Size.X * Size.Y * sizeof(CHAR_INFO));
342 if (!Buffer) return STATUS_NO_MEMORY;
343
344 DPRINT1("Resizing (%d,%d) to (%d,%d)\n", ScreenBuffer->ScreenBufferSize.X, ScreenBuffer->ScreenBufferSize.Y, Size.X, Size.Y);
345 OldBuffer = ScreenBuffer->Buffer;
346
347 for (CurrentY = 0; CurrentY < ScreenBuffer->ScreenBufferSize.Y && CurrentY < Size.Y; CurrentY++)
348 {
349 ptr = ConioCoordToPointer(ScreenBuffer, 0, CurrentY);
350 if (Size.X <= ScreenBuffer->ScreenBufferSize.X)
351 {
352 /* Reduce size */
353 RtlCopyMemory(Buffer + Offset, ptr, Size.X * sizeof(CHAR_INFO));
354 Offset += Size.X;
355 }
356 else
357 {
358 /* Enlarge size */
359 RtlCopyMemory(Buffer + Offset, ptr, ScreenBuffer->ScreenBufferSize.X * sizeof(CHAR_INFO));
360 Offset += ScreenBuffer->ScreenBufferSize.X;
361
362 /* The attribute to be used is the one of the last cell of the current line */
363 CurrentAttribute = ConioCoordToPointer(ScreenBuffer,
364 ScreenBuffer->ScreenBufferSize.X - 1,
365 CurrentY)->Attributes;
366
367 diff = Size.X - ScreenBuffer->ScreenBufferSize.X;
368
369 /* Zero-out the new part of the buffer */
370 for (i = 0; i < diff; i++)
371 {
372 ptr = Buffer + Offset;
373 ptr->Char.UnicodeChar = L' ';
374 ptr->Attributes = CurrentAttribute;
375 ++Offset;
376 }
377 }
378 }
379
380 if (Size.Y > ScreenBuffer->ScreenBufferSize.Y)
381 {
382 diff = Size.X * (Size.Y - ScreenBuffer->ScreenBufferSize.Y);
383
384 /* Zero-out the new part of the buffer */
385 for (i = 0; i < diff; i++)
386 {
387 ptr = Buffer + Offset;
388 ptr->Char.UnicodeChar = L' ';
389 ptr->Attributes = ScreenBuffer->ScreenDefaultAttrib;
390 ++Offset;
391 }
392 }
393
394 (void)InterlockedExchangePointer((PVOID volatile*)&ScreenBuffer->Buffer, Buffer);
395 ConsoleFreeHeap(OldBuffer);
396 ScreenBuffer->ScreenBufferSize = ScreenBuffer->OldScreenBufferSize = Size;
397 ScreenBuffer->VirtualY = 0;
398
399 /* Ensure cursor and window are within buffer */
400 if (ScreenBuffer->CursorPosition.X >= Size.X)
401 ScreenBuffer->CursorPosition.X = Size.X - 1;
402 if (ScreenBuffer->CursorPosition.Y >= Size.Y)
403 ScreenBuffer->CursorPosition.Y = Size.Y - 1;
404 if (ScreenBuffer->ViewOrigin.X > Size.X - ScreenBuffer->ViewSize.X)
405 ScreenBuffer->ViewOrigin.X = Size.X - ScreenBuffer->ViewSize.X;
406 if (ScreenBuffer->ViewOrigin.Y > Size.Y - ScreenBuffer->ViewSize.Y)
407 ScreenBuffer->ViewOrigin.Y = Size.Y - ScreenBuffer->ViewSize.Y;
408
409 /*
410 * Trigger a buffer resize event
411 */
412 if (Console->InputBuffer.Mode & ENABLE_WINDOW_INPUT)
413 {
414 INPUT_RECORD er;
415
416 er.EventType = WINDOW_BUFFER_SIZE_EVENT;
417 er.Event.WindowBufferSizeEvent.dwSize = ScreenBuffer->ScreenBufferSize;
418
419 ConioProcessInputEvent(Console, &er);
420 }
421
422 return STATUS_SUCCESS;
423 }
424
425 static VOID
426 ConioNextLine(PTEXTMODE_SCREEN_BUFFER Buff, PSMALL_RECT UpdateRect, PUINT ScrolledLines)
427 {
428 /* If we hit bottom, slide the viewable screen */
429 if (++Buff->CursorPosition.Y == Buff->ScreenBufferSize.Y)
430 {
431 Buff->CursorPosition.Y--;
432 if (++Buff->VirtualY == Buff->ScreenBufferSize.Y)
433 {
434 Buff->VirtualY = 0;
435 }
436 (*ScrolledLines)++;
437 ClearLineBuffer(Buff);
438 if (UpdateRect->Top != 0)
439 {
440 UpdateRect->Top--;
441 }
442 }
443 UpdateRect->Left = 0;
444 UpdateRect->Right = Buff->ScreenBufferSize.X - 1;
445 UpdateRect->Bottom = Buff->CursorPosition.Y;
446 }
447
448 NTSTATUS
449 ConioWriteConsole(PCONSOLE Console,
450 PTEXTMODE_SCREEN_BUFFER Buff,
451 PWCHAR Buffer,
452 DWORD Length,
453 BOOL Attrib)
454 {
455 UINT i;
456 PCHAR_INFO Ptr;
457 SMALL_RECT UpdateRect;
458 SHORT CursorStartX, CursorStartY;
459 UINT ScrolledLines;
460
461 CursorStartX = Buff->CursorPosition.X;
462 CursorStartY = Buff->CursorPosition.Y;
463 UpdateRect.Left = Buff->ScreenBufferSize.X;
464 UpdateRect.Top = Buff->CursorPosition.Y;
465 UpdateRect.Right = -1;
466 UpdateRect.Bottom = Buff->CursorPosition.Y;
467 ScrolledLines = 0;
468
469 for (i = 0; i < Length; i++)
470 {
471 /*
472 * If we are in processed mode, interpret special characters and
473 * display them correctly. Otherwise, just put them into the buffer.
474 */
475 if (Buff->Mode & ENABLE_PROCESSED_OUTPUT)
476 {
477 /* --- CR --- */
478 if (Buffer[i] == L'\r')
479 {
480 Buff->CursorPosition.X = 0;
481 UpdateRect.Left = min(UpdateRect.Left, Buff->CursorPosition.X);
482 UpdateRect.Right = max(UpdateRect.Right, Buff->CursorPosition.X);
483 continue;
484 }
485 /* --- LF --- */
486 else if (Buffer[i] == L'\n')
487 {
488 Buff->CursorPosition.X = 0;
489 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
490 continue;
491 }
492 /* --- BS --- */
493 else if (Buffer[i] == L'\b')
494 {
495 /* Only handle BS if we're not on the first pos of the first line */
496 if (0 != Buff->CursorPosition.X || 0 != Buff->CursorPosition.Y)
497 {
498 if (0 == Buff->CursorPosition.X)
499 {
500 /* slide virtual position up */
501 Buff->CursorPosition.X = Buff->ScreenBufferSize.X - 1;
502 Buff->CursorPosition.Y--;
503 UpdateRect.Top = min(UpdateRect.Top, Buff->CursorPosition.Y);
504 }
505 else
506 {
507 Buff->CursorPosition.X--;
508 }
509 Ptr = ConioCoordToPointer(Buff, Buff->CursorPosition.X, Buff->CursorPosition.Y);
510 Ptr->Char.UnicodeChar = L' ';
511 Ptr->Attributes = Buff->ScreenDefaultAttrib;
512 UpdateRect.Left = min(UpdateRect.Left, Buff->CursorPosition.X);
513 UpdateRect.Right = max(UpdateRect.Right, Buff->CursorPosition.X);
514 }
515 continue;
516 }
517 /* --- TAB --- */
518 else if (Buffer[i] == L'\t')
519 {
520 UINT EndX;
521
522 UpdateRect.Left = min(UpdateRect.Left, Buff->CursorPosition.X);
523 EndX = (Buff->CursorPosition.X + TAB_WIDTH) & ~(TAB_WIDTH - 1);
524 EndX = min(EndX, (UINT)Buff->ScreenBufferSize.X);
525 Ptr = ConioCoordToPointer(Buff, Buff->CursorPosition.X, Buff->CursorPosition.Y);
526 while (Buff->CursorPosition.X < EndX)
527 {
528 Ptr->Char.UnicodeChar = L' ';
529 Ptr->Attributes = Buff->ScreenDefaultAttrib;
530 ++Ptr;
531 Buff->CursorPosition.X++;
532 }
533 UpdateRect.Right = max(UpdateRect.Right, Buff->CursorPosition.X - 1);
534 if (Buff->CursorPosition.X == Buff->ScreenBufferSize.X)
535 {
536 if (Buff->Mode & ENABLE_WRAP_AT_EOL_OUTPUT)
537 {
538 Buff->CursorPosition.X = 0;
539 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
540 }
541 else
542 {
543 Buff->CursorPosition.X--;
544 }
545 }
546 continue;
547 }
548 // /* --- BEL ---*/
549 // else if (Buffer[i] == L'\a')
550 // {
551 // // FIXME: This MUST BE moved to the terminal emulator frontend!!
552 // DPRINT1("Bell\n");
553 // // SendNotifyMessage(Console->hWindow, PM_CONSOLE_BEEP, 0, 0);
554 // continue;
555 // }
556 }
557 UpdateRect.Left = min(UpdateRect.Left, Buff->CursorPosition.X);
558 UpdateRect.Right = max(UpdateRect.Right, Buff->CursorPosition.X);
559
560 Ptr = ConioCoordToPointer(Buff, Buff->CursorPosition.X, Buff->CursorPosition.Y);
561 Ptr->Char.UnicodeChar = Buffer[i];
562 if (Attrib) Ptr->Attributes = Buff->ScreenDefaultAttrib;
563
564 Buff->CursorPosition.X++;
565 if (Buff->CursorPosition.X == Buff->ScreenBufferSize.X)
566 {
567 if (Buff->Mode & ENABLE_WRAP_AT_EOL_OUTPUT)
568 {
569 Buff->CursorPosition.X = 0;
570 ConioNextLine(Buff, &UpdateRect, &ScrolledLines);
571 }
572 else
573 {
574 Buff->CursorPosition.X = CursorStartX;
575 }
576 }
577 }
578
579 if (!ConioIsRectEmpty(&UpdateRect) && (PCONSOLE_SCREEN_BUFFER)Buff == Console->ActiveBuffer)
580 {
581 TermWriteStream(Console, &UpdateRect, CursorStartX, CursorStartY,
582 ScrolledLines, Buffer, Length);
583 }
584
585 return STATUS_SUCCESS;
586 }
587
588 NTSTATUS NTAPI
589 ConDrvChangeScreenBufferAttributes(IN PCONSOLE Console,
590 IN PTEXTMODE_SCREEN_BUFFER Buffer,
591 IN USHORT NewScreenAttrib,
592 IN USHORT NewPopupAttrib)
593 {
594 DWORD X, Y, Length;
595 PCHAR_INFO Ptr;
596
597 COORD TopLeft = {0};
598 ULONG NumCodesToWrite;
599 USHORT OldScreenAttrib;
600
601 if (Console == NULL || Buffer == NULL)
602 {
603 return STATUS_INVALID_PARAMETER;
604 }
605
606 /* Validity check */
607 ASSERT(Console == Buffer->Header.Console);
608
609 NumCodesToWrite = Buffer->ScreenBufferSize.X * Buffer->ScreenBufferSize.Y;
610 OldScreenAttrib = Buffer->ScreenDefaultAttrib;
611
612 X = TopLeft.X;
613 Y = (TopLeft.Y + Buffer->VirtualY) % Buffer->ScreenBufferSize.Y;
614 Length = NumCodesToWrite;
615
616 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work
617 // Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X]; // May work
618
619 while (Length--)
620 {
621 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work either
622 Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X];
623
624 /*
625 * Change the current colors only if they are the old ones.
626 */
627
628 /* Foreground color */
629 if ((Ptr->Attributes & 0x0F) == (OldScreenAttrib & 0x0F))
630 Ptr->Attributes = (Ptr->Attributes & 0xFFF0) | (NewScreenAttrib & 0x0F);
631
632 /* Background color */
633 if ((Ptr->Attributes & 0xF0) == (OldScreenAttrib & 0xF0))
634 Ptr->Attributes = (Ptr->Attributes & 0xFF0F) | (NewScreenAttrib & 0xF0);
635
636 // ++Ptr;
637
638 if (++X == Buffer->ScreenBufferSize.X)
639 {
640 X = 0;
641
642 if (++Y == Buffer->ScreenBufferSize.Y)
643 {
644 Y = 0;
645 }
646 }
647 }
648
649 /* Save foreground and background colors for both screen and popup */
650 Buffer->ScreenDefaultAttrib = (NewScreenAttrib & 0x00FF);
651 Buffer->PopupDefaultAttrib = (NewPopupAttrib & 0x00FF);
652
653 /* Refresh the display if needed */
654 if ((PCONSOLE_SCREEN_BUFFER)Buffer == Console->ActiveBuffer)
655 {
656 SMALL_RECT UpdateRect;
657 ConioComputeUpdateRect(Buffer, &UpdateRect, &TopLeft, NumCodesToWrite);
658 TermDrawRegion(Console, &UpdateRect);
659 }
660
661 return STATUS_SUCCESS;
662 }
663
664
665 /* PUBLIC DRIVER APIS *********************************************************/
666
667 NTSTATUS NTAPI
668 ConDrvReadConsoleOutput(IN PCONSOLE Console,
669 IN PTEXTMODE_SCREEN_BUFFER Buffer,
670 IN BOOLEAN Unicode,
671 OUT PCHAR_INFO CharInfo/*Buffer*/,
672 IN PCOORD BufferSize,
673 IN PCOORD BufferCoord,
674 IN OUT PSMALL_RECT ReadRegion)
675 {
676 PCHAR_INFO CurCharInfo;
677 SHORT SizeX, SizeY;
678 SMALL_RECT CapturedReadRegion;
679 SMALL_RECT ScreenRect;
680 DWORD i;
681 PCHAR_INFO Ptr;
682 LONG X, Y;
683 UINT CodePage;
684
685 if (Console == NULL || Buffer == NULL || CharInfo == NULL ||
686 BufferSize == NULL || BufferCoord == NULL || ReadRegion == NULL)
687 {
688 return STATUS_INVALID_PARAMETER;
689 }
690
691 /* Validity check */
692 ASSERT(Console == Buffer->Header.Console);
693
694 CapturedReadRegion = *ReadRegion;
695
696 /* FIXME: Is this correct? */
697 CodePage = Console->OutputCodePage;
698
699 SizeX = min(BufferSize->X - BufferCoord->X, ConioRectWidth(&CapturedReadRegion));
700 SizeY = min(BufferSize->Y - BufferCoord->Y, ConioRectHeight(&CapturedReadRegion));
701 CapturedReadRegion.Right = CapturedReadRegion.Left + SizeX;
702 CapturedReadRegion.Bottom = CapturedReadRegion.Top + SizeY;
703
704 ConioInitRect(&ScreenRect, 0, 0, Buffer->ScreenBufferSize.Y, Buffer->ScreenBufferSize.X);
705 if (!ConioGetIntersection(&CapturedReadRegion, &ScreenRect, &CapturedReadRegion))
706 {
707 return STATUS_SUCCESS;
708 }
709
710 for (i = 0, Y = CapturedReadRegion.Top; Y < CapturedReadRegion.Bottom; ++i, ++Y)
711 {
712 CurCharInfo = CharInfo + (i * BufferSize->X);
713
714 Ptr = ConioCoordToPointer(Buffer, CapturedReadRegion.Left, Y);
715 for (X = CapturedReadRegion.Left; X < CapturedReadRegion.Right; ++X)
716 {
717 if (Unicode)
718 {
719 CurCharInfo->Char.UnicodeChar = Ptr->Char.UnicodeChar;
720 }
721 else
722 {
723 // ConsoleUnicodeCharToAnsiChar(Console, &CurCharInfo->Char.AsciiChar, &Ptr->Char.UnicodeChar);
724 WideCharToMultiByte(CodePage, 0, &Ptr->Char.UnicodeChar, 1,
725 &CurCharInfo->Char.AsciiChar, 1, NULL, NULL);
726 }
727 CurCharInfo->Attributes = Ptr->Attributes;
728 ++Ptr;
729 ++CurCharInfo;
730 }
731 }
732
733 ReadRegion->Left = CapturedReadRegion.Left;
734 ReadRegion->Top = CapturedReadRegion.Top ;
735 ReadRegion->Right = CapturedReadRegion.Left + SizeX - 1;
736 ReadRegion->Bottom = CapturedReadRegion.Top + SizeY - 1;
737
738 return STATUS_SUCCESS;
739 }
740
741 NTSTATUS NTAPI
742 ConDrvWriteConsoleOutput(IN PCONSOLE Console,
743 IN PTEXTMODE_SCREEN_BUFFER Buffer,
744 IN BOOLEAN Unicode,
745 IN PCHAR_INFO CharInfo/*Buffer*/,
746 IN PCOORD BufferSize,
747 IN PCOORD BufferCoord,
748 IN OUT PSMALL_RECT WriteRegion)
749 {
750 SHORT i, X, Y, SizeX, SizeY;
751 SMALL_RECT ScreenBuffer;
752 PCHAR_INFO CurCharInfo;
753 SMALL_RECT CapturedWriteRegion;
754 PCHAR_INFO Ptr;
755
756 if (Console == NULL || Buffer == NULL || CharInfo == NULL ||
757 BufferSize == NULL || BufferCoord == NULL || WriteRegion == NULL)
758 {
759 return STATUS_INVALID_PARAMETER;
760 }
761
762 /* Validity check */
763 ASSERT(Console == Buffer->Header.Console);
764
765 CapturedWriteRegion = *WriteRegion;
766
767 SizeX = min(BufferSize->X - BufferCoord->X, ConioRectWidth(&CapturedWriteRegion));
768 SizeY = min(BufferSize->Y - BufferCoord->Y, ConioRectHeight(&CapturedWriteRegion));
769 CapturedWriteRegion.Right = CapturedWriteRegion.Left + SizeX - 1;
770 CapturedWriteRegion.Bottom = CapturedWriteRegion.Top + SizeY - 1;
771
772 /* Make sure WriteRegion is inside the screen buffer */
773 ConioInitRect(&ScreenBuffer, 0, 0, Buffer->ScreenBufferSize.Y - 1, Buffer->ScreenBufferSize.X - 1);
774 if (!ConioGetIntersection(&CapturedWriteRegion, &ScreenBuffer, &CapturedWriteRegion))
775 {
776 /*
777 * It is okay to have a WriteRegion completely outside
778 * the screen buffer. No data is written then.
779 */
780 return STATUS_SUCCESS;
781 }
782
783 for (i = 0, Y = CapturedWriteRegion.Top; Y <= CapturedWriteRegion.Bottom; i++, Y++)
784 {
785 CurCharInfo = CharInfo + (i + BufferCoord->Y) * BufferSize->X + BufferCoord->X;
786
787 Ptr = ConioCoordToPointer(Buffer, CapturedWriteRegion.Left, Y);
788 for (X = CapturedWriteRegion.Left; X <= CapturedWriteRegion.Right; X++)
789 {
790 if (Unicode)
791 {
792 Ptr->Char.UnicodeChar = CurCharInfo->Char.UnicodeChar;
793 }
794 else
795 {
796 ConsoleAnsiCharToUnicodeChar(Console, &Ptr->Char.UnicodeChar, &CurCharInfo->Char.AsciiChar);
797 }
798 Ptr->Attributes = CurCharInfo->Attributes;
799 ++Ptr;
800 ++CurCharInfo;
801 }
802 }
803
804 TermDrawRegion(Console, &CapturedWriteRegion);
805
806 WriteRegion->Left = CapturedWriteRegion.Left;
807 WriteRegion->Top = CapturedWriteRegion.Top ;
808 WriteRegion->Right = CapturedWriteRegion.Left + SizeX - 1;
809 WriteRegion->Bottom = CapturedWriteRegion.Top + SizeY - 1;
810
811 return STATUS_SUCCESS;
812 }
813
814 NTSTATUS NTAPI
815 ConDrvWriteConsole(IN PCONSOLE Console,
816 IN PTEXTMODE_SCREEN_BUFFER ScreenBuffer,
817 IN BOOLEAN Unicode,
818 IN PVOID StringBuffer,
819 IN ULONG NumCharsToWrite,
820 OUT PULONG NumCharsWritten OPTIONAL)
821 {
822 NTSTATUS Status = STATUS_SUCCESS;
823 PWCHAR Buffer = NULL;
824 ULONG Written = 0;
825 ULONG Length;
826
827 if (Console == NULL || ScreenBuffer == NULL /* || StringBuffer == NULL */)
828 return STATUS_INVALID_PARAMETER;
829
830 /* Validity checks */
831 ASSERT(Console == ScreenBuffer->Header.Console);
832 ASSERT((StringBuffer != NULL) || (StringBuffer == NULL && NumCharsToWrite == 0));
833
834 // if (Console->PauseFlags & (PAUSED_FROM_KEYBOARD | PAUSED_FROM_SCROLLBAR | PAUSED_FROM_SELECTION))
835 if (Console->PauseFlags && Console->UnpauseEvent != NULL)
836 {
837 return STATUS_PENDING;
838 }
839
840 if (Unicode)
841 {
842 Buffer = StringBuffer;
843 }
844 else
845 {
846 Length = MultiByteToWideChar(Console->OutputCodePage, 0,
847 (PCHAR)StringBuffer,
848 NumCharsToWrite,
849 NULL, 0);
850 Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length * sizeof(WCHAR));
851 if (Buffer)
852 {
853 MultiByteToWideChar(Console->OutputCodePage, 0,
854 (PCHAR)StringBuffer,
855 NumCharsToWrite,
856 (PWCHAR)Buffer, Length);
857 }
858 else
859 {
860 Status = STATUS_NO_MEMORY;
861 }
862 }
863
864 if (Buffer)
865 {
866 if (NT_SUCCESS(Status))
867 {
868 Status = ConioWriteConsole(Console,
869 ScreenBuffer,
870 Buffer,
871 NumCharsToWrite,
872 TRUE);
873 if (NT_SUCCESS(Status))
874 {
875 Written = NumCharsToWrite;
876 }
877 }
878
879 if (!Unicode) RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
880 }
881
882 if (NumCharsWritten) *NumCharsWritten = Written;
883
884 return Status;
885 }
886
887 NTSTATUS NTAPI
888 ConDrvReadConsoleOutputString(IN PCONSOLE Console,
889 IN PTEXTMODE_SCREEN_BUFFER Buffer,
890 IN CODE_TYPE CodeType,
891 OUT PVOID StringBuffer,
892 IN ULONG NumCodesToRead,
893 IN PCOORD ReadCoord,
894 // OUT PCOORD EndCoord,
895 OUT PULONG CodesRead)
896 {
897 SHORT Xpos, Ypos;
898 PVOID ReadBuffer;
899 ULONG i;
900 ULONG CodeSize;
901 PCHAR_INFO Ptr;
902
903 if (Console == NULL || Buffer == NULL ||
904 ReadCoord == NULL || /* EndCoord == NULL || */ CodesRead == NULL)
905 {
906 return STATUS_INVALID_PARAMETER;
907 }
908
909 /* Validity checks */
910 ASSERT(Console == Buffer->Header.Console);
911 ASSERT((StringBuffer != NULL) || (StringBuffer == NULL && NumCodesToRead == 0));
912
913 switch (CodeType)
914 {
915 case CODE_ASCII:
916 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
917 break;
918
919 case CODE_UNICODE:
920 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
921 break;
922
923 case CODE_ATTRIBUTE:
924 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
925 break;
926
927 default:
928 return STATUS_INVALID_PARAMETER;
929 }
930
931 ReadBuffer = StringBuffer;
932 Xpos = ReadCoord->X;
933 Ypos = (ReadCoord->Y + Buffer->VirtualY) % Buffer->ScreenBufferSize.Y;
934
935 /*
936 * MSDN (ReadConsoleOutputAttribute and ReadConsoleOutputCharacter) :
937 *
938 * If the number of attributes (resp. characters) to be read from extends
939 * beyond the end of the specified screen buffer row, attributes (resp.
940 * characters) are read from the next row. If the number of attributes
941 * (resp. characters) to be read from extends beyond the end of the console
942 * screen buffer, attributes (resp. characters) up to the end of the console
943 * screen buffer are read.
944 *
945 * TODO: Do NOT loop up to NumCodesToRead, but stop before
946 * if we are going to overflow...
947 */
948 // Ptr = ConioCoordToPointer(Buffer, Xpos, Ypos); // Doesn't work
949 for (i = 0; i < min(NumCodesToRead, Buffer->ScreenBufferSize.X * Buffer->ScreenBufferSize.Y); ++i)
950 {
951 // Ptr = ConioCoordToPointer(Buffer, Xpos, Ypos); // Doesn't work either
952 Ptr = &Buffer->Buffer[Xpos + Ypos * Buffer->ScreenBufferSize.X];
953
954 switch (CodeType)
955 {
956 case CODE_ASCII:
957 ConsoleUnicodeCharToAnsiChar(Console, (PCHAR)ReadBuffer, &Ptr->Char.UnicodeChar);
958 break;
959
960 case CODE_UNICODE:
961 *(PWCHAR)ReadBuffer = Ptr->Char.UnicodeChar;
962 break;
963
964 case CODE_ATTRIBUTE:
965 *(PWORD)ReadBuffer = Ptr->Attributes;
966 break;
967 }
968 ReadBuffer = (PVOID)((ULONG_PTR)ReadBuffer + CodeSize);
969 // ++Ptr;
970
971 Xpos++;
972
973 if (Xpos == Buffer->ScreenBufferSize.X)
974 {
975 Xpos = 0;
976 Ypos++;
977
978 if (Ypos == Buffer->ScreenBufferSize.Y)
979 {
980 Ypos = 0;
981 }
982 }
983 }
984
985 // EndCoord->X = Xpos;
986 // EndCoord->Y = (Ypos - Buffer->VirtualY + Buffer->ScreenBufferSize.Y) % Buffer->ScreenBufferSize.Y;
987
988 *CodesRead = (ULONG)((ULONG_PTR)ReadBuffer - (ULONG_PTR)StringBuffer) / CodeSize;
989 // <= NumCodesToRead
990
991 return STATUS_SUCCESS;
992 }
993
994 NTSTATUS NTAPI
995 ConDrvWriteConsoleOutputString(IN PCONSOLE Console,
996 IN PTEXTMODE_SCREEN_BUFFER Buffer,
997 IN CODE_TYPE CodeType,
998 IN PVOID StringBuffer,
999 IN ULONG NumCodesToWrite,
1000 IN PCOORD WriteCoord /*,
1001 OUT PCOORD EndCoord,
1002 OUT PULONG CodesWritten */)
1003 {
1004 NTSTATUS Status = STATUS_SUCCESS;
1005 PVOID WriteBuffer = NULL;
1006 PWCHAR tmpString = NULL;
1007 DWORD X, Y, Length; // , Written = 0;
1008 ULONG CodeSize;
1009 PCHAR_INFO Ptr;
1010
1011 if (Console == NULL || Buffer == NULL ||
1012 WriteCoord == NULL /* || EndCoord == NULL || CodesWritten == NULL */)
1013 {
1014 return STATUS_INVALID_PARAMETER;
1015 }
1016
1017 /* Validity checks */
1018 ASSERT(Console == Buffer->Header.Console);
1019 ASSERT((StringBuffer != NULL) || (StringBuffer == NULL && NumCodesToWrite == 0));
1020
1021 switch (CodeType)
1022 {
1023 case CODE_ASCII:
1024 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, AsciiChar);
1025 break;
1026
1027 case CODE_UNICODE:
1028 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, UnicodeChar);
1029 break;
1030
1031 case CODE_ATTRIBUTE:
1032 CodeSize = RTL_FIELD_SIZE(CODE_ELEMENT, Attribute);
1033 break;
1034
1035 default:
1036 return STATUS_INVALID_PARAMETER;
1037 }
1038
1039 if (CodeType == CODE_ASCII)
1040 {
1041 /* Convert the ASCII string into Unicode before writing it to the console */
1042 Length = MultiByteToWideChar(Console->OutputCodePage, 0,
1043 (PCHAR)StringBuffer,
1044 NumCodesToWrite,
1045 NULL, 0);
1046 tmpString = WriteBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, Length * sizeof(WCHAR));
1047 if (WriteBuffer)
1048 {
1049 MultiByteToWideChar(Console->OutputCodePage, 0,
1050 (PCHAR)StringBuffer,
1051 NumCodesToWrite,
1052 (PWCHAR)WriteBuffer, Length);
1053 }
1054 else
1055 {
1056 Status = STATUS_NO_MEMORY;
1057 }
1058 }
1059 else
1060 {
1061 /* For CODE_UNICODE or CODE_ATTRIBUTE, we are already OK */
1062 WriteBuffer = StringBuffer;
1063 }
1064
1065 if (WriteBuffer == NULL || !NT_SUCCESS(Status)) goto Cleanup;
1066
1067 X = WriteCoord->X;
1068 Y = (WriteCoord->Y + Buffer->VirtualY) % Buffer->ScreenBufferSize.Y;
1069 Length = NumCodesToWrite;
1070 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work
1071 // Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X]; // May work
1072
1073 while (Length--)
1074 {
1075 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work either
1076 Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X];
1077
1078 switch (CodeType)
1079 {
1080 case CODE_ASCII:
1081 case CODE_UNICODE:
1082 Ptr->Char.UnicodeChar = *(PWCHAR)WriteBuffer;
1083 break;
1084
1085 case CODE_ATTRIBUTE:
1086 Ptr->Attributes = *(PWORD)WriteBuffer;
1087 break;
1088 }
1089 WriteBuffer = (PVOID)((ULONG_PTR)WriteBuffer + CodeSize);
1090 // ++Ptr;
1091
1092 // Written++;
1093 if (++X == Buffer->ScreenBufferSize.X)
1094 {
1095 X = 0;
1096
1097 if (++Y == Buffer->ScreenBufferSize.Y)
1098 {
1099 Y = 0;
1100 }
1101 }
1102 }
1103
1104 if ((PCONSOLE_SCREEN_BUFFER)Buffer == Console->ActiveBuffer)
1105 {
1106 SMALL_RECT UpdateRect;
1107 ConioComputeUpdateRect(Buffer, &UpdateRect, WriteCoord, NumCodesToWrite);
1108 TermDrawRegion(Console, &UpdateRect);
1109 }
1110
1111 // EndCoord->X = X;
1112 // EndCoord->Y = (Y + Buffer->ScreenBufferSize.Y - Buffer->VirtualY) % Buffer->ScreenBufferSize.Y;
1113
1114 Cleanup:
1115 if (tmpString) RtlFreeHeap(RtlGetProcessHeap(), 0, tmpString);
1116
1117 // CodesWritten = Written;
1118 return Status;
1119 }
1120
1121 NTSTATUS NTAPI
1122 ConDrvFillConsoleOutput(IN PCONSOLE Console,
1123 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1124 IN CODE_TYPE CodeType,
1125 IN CODE_ELEMENT Code,
1126 IN ULONG NumCodesToWrite,
1127 IN PCOORD WriteCoord /*,
1128 OUT PULONG CodesWritten */)
1129 {
1130 DWORD X, Y, Length; // , Written = 0;
1131 PCHAR_INFO Ptr;
1132
1133 if (Console == NULL || Buffer == NULL || WriteCoord == NULL /* || CodesWritten == NULL */)
1134 {
1135 return STATUS_INVALID_PARAMETER;
1136 }
1137
1138 /* Validity check */
1139 ASSERT(Console == Buffer->Header.Console);
1140
1141 if (CodeType == CODE_ASCII)
1142 {
1143 /* Conversion from the ASCII char to the UNICODE char */
1144 CODE_ELEMENT tmp;
1145 ConsoleAnsiCharToUnicodeChar(Console, &tmp.UnicodeChar, &Code.AsciiChar);
1146 Code = tmp;
1147 }
1148
1149 X = WriteCoord->X;
1150 Y = (WriteCoord->Y + Buffer->VirtualY) % Buffer->ScreenBufferSize.Y;
1151 Length = NumCodesToWrite;
1152 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work
1153 // Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X]; // May work
1154
1155 while (Length--)
1156 {
1157 // Ptr = ConioCoordToPointer(Buffer, X, Y); // Doesn't work either
1158 Ptr = &Buffer->Buffer[X + Y * Buffer->ScreenBufferSize.X];
1159
1160 switch (CodeType)
1161 {
1162 case CODE_ASCII:
1163 case CODE_UNICODE:
1164 Ptr->Char.UnicodeChar = Code.UnicodeChar;
1165 break;
1166
1167 case CODE_ATTRIBUTE:
1168 Ptr->Attributes = Code.Attribute;
1169 break;
1170 }
1171 // ++Ptr;
1172
1173 // Written++;
1174 if (++X == Buffer->ScreenBufferSize.X)
1175 {
1176 X = 0;
1177
1178 if (++Y == Buffer->ScreenBufferSize.Y)
1179 {
1180 Y = 0;
1181 }
1182 }
1183 }
1184
1185 if ((PCONSOLE_SCREEN_BUFFER)Buffer == Console->ActiveBuffer)
1186 {
1187 SMALL_RECT UpdateRect;
1188 ConioComputeUpdateRect(Buffer, &UpdateRect, WriteCoord, NumCodesToWrite);
1189 TermDrawRegion(Console, &UpdateRect);
1190 }
1191
1192 // CodesWritten = Written; // NumCodesToWrite;
1193 return STATUS_SUCCESS;
1194 }
1195
1196 NTSTATUS NTAPI
1197 ConDrvGetConsoleScreenBufferInfo(IN PCONSOLE Console,
1198 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1199 OUT PCOORD ScreenBufferSize,
1200 OUT PCOORD CursorPosition,
1201 OUT PCOORD ViewOrigin,
1202 OUT PCOORD ViewSize,
1203 OUT PCOORD MaximumViewSize,
1204 OUT PWORD Attributes)
1205 {
1206 if (Console == NULL || Buffer == NULL || ScreenBufferSize == NULL ||
1207 CursorPosition == NULL || ViewOrigin == NULL || ViewSize == NULL ||
1208 MaximumViewSize == NULL || Attributes == NULL)
1209 {
1210 return STATUS_INVALID_PARAMETER;
1211 }
1212
1213 /* Validity check */
1214 ASSERT(Console == Buffer->Header.Console);
1215
1216 *ScreenBufferSize = Buffer->ScreenBufferSize;
1217 *CursorPosition = Buffer->CursorPosition;
1218 *ViewOrigin = Buffer->ViewOrigin;
1219 *ViewSize = Buffer->ViewSize;
1220 *Attributes = Buffer->ScreenDefaultAttrib;
1221
1222 // FIXME: Refine the computation
1223 *MaximumViewSize = Buffer->ScreenBufferSize;
1224
1225 return STATUS_SUCCESS;
1226 }
1227
1228 NTSTATUS NTAPI
1229 ConDrvSetConsoleTextAttribute(IN PCONSOLE Console,
1230 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1231 IN WORD Attributes)
1232 {
1233 if (Console == NULL || Buffer == NULL)
1234 return STATUS_INVALID_PARAMETER;
1235
1236 /* Validity check */
1237 ASSERT(Console == Buffer->Header.Console);
1238
1239 Buffer->ScreenDefaultAttrib = Attributes;
1240 return STATUS_SUCCESS;
1241 }
1242
1243 NTSTATUS NTAPI
1244 ConDrvSetConsoleScreenBufferSize(IN PCONSOLE Console,
1245 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1246 IN PCOORD Size)
1247 {
1248 NTSTATUS Status;
1249
1250 if (Console == NULL || Buffer == NULL || Size == NULL)
1251 return STATUS_INVALID_PARAMETER;
1252
1253 /* Validity check */
1254 ASSERT(Console == Buffer->Header.Console);
1255
1256 Status = ConioResizeBuffer(Console, Buffer, *Size);
1257 if (NT_SUCCESS(Status)) TermResizeTerminal(Console);
1258
1259 return Status;
1260 }
1261
1262 NTSTATUS NTAPI
1263 ConDrvScrollConsoleScreenBuffer(IN PCONSOLE Console,
1264 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1265 IN BOOLEAN Unicode,
1266 IN PSMALL_RECT ScrollRectangle,
1267 IN BOOLEAN UseClipRectangle,
1268 IN PSMALL_RECT ClipRectangle OPTIONAL,
1269 IN PCOORD DestinationOrigin,
1270 IN CHAR_INFO FillChar)
1271 {
1272 COORD CapturedDestinationOrigin;
1273 SMALL_RECT ScreenBuffer;
1274 SMALL_RECT SrcRegion;
1275 SMALL_RECT DstRegion;
1276 SMALL_RECT UpdateRegion;
1277 SMALL_RECT CapturedClipRectangle;
1278
1279 if (Console == NULL || Buffer == NULL || ScrollRectangle == NULL ||
1280 (UseClipRectangle ? ClipRectangle == NULL : FALSE) || DestinationOrigin == NULL)
1281 {
1282 return STATUS_INVALID_PARAMETER;
1283 }
1284
1285 /* Validity check */
1286 ASSERT(Console == Buffer->Header.Console);
1287
1288 CapturedDestinationOrigin = *DestinationOrigin;
1289
1290 /* Make sure the source rectangle is inside the screen buffer */
1291 ConioInitRect(&ScreenBuffer, 0, 0, Buffer->ScreenBufferSize.Y - 1, Buffer->ScreenBufferSize.X - 1);
1292 if (!ConioGetIntersection(&SrcRegion, &ScreenBuffer, ScrollRectangle))
1293 {
1294 return STATUS_SUCCESS;
1295 }
1296
1297 /* If the source was clipped on the left or top, adjust the destination accordingly */
1298 if (ScrollRectangle->Left < 0)
1299 {
1300 CapturedDestinationOrigin.X -= ScrollRectangle->Left;
1301 }
1302 if (ScrollRectangle->Top < 0)
1303 {
1304 CapturedDestinationOrigin.Y -= ScrollRectangle->Top;
1305 }
1306
1307 if (UseClipRectangle)
1308 {
1309 CapturedClipRectangle = *ClipRectangle;
1310 if (!ConioGetIntersection(&CapturedClipRectangle, &CapturedClipRectangle, &ScreenBuffer))
1311 {
1312 return STATUS_SUCCESS;
1313 }
1314 }
1315 else
1316 {
1317 CapturedClipRectangle = ScreenBuffer;
1318 }
1319
1320 ConioInitRect(&DstRegion,
1321 CapturedDestinationOrigin.Y,
1322 CapturedDestinationOrigin.X,
1323 CapturedDestinationOrigin.Y + ConioRectHeight(&SrcRegion) - 1,
1324 CapturedDestinationOrigin.X + ConioRectWidth(&SrcRegion ) - 1);
1325
1326 if (!Unicode)
1327 ConsoleAnsiCharToUnicodeChar(Console, &FillChar.Char.UnicodeChar, &FillChar.Char.AsciiChar);
1328
1329 ConioMoveRegion(Buffer, &SrcRegion, &DstRegion, &CapturedClipRectangle, FillChar);
1330
1331 if ((PCONSOLE_SCREEN_BUFFER)Buffer == Console->ActiveBuffer)
1332 {
1333 ConioGetUnion(&UpdateRegion, &SrcRegion, &DstRegion);
1334 if (ConioGetIntersection(&UpdateRegion, &UpdateRegion, &CapturedClipRectangle))
1335 {
1336 /* Draw update region */
1337 TermDrawRegion(Console, &UpdateRegion);
1338 }
1339 }
1340
1341 return STATUS_SUCCESS;
1342 }
1343
1344 NTSTATUS NTAPI
1345 ConDrvSetConsoleWindowInfo(IN PCONSOLE Console,
1346 IN PTEXTMODE_SCREEN_BUFFER Buffer,
1347 IN BOOLEAN Absolute,
1348 IN PSMALL_RECT WindowRect)
1349 {
1350 SMALL_RECT CapturedWindowRect;
1351
1352 if (Console == NULL || Buffer == NULL || WindowRect == NULL)
1353 return STATUS_INVALID_PARAMETER;
1354
1355 /* Validity check */
1356 ASSERT(Console == Buffer->Header.Console);
1357
1358 CapturedWindowRect = *WindowRect;
1359
1360 if (Absolute == FALSE)
1361 {
1362 /* Relative positions given. Transform them to absolute ones */
1363 CapturedWindowRect.Left += Buffer->ViewOrigin.X;
1364 CapturedWindowRect.Top += Buffer->ViewOrigin.Y;
1365 CapturedWindowRect.Right += Buffer->ViewOrigin.X + Buffer->ViewSize.X - 1;
1366 CapturedWindowRect.Bottom += Buffer->ViewOrigin.Y + Buffer->ViewSize.Y - 1;
1367 }
1368
1369 /* See MSDN documentation on SetConsoleWindowInfo about the performed checks */
1370 if ( (CapturedWindowRect.Left < 0) || (CapturedWindowRect.Top < 0) ||
1371 (CapturedWindowRect.Right >= Buffer->ScreenBufferSize.X) ||
1372 (CapturedWindowRect.Bottom >= Buffer->ScreenBufferSize.Y) ||
1373 (CapturedWindowRect.Right <= CapturedWindowRect.Left) ||
1374 (CapturedWindowRect.Bottom <= CapturedWindowRect.Top) )
1375 {
1376 return STATUS_INVALID_PARAMETER;
1377 }
1378
1379 Buffer->ViewOrigin.X = CapturedWindowRect.Left;
1380 Buffer->ViewOrigin.Y = CapturedWindowRect.Top;
1381
1382 Buffer->ViewSize.X = CapturedWindowRect.Right - CapturedWindowRect.Left + 1;
1383 Buffer->ViewSize.Y = CapturedWindowRect.Bottom - CapturedWindowRect.Top + 1;
1384
1385 // TermResizeTerminal(Console);
1386
1387 return STATUS_SUCCESS;
1388 }
1389
1390 /* EOF */