[MSI]
[reactos.git] / rosapps / drivers / green / screen.c
1 /*
2 * PROJECT: ReactOS VT100 emulator
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/base/green/screen.c
5 * PURPOSE: IRP_MJ_PNP operations
6 * PROGRAMMERS: Copyright 2005 Eric Kohl (ekohl@abo.rhein-zeitung.de)
7 * Copyright 2005 Art Yerkes
8 * Copyright 2005-2006 Hervé Poussineau (hpoussin@reactos.org)
9 */
10
11 #include "green.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 #define ESC ((UCHAR)0x1b)
17
18 /* Force a move of the cursor on each printer char.
19 * Very useful for debug, but it is very slow...
20 */
21 //#define FORCE_POSITION
22
23 /* UCHAR is promoted to int when passed through '...',
24 * so we get int with va_arg and cast them back to UCHAR.
25 */
26 static VOID
27 AddToSendBuffer(
28 IN PSCREEN_DEVICE_EXTENSION DeviceExtension,
29 IN ULONG NumberOfChars,
30 ... /* IN int */)
31 {
32 PIRP Irp;
33 IO_STATUS_BLOCK ioStatus;
34 va_list args;
35 PDEVICE_OBJECT SerialDevice;
36 ULONG SizeLeft;
37 int CurrentInt;
38 UCHAR CurrentChar;
39 NTSTATUS Status;
40 LARGE_INTEGER ZeroOffset;
41
42 ZeroOffset.QuadPart = 0;
43
44 SizeLeft = sizeof(DeviceExtension->SendBuffer) - DeviceExtension->SendBufferPosition;
45 if (SizeLeft < NumberOfChars * 2 || NumberOfChars == 0)
46 {
47 SerialDevice = ((PGREEN_DEVICE_EXTENSION)DeviceExtension->Green->DeviceExtension)->Serial;
48 Irp = IoBuildSynchronousFsdRequest(
49 IRP_MJ_WRITE,
50 SerialDevice,
51 DeviceExtension->SendBuffer, DeviceExtension->SendBufferPosition,
52 &ZeroOffset,
53 NULL, /* Event */
54 &ioStatus);
55 if (!Irp)
56 {
57 DPRINT1("IoBuildSynchronousFsdRequest() failed. Unable to flush output buffer\n");
58 return;
59 }
60
61 Status = IoCallDriver(SerialDevice, Irp);
62
63 if (!NT_SUCCESS(Status) && Status != STATUS_PENDING)
64 {
65 DPRINT1("IoCallDriver() failed. Status = 0x%08lx\n", Status);
66 return;
67 }
68 DeviceExtension->SendBufferPosition = 0;
69 SizeLeft = sizeof(DeviceExtension->SendBuffer);
70 }
71
72 va_start(args, NumberOfChars);
73 while (NumberOfChars-- > 0)
74 {
75 CurrentInt = va_arg(args, int);
76
77 if (CurrentInt > 0)
78 {
79 CurrentChar = (UCHAR)CurrentInt;
80
81 /* Why 0xff chars are printed on a 'dir' ? */
82 if (CurrentChar == 0xff) CurrentChar = ' ';
83
84 DeviceExtension->SendBuffer[DeviceExtension->SendBufferPosition++] = CurrentChar;
85 SizeLeft--;
86 }
87 else if (CurrentInt == 0)
88 {
89 DeviceExtension->SendBuffer[DeviceExtension->SendBufferPosition++] = '0';
90 SizeLeft--;
91 }
92 else
93 {
94 CurrentInt = -CurrentInt;
95 ASSERT(CurrentInt < 100);
96 if (CurrentInt >= 10)
97 {
98 DeviceExtension->SendBuffer[DeviceExtension->SendBufferPosition++] =
99 (CurrentInt / 10) % 10 + '0';
100 SizeLeft--;
101 }
102 DeviceExtension->SendBuffer[DeviceExtension->SendBufferPosition++] =
103 CurrentInt % 10 + '0';
104 SizeLeft--;
105 }
106 }
107 va_end(args);
108 }
109
110 NTSTATUS
111 ScreenAddDevice(
112 IN PDRIVER_OBJECT DriverObject,
113 IN PDEVICE_OBJECT Pdo)
114 {
115 /* We want to be an upper filter of Blue, if it is existing.
116 * We also *have to* create a Fdo on top of the given Pdo.
117 * Hence, we have 2 cases:
118 * - Blue doesn't exist -> Create a unique Fdo (named Blue) at
119 * the top of the given Pdo
120 * - Blue does exist -> Create a Fdo at the top of the existing
121 * DO, and create a "pass to Green" FDO at the top of the Pdo
122 */
123 PDEVICE_OBJECT Fdo = NULL;
124 PDEVICE_OBJECT PassThroughFdo = NULL;
125 PDEVICE_OBJECT LowerDevice = NULL;
126 PDEVICE_OBJECT PreviousBlue = NULL;
127 PSCREEN_DEVICE_EXTENSION DeviceExtension = NULL;
128 UNICODE_STRING BlueScreenName = RTL_CONSTANT_STRING(L"\\Device\\BlueScreen");
129 NTSTATUS Status;
130
131 DPRINT("ScreenInitialize() called\n");
132
133 /* Try to create a unique Fdo */
134 Status = IoCreateDevice(
135 DriverObject,
136 sizeof(SCREEN_DEVICE_EXTENSION),
137 &BlueScreenName,
138 FILE_DEVICE_SCREEN,
139 FILE_DEVICE_SECURE_OPEN,
140 TRUE,
141 &Fdo);
142
143 if (Status == STATUS_OBJECT_NAME_COLLISION)
144 {
145 DPRINT("Attaching to old blue\n");
146
147 /* Suggested by hpoussin .. Hide previous blue device
148 * This makes us able to coexist with blue, and install
149 * when loaded */
150 Status = IoCreateDevice(
151 DriverObject,
152 sizeof(SCREEN_DEVICE_EXTENSION),
153 NULL,
154 FILE_DEVICE_SCREEN,
155 FILE_DEVICE_SECURE_OPEN,
156 TRUE,
157 &Fdo);
158 if (!NT_SUCCESS(Status))
159 {
160 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
161 goto cleanup;
162 }
163
164 /* Initialize some fields, as IoAttachDevice will trigger the
165 * sending of IRP_MJ_CLEANUP/IRP_MJ_CLOSE. We have to know where to
166 * dispatch these IRPs... */
167 ((PSCREEN_DEVICE_EXTENSION)Fdo->DeviceExtension)->Common.Type = ScreenPDO;
168 Status = IoAttachDevice(
169 Fdo,
170 &BlueScreenName,
171 &LowerDevice);
172 if (!NT_SUCCESS(Status))
173 {
174 DPRINT("IoAttachDevice() failed with status 0x%08lx\n", Status);
175 goto cleanup;
176 }
177 PreviousBlue = LowerDevice;
178
179 /* Attach a faked FDO to PDO */
180 Status = IoCreateDevice(
181 DriverObject,
182 sizeof(COMMON_FDO_DEVICE_EXTENSION),
183 NULL,
184 FILE_DEVICE_SCREEN,
185 FILE_DEVICE_SECURE_OPEN,
186 TRUE,
187 &PassThroughFdo);
188 if (!NT_SUCCESS(Status))
189 {
190 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
191 goto cleanup;
192 }
193 ((PCOMMON_FDO_DEVICE_EXTENSION)PassThroughFdo->DeviceExtension)->Type = PassThroughFDO;
194 ((PCOMMON_FDO_DEVICE_EXTENSION)PassThroughFdo->DeviceExtension)->LowerDevice = Fdo;
195 PassThroughFdo->StackSize = Fdo->StackSize + 1;
196 }
197 else if (NT_SUCCESS(Status))
198 {
199 /* Attach the named Fdo on top of Pdo */
200 LowerDevice = IoAttachDeviceToDeviceStack(Fdo, Pdo);
201 }
202 else
203 {
204 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
205 return Status;
206 }
207
208 /* We definately have a device object. PreviousBlue may or may
209 * not be null */
210 DeviceExtension = (PSCREEN_DEVICE_EXTENSION)Fdo->DeviceExtension;
211 RtlZeroMemory(DeviceExtension, sizeof(SCREEN_DEVICE_EXTENSION));
212 DeviceExtension->Common.Type = ScreenFDO;
213 DeviceExtension->Common.LowerDevice = LowerDevice;
214 DeviceExtension->Green = ((PGREEN_DRIVER_EXTENSION)IoGetDriverObjectExtension(DriverObject, DriverObject))->GreenMainDO;
215 ((PGREEN_DEVICE_EXTENSION)DeviceExtension->Green->DeviceExtension)->ScreenFdo = Fdo;
216 DeviceExtension->PreviousBlue = PreviousBlue;
217 IoAttachDeviceToDeviceStack(PassThroughFdo ? PassThroughFdo : Fdo, Pdo);
218
219 /* initialize screen */
220 DeviceExtension->Columns = 80;
221 DeviceExtension->Rows = 25;
222 DeviceExtension->ScanLines = 16;
223 DeviceExtension->VideoMemory = (PUCHAR)ExAllocatePool(
224 PagedPool,
225 2 * DeviceExtension->Columns * DeviceExtension->Rows * sizeof(UCHAR));
226 if (!DeviceExtension->VideoMemory)
227 {
228 DPRINT("ExAllocatePool() failed\n");
229 Status = STATUS_INSUFFICIENT_RESOURCES;
230 goto cleanup;
231 }
232 DeviceExtension->TabWidth = 8;
233
234 /* more initialization */
235 DeviceExtension->Mode = ENABLE_PROCESSED_OUTPUT |
236 ENABLE_WRAP_AT_EOL_OUTPUT;
237
238 /* initialize screen at next write */
239 AddToSendBuffer(DeviceExtension, 2, ESC, 'c'); /* reset device */
240 AddToSendBuffer(DeviceExtension, 4, ESC, '[', '7', 'l'); /* disable line wrap */
241 AddToSendBuffer(DeviceExtension, 4, ESC, '[', '3', 'g'); /* clear all tabs */
242
243 Fdo->Flags |= DO_POWER_PAGABLE;
244 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
245
246 Status = STATUS_SUCCESS;
247
248 cleanup:
249 if (!NT_SUCCESS(Status))
250 {
251 if (DeviceExtension)
252 ExFreePool(DeviceExtension->VideoMemory);
253 if (LowerDevice)
254 IoDetachDevice(LowerDevice);
255 if (Fdo)
256 IoDeleteDevice(Fdo);
257 if (PassThroughFdo)
258 IoDeleteDevice(PassThroughFdo);
259 }
260
261 return Status;
262 }
263
264 NTSTATUS
265 ScreenWrite(
266 IN PDEVICE_OBJECT DeviceObject,
267 IN PIRP Irp)
268 {
269 PIO_STACK_LOCATION Stack;
270 PUCHAR Buffer;
271 PSCREEN_DEVICE_EXTENSION DeviceExtension;
272 PDEVICE_OBJECT SerialDevice;
273 PUCHAR VideoMemory; /* FIXME: is it useful? */
274 ULONG VideoMemorySize; /* FIXME: is it useful? */
275
276 ULONG Columns, Rows;
277 ULONG CursorX, CursorY;
278 ULONG i, j;
279
280 DPRINT("ScreenWrite() called\n");
281
282 Stack = IoGetCurrentIrpStackLocation (Irp);
283 Buffer = Irp->UserBuffer;
284 DeviceExtension = (PSCREEN_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
285 VideoMemory = DeviceExtension->VideoMemory;
286
287 SerialDevice = ((PGREEN_DEVICE_EXTENSION)DeviceExtension->Green->DeviceExtension)->Serial;
288 if (!SerialDevice)
289 {
290 DPRINT1("Calling blue\n");
291 IoSkipCurrentIrpStackLocation(Irp);
292 return IoCallDriver(DeviceExtension->PreviousBlue, Irp);
293 }
294
295 Columns = DeviceExtension->Columns;
296 Rows = DeviceExtension->Rows;
297 CursorX = (DeviceExtension->LogicalOffset / 2) % Columns + 1;
298 CursorY = (DeviceExtension->LogicalOffset / 2) / Columns + 1;
299 VideoMemorySize = Columns * Rows * 2 * sizeof(UCHAR);
300
301 if (!(DeviceExtension->Mode & ENABLE_PROCESSED_OUTPUT))
302 {
303 /* raw output mode */
304 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
305 IoCompleteRequest (Irp, IO_NO_INCREMENT);
306
307 return STATUS_NOT_SUPPORTED;
308 }
309 else
310 {
311 for (i = 0; i < Stack->Parameters.Write.Length; i++, Buffer++)
312 {
313 switch (*Buffer)
314 {
315 case '\b':
316 {
317 if (CursorX > 1)
318 {
319 CursorX--;
320 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
321 AddToSendBuffer(DeviceExtension, 1, ' ');
322 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
323 }
324 else if (CursorY > 1)
325 {
326 CursorX = Columns;
327 CursorY--;
328 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
329 }
330 break;
331 }
332 case '\n':
333 {
334 CursorY++;
335 CursorX = 1;
336 AddToSendBuffer(DeviceExtension, 1, '\n');
337 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', '1', 'H');
338 break;
339 }
340 case '\r':
341 {
342 if (CursorX > 1)
343 {
344 AddToSendBuffer(DeviceExtension, 4, ESC, '[', -(int)(CursorX-1), 'D');
345 CursorX = 1;
346 }
347 break;
348 }
349 case '\t':
350 {
351 ULONG Offset = DeviceExtension->TabWidth - (CursorX % DeviceExtension->TabWidth);
352 for (j = 0; j < Offset; j++)
353 {
354 #ifdef FORCE_POSITION
355 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
356 #endif
357 AddToSendBuffer(DeviceExtension, 1, ' ');
358 CursorX++;
359 if (CursorX > Columns)
360 {
361 CursorX = 1;
362 CursorY++;
363 }
364 }
365 break;
366 }
367 default:
368 {
369 #ifdef FORCE_POSITION
370 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
371 #endif
372 AddToSendBuffer(DeviceExtension, 1, *Buffer);
373 CursorX++;
374 if (CursorX > Columns)
375 {
376 CursorX = 1;
377 DPRINT("Y: %lu -> %lu\n", CursorY, CursorY + 1);
378 CursorY++;
379 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', '1', 'H');
380
381 }
382 }
383 }
384 if (CursorY >= Rows)
385 {
386 DPRINT("Y: %lu -> %lu\n", CursorY, CursorY - 1);
387 CursorY--;
388 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)1, ';', -(int)(Rows), 'r');
389 AddToSendBuffer(DeviceExtension, 2, ESC, 'D');
390 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
391 }
392 }
393 }
394
395 DeviceExtension->LogicalOffset = ((CursorX-1) + (CursorY-1) * Columns) * 2;
396
397 /* flush output buffer */
398 AddToSendBuffer(DeviceExtension, 0);
399
400 /* Call lower driver */
401 IoSkipCurrentIrpStackLocation(Irp);
402 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
403 }
404
405 NTSTATUS
406 ScreenDeviceControl(
407 IN PDEVICE_OBJECT DeviceObject,
408 IN PIRP Irp)
409 {
410 PIO_STACK_LOCATION Stack;
411 PSCREEN_DEVICE_EXTENSION DeviceExtension;
412 PDEVICE_OBJECT SerialDevice;
413 NTSTATUS Status;
414
415 Stack = IoGetCurrentIrpStackLocation(Irp);
416 DeviceExtension = (PSCREEN_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
417 SerialDevice = ((PGREEN_DEVICE_EXTENSION)DeviceExtension->Green->DeviceExtension)->Serial;
418 if (!SerialDevice)
419 {
420 DPRINT1("Calling blue\n");
421 IoSkipCurrentIrpStackLocation(Irp);
422 return IoCallDriver(DeviceExtension->PreviousBlue, Irp);
423 }
424
425 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
426 {
427 #if 0
428 case IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO:
429 {
430 PCONSOLE_SCREEN_BUFFER_INFO pcsbi;
431 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO\n");
432
433 pcsbi = (PCONSOLE_SCREEN_BUFFER_INFO)Irp->AssociatedIrp.SystemBuffer;
434
435 pcsbi->dwSize.X = DeviceExtension->Columns;
436 pcsbi->dwSize.Y = DeviceExtension->Rows;
437
438 pcsbi->dwCursorPosition.X = (SHORT)(DeviceExtension->LogicalOffset % DeviceExtension->Columns);
439 pcsbi->dwCursorPosition.Y = (SHORT)(DeviceExtension->LogicalOffset / DeviceExtension->Columns);
440
441 pcsbi->wAttributes = DeviceExtension->CharAttribute;
442
443 pcsbi->srWindow.Left = 1;
444 pcsbi->srWindow.Right = DeviceExtension->Columns;
445 pcsbi->srWindow.Top = 1;
446 pcsbi->srWindow.Bottom = DeviceExtension->Rows;
447
448 pcsbi->dwMaximumWindowSize.X = DeviceExtension->Columns;
449 pcsbi->dwMaximumWindowSize.Y = DeviceExtension->Rows;
450
451 Irp->IoStatus.Information = sizeof(CONSOLE_SCREEN_BUFFER_INFO);
452 Status = STATUS_SUCCESS;
453 break;
454 }
455 case IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO:
456 {
457 PCONSOLE_SCREEN_BUFFER_INFO pcsbi;
458 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO\n");
459
460 pcsbi = (PCONSOLE_SCREEN_BUFFER_INFO)Irp->AssociatedIrp.SystemBuffer;
461 /* FIXME: remove */ { pcsbi->dwCursorPosition.X++; }
462 /* FIXME: remove */ { pcsbi->dwCursorPosition.Y++; }
463 ASSERT(pcsbi->dwCursorPosition.X >= 1);
464 ASSERT(pcsbi->dwCursorPosition.Y >= 1);
465 ASSERT(pcsbi->dwCursorPosition.X <= DeviceExtension->Columns);
466 ASSERT(pcsbi->dwCursorPosition.Y <= DeviceExtension->Rows);
467
468 DeviceExtension->LogicalOffset = (
469 (pcsbi->dwCursorPosition.Y-1) * DeviceExtension->Columns +
470 (pcsbi->dwCursorPosition.X-1)) * 2;
471 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
472 -(int)pcsbi->dwCursorPosition.Y, ';',
473 -(int)pcsbi->dwCursorPosition.X, 'H');
474
475 /* flush buffer */
476 AddToSendBuffer(DeviceExtension, 0);
477
478 DeviceExtension->CharAttribute = pcsbi->wAttributes;
479
480 Irp->IoStatus.Information = 0;
481 Status = STATUS_SUCCESS;
482 break;
483 }
484 case IOCTL_CONSOLE_GET_CURSOR_INFO:
485 {
486 PCONSOLE_CURSOR_INFO pcci = (PCONSOLE_CURSOR_INFO)Irp->AssociatedIrp.SystemBuffer;
487 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_CURSOR_INFO\n");
488
489 pcci->dwSize = 1;
490 pcci->bVisible = TRUE;
491
492 Irp->IoStatus.Information = sizeof (CONSOLE_CURSOR_INFO);
493 Status = STATUS_SUCCESS;
494 break;
495 }
496 case IOCTL_CONSOLE_GET_MODE:
497 {
498 PCONSOLE_MODE pcm = (PCONSOLE_MODE)Irp->AssociatedIrp.SystemBuffer;
499 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_MODE\n");
500
501 pcm->dwMode = DeviceExtension->Mode;
502
503 Irp->IoStatus.Information = sizeof(CONSOLE_MODE);
504 Status = STATUS_SUCCESS;
505 break;
506 }
507 case IOCTL_CONSOLE_SET_MODE:
508 {
509 PCONSOLE_MODE pcm = (PCONSOLE_MODE)Irp->AssociatedIrp.SystemBuffer;
510 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_MODE\n");
511
512 DeviceExtension->Mode = pcm->dwMode;
513
514 Irp->IoStatus.Information = 0;
515 Status = STATUS_SUCCESS;
516 break;
517 }
518 case IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE:
519 {
520 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE\n");
521 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE */
522 break;
523 }
524 case IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE:
525 {
526 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE\n");
527 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE */
528 break;
529 }
530 case IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE:
531 {
532 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE\n");
533 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE */
534 break;
535 }
536 case IOCTL_CONSOLE_SET_TEXT_ATTRIBUTE:
537 {
538 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_TEXT_ATTRIBUTE\n");
539
540 DeviceExtension->CharAttribute = (WORD)*(PWORD)Irp->AssociatedIrp.SystemBuffer;
541 Irp->IoStatus.Information = 0;
542 Status = STATUS_SUCCESS;
543 break;
544 }
545 case IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER:
546 {
547 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER\n");
548 Status = STATUS_NOT_IMPLEMENTED; /* FIXME:IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER */
549 break;
550 }
551 case IOCTL_CONSOLE_READ_OUTPUT_CHARACTER:
552 {
553 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_READ_OUTPUT_CHARACTER\n");
554 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_READ_OUTPUT_CHARACTER */
555 break;
556 }
557 case IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER:
558 {
559 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER\n");
560 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER */
561 break;
562 }
563 case IOCTL_CONSOLE_DRAW:
564 {
565 PCONSOLE_DRAW ConsoleDraw;
566 PUCHAR Video;
567 ULONG x, y;
568 BOOLEAN DoOptimization = FALSE;
569 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_DRAW\n");
570
571 ConsoleDraw = (PCONSOLE_DRAW)MmGetSystemAddressForMdl(Irp->MdlAddress);
572 /* FIXME: remove */ { ConsoleDraw->X++; ConsoleDraw->CursorX++; }
573 /* FIXME: remove */ { ConsoleDraw->Y++; ConsoleDraw->CursorY++; }
574 DPRINT1("%lu %lu %lu %lu\n",
575 ConsoleDraw->X, ConsoleDraw->Y,
576 ConsoleDraw->SizeX, ConsoleDraw->SizeY);
577 ASSERT(ConsoleDraw->X >= 1);
578 ASSERT(ConsoleDraw->Y >= 1);
579 ASSERT(ConsoleDraw->X <= DeviceExtension->Columns);
580 ASSERT(ConsoleDraw->Y <= DeviceExtension->Rows);
581 ASSERT(ConsoleDraw->X + ConsoleDraw->SizeX >= 1);
582 ASSERT(ConsoleDraw->Y + ConsoleDraw->SizeY >= 1);
583 ASSERT(ConsoleDraw->X + ConsoleDraw->SizeX - 1 <= DeviceExtension->Columns);
584 ASSERT(ConsoleDraw->Y + ConsoleDraw->SizeY - 1 <= DeviceExtension->Rows);
585 ASSERT(ConsoleDraw->CursorX >= 1);
586 ASSERT(ConsoleDraw->CursorY >= 1);
587 ASSERT(ConsoleDraw->CursorX <= DeviceExtension->Columns);
588 ASSERT(ConsoleDraw->CursorY <= DeviceExtension->Rows);
589
590 #if 0
591 if (ConsoleDraw->X == 1
592 && ConsoleDraw->Y == 1
593 && ConsoleDraw->SizeX == DeviceExtension->Columns
594 && ConsoleDraw->SizeY == DeviceExtension->Rows)
595 {
596 /* search if we need to clear all screen */
597 DoOptimization = TRUE;
598 Video = (PUCHAR)(ConsoleDraw + 1);
599 x = 0;
600 while (DoOptimization && x < DeviceExtension->Columns * DeviceExtension->Rows)
601 {
602 if (Video[x++] != ' ')
603 {
604 DoOptimization = FALSE;
605 }
606 /*if (Video[x++] != DeviceExtension->CharAttribute) DoOptimization = FALSE; */
607 }
608 if (DoOptimization)
609 {
610 AddToSendBuffer(DeviceExtension, 4, ESC, '[', '2', 'J');
611 }
612 }
613 #endif
614 /* add here more optimizations if needed */
615
616 if (!DoOptimization)
617 {
618 for (y = 0; y < ConsoleDraw->SizeY; y++)
619 {
620 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
621 -(int)(ConsoleDraw->Y + y), ';',
622 -(int)(ConsoleDraw->X), 'H');
623 Video = (PUCHAR)(ConsoleDraw + 1);
624 Video = &Video[((ConsoleDraw->Y + y) * /*DeviceExtension->Columns +*/ ConsoleDraw->X) * 2];
625 for (x = 0; x < ConsoleDraw->SizeX; x++)
626 {
627 AddToSendBuffer(DeviceExtension, 1, Video[x * 2]);
628 }
629 }
630 }
631
632 DeviceExtension->LogicalOffset = (
633 (ConsoleDraw->CursorY-1) * DeviceExtension->Columns +
634 (ConsoleDraw->CursorX-1)) * 2;
635 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
636 -(int)(ConsoleDraw->CursorY), ';',
637 -(int)(ConsoleDraw->CursorX), 'H');
638
639 /* flush buffer */
640 AddToSendBuffer(DeviceExtension, 0);
641
642 Irp->IoStatus.Information = 0;
643 Status = STATUS_SUCCESS;
644 break;
645 }
646 #endif
647 default:
648 {
649 DPRINT1("IRP_MJ_DEVICE_CONTROL / unknown ioctl code 0x%lx\n",
650 Stack->Parameters.DeviceIoControl.IoControlCode);
651 /* Call lower driver */
652 IoSkipCurrentIrpStackLocation(Irp);
653 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
654 }
655 }
656
657 if (!NT_SUCCESS(Status))
658 {
659 /* Don't call blue (if any), as we encountered an error */
660 Irp->IoStatus.Status = Status;
661 IoCompleteRequest(Irp, IO_NO_INCREMENT);
662 return Status;
663 }
664 else
665 {
666 /* Call lower driver */
667 IoSkipCurrentIrpStackLocation(Irp);
668 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
669 }
670 }