sync to trunk revision 36700
[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 CHECKPOINT;
305 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
306 IoCompleteRequest (Irp, IO_NO_INCREMENT);
307
308 return STATUS_NOT_SUPPORTED;
309 }
310 else
311 {
312 for (i = 0; i < Stack->Parameters.Write.Length; i++, Buffer++)
313 {
314 switch (*Buffer)
315 {
316 case '\b':
317 {
318 if (CursorX > 1)
319 {
320 CursorX--;
321 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
322 AddToSendBuffer(DeviceExtension, 1, ' ');
323 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
324 }
325 else if (CursorY > 1)
326 {
327 CursorX = Columns;
328 CursorY--;
329 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
330 }
331 break;
332 }
333 case '\n':
334 {
335 CursorY++;
336 CursorX = 1;
337 AddToSendBuffer(DeviceExtension, 1, '\n');
338 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', '1', 'H');
339 break;
340 }
341 case '\r':
342 {
343 if (CursorX > 1)
344 {
345 AddToSendBuffer(DeviceExtension, 4, ESC, '[', -(int)(CursorX-1), 'D');
346 CursorX = 1;
347 }
348 break;
349 }
350 case '\t':
351 {
352 ULONG Offset = DeviceExtension->TabWidth - (CursorX % DeviceExtension->TabWidth);
353 for (j = 0; j < Offset; j++)
354 {
355 #ifdef FORCE_POSITION
356 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
357 #endif
358 AddToSendBuffer(DeviceExtension, 1, ' ');
359 CursorX++;
360 if (CursorX > Columns)
361 {
362 CursorX = 1;
363 CursorY++;
364 }
365 }
366 break;
367 }
368 default:
369 {
370 #ifdef FORCE_POSITION
371 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
372 #endif
373 AddToSendBuffer(DeviceExtension, 1, *Buffer);
374 CursorX++;
375 if (CursorX > Columns)
376 {
377 CursorX = 1;
378 DPRINT("Y: %lu -> %lu\n", CursorY, CursorY + 1);
379 CursorY++;
380 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', '1', 'H');
381
382 }
383 }
384 }
385 if (CursorY >= Rows)
386 {
387 DPRINT("Y: %lu -> %lu\n", CursorY, CursorY - 1);
388 CursorY--;
389 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)1, ';', -(int)(Rows), 'r');
390 AddToSendBuffer(DeviceExtension, 2, ESC, 'D');
391 AddToSendBuffer(DeviceExtension, 6, ESC, '[', -(int)CursorY, ';', -(int)CursorX, 'H');
392 }
393 }
394 }
395
396 DeviceExtension->LogicalOffset = ((CursorX-1) + (CursorY-1) * Columns) * 2;
397
398 /* flush output buffer */
399 AddToSendBuffer(DeviceExtension, 0);
400
401 /* Call lower driver */
402 IoSkipCurrentIrpStackLocation(Irp);
403 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
404 }
405
406 NTSTATUS
407 ScreenDeviceControl(
408 IN PDEVICE_OBJECT DeviceObject,
409 IN PIRP Irp)
410 {
411 PIO_STACK_LOCATION Stack;
412 PSCREEN_DEVICE_EXTENSION DeviceExtension;
413 PDEVICE_OBJECT SerialDevice;
414 NTSTATUS Status;
415
416 Stack = IoGetCurrentIrpStackLocation(Irp);
417 DeviceExtension = (PSCREEN_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
418 SerialDevice = ((PGREEN_DEVICE_EXTENSION)DeviceExtension->Green->DeviceExtension)->Serial;
419 if (!SerialDevice)
420 {
421 DPRINT1("Calling blue\n");
422 IoSkipCurrentIrpStackLocation(Irp);
423 return IoCallDriver(DeviceExtension->PreviousBlue, Irp);
424 }
425
426 switch (Stack->Parameters.DeviceIoControl.IoControlCode)
427 {
428 #if 0
429 case IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO:
430 {
431 PCONSOLE_SCREEN_BUFFER_INFO pcsbi;
432 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_SCREEN_BUFFER_INFO\n");
433
434 pcsbi = (PCONSOLE_SCREEN_BUFFER_INFO)Irp->AssociatedIrp.SystemBuffer;
435
436 pcsbi->dwSize.X = DeviceExtension->Columns;
437 pcsbi->dwSize.Y = DeviceExtension->Rows;
438
439 pcsbi->dwCursorPosition.X = (SHORT)(DeviceExtension->LogicalOffset % DeviceExtension->Columns);
440 pcsbi->dwCursorPosition.Y = (SHORT)(DeviceExtension->LogicalOffset / DeviceExtension->Columns);
441
442 pcsbi->wAttributes = DeviceExtension->CharAttribute;
443
444 pcsbi->srWindow.Left = 1;
445 pcsbi->srWindow.Right = DeviceExtension->Columns;
446 pcsbi->srWindow.Top = 1;
447 pcsbi->srWindow.Bottom = DeviceExtension->Rows;
448
449 pcsbi->dwMaximumWindowSize.X = DeviceExtension->Columns;
450 pcsbi->dwMaximumWindowSize.Y = DeviceExtension->Rows;
451
452 Irp->IoStatus.Information = sizeof(CONSOLE_SCREEN_BUFFER_INFO);
453 Status = STATUS_SUCCESS;
454 break;
455 }
456 case IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO:
457 {
458 PCONSOLE_SCREEN_BUFFER_INFO pcsbi;
459 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_SCREEN_BUFFER_INFO\n");
460
461 pcsbi = (PCONSOLE_SCREEN_BUFFER_INFO)Irp->AssociatedIrp.SystemBuffer;
462 /* FIXME: remove */ { pcsbi->dwCursorPosition.X++; }
463 /* FIXME: remove */ { pcsbi->dwCursorPosition.Y++; }
464 ASSERT(pcsbi->dwCursorPosition.X >= 1);
465 ASSERT(pcsbi->dwCursorPosition.Y >= 1);
466 ASSERT(pcsbi->dwCursorPosition.X <= DeviceExtension->Columns);
467 ASSERT(pcsbi->dwCursorPosition.Y <= DeviceExtension->Rows);
468
469 DeviceExtension->LogicalOffset = (
470 (pcsbi->dwCursorPosition.Y-1) * DeviceExtension->Columns +
471 (pcsbi->dwCursorPosition.X-1)) * 2;
472 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
473 -(int)pcsbi->dwCursorPosition.Y, ';',
474 -(int)pcsbi->dwCursorPosition.X, 'H');
475
476 /* flush buffer */
477 AddToSendBuffer(DeviceExtension, 0);
478
479 DeviceExtension->CharAttribute = pcsbi->wAttributes;
480
481 Irp->IoStatus.Information = 0;
482 Status = STATUS_SUCCESS;
483 break;
484 }
485 case IOCTL_CONSOLE_GET_CURSOR_INFO:
486 {
487 PCONSOLE_CURSOR_INFO pcci = (PCONSOLE_CURSOR_INFO)Irp->AssociatedIrp.SystemBuffer;
488 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_CURSOR_INFO\n");
489
490 pcci->dwSize = 1;
491 pcci->bVisible = TRUE;
492
493 Irp->IoStatus.Information = sizeof (CONSOLE_CURSOR_INFO);
494 Status = STATUS_SUCCESS;
495 break;
496 }
497 case IOCTL_CONSOLE_GET_MODE:
498 {
499 PCONSOLE_MODE pcm = (PCONSOLE_MODE)Irp->AssociatedIrp.SystemBuffer;
500 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_GET_MODE\n");
501
502 pcm->dwMode = DeviceExtension->Mode;
503
504 Irp->IoStatus.Information = sizeof(CONSOLE_MODE);
505 Status = STATUS_SUCCESS;
506 break;
507 }
508 case IOCTL_CONSOLE_SET_MODE:
509 {
510 PCONSOLE_MODE pcm = (PCONSOLE_MODE)Irp->AssociatedIrp.SystemBuffer;
511 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_MODE\n");
512
513 DeviceExtension->Mode = pcm->dwMode;
514
515 Irp->IoStatus.Information = 0;
516 Status = STATUS_SUCCESS;
517 break;
518 }
519 case IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE:
520 {
521 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE\n");
522 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_FILL_OUTPUT_ATTRIBUTE */
523 break;
524 }
525 case IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE:
526 {
527 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE\n");
528 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_READ_OUTPUT_ATTRIBUTE */
529 break;
530 }
531 case IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE:
532 {
533 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE\n");
534 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_WRITE_OUTPUT_ATTRIBUTE */
535 break;
536 }
537 case IOCTL_CONSOLE_SET_TEXT_ATTRIBUTE:
538 {
539 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_SET_TEXT_ATTRIBUTE\n");
540
541 DeviceExtension->CharAttribute = (WORD)*(PWORD)Irp->AssociatedIrp.SystemBuffer;
542 Irp->IoStatus.Information = 0;
543 Status = STATUS_SUCCESS;
544 break;
545 }
546 case IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER:
547 {
548 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER\n");
549 Status = STATUS_NOT_IMPLEMENTED; /* FIXME:IOCTL_CONSOLE_FILL_OUTPUT_CHARACTER */
550 break;
551 }
552 case IOCTL_CONSOLE_READ_OUTPUT_CHARACTER:
553 {
554 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_READ_OUTPUT_CHARACTER\n");
555 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_READ_OUTPUT_CHARACTER */
556 break;
557 }
558 case IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER:
559 {
560 DPRINT1("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER\n");
561 Status = STATUS_NOT_IMPLEMENTED; /* FIXME: IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER */
562 break;
563 }
564 case IOCTL_CONSOLE_DRAW:
565 {
566 PCONSOLE_DRAW ConsoleDraw;
567 PUCHAR Video;
568 ULONG x, y;
569 BOOLEAN DoOptimization = FALSE;
570 DPRINT("IRP_MJ_DEVICE_CONTROL / IOCTL_CONSOLE_DRAW\n");
571
572 ConsoleDraw = (PCONSOLE_DRAW)MmGetSystemAddressForMdl(Irp->MdlAddress);
573 /* FIXME: remove */ { ConsoleDraw->X++; ConsoleDraw->CursorX++; }
574 /* FIXME: remove */ { ConsoleDraw->Y++; ConsoleDraw->CursorY++; }
575 DPRINT1("%lu %lu %lu %lu\n",
576 ConsoleDraw->X, ConsoleDraw->Y,
577 ConsoleDraw->SizeX, ConsoleDraw->SizeY);
578 ASSERT(ConsoleDraw->X >= 1);
579 ASSERT(ConsoleDraw->Y >= 1);
580 ASSERT(ConsoleDraw->X <= DeviceExtension->Columns);
581 ASSERT(ConsoleDraw->Y <= DeviceExtension->Rows);
582 ASSERT(ConsoleDraw->X + ConsoleDraw->SizeX >= 1);
583 ASSERT(ConsoleDraw->Y + ConsoleDraw->SizeY >= 1);
584 ASSERT(ConsoleDraw->X + ConsoleDraw->SizeX - 1 <= DeviceExtension->Columns);
585 ASSERT(ConsoleDraw->Y + ConsoleDraw->SizeY - 1 <= DeviceExtension->Rows);
586 ASSERT(ConsoleDraw->CursorX >= 1);
587 ASSERT(ConsoleDraw->CursorY >= 1);
588 ASSERT(ConsoleDraw->CursorX <= DeviceExtension->Columns);
589 ASSERT(ConsoleDraw->CursorY <= DeviceExtension->Rows);
590
591 #if 0
592 if (ConsoleDraw->X == 1
593 && ConsoleDraw->Y == 1
594 && ConsoleDraw->SizeX == DeviceExtension->Columns
595 && ConsoleDraw->SizeY == DeviceExtension->Rows)
596 {
597 CHECKPOINT1;
598 /* search if we need to clear all screen */
599 DoOptimization = TRUE;
600 Video = (PUCHAR)(ConsoleDraw + 1);
601 x = 0;
602 while (DoOptimization && x < DeviceExtension->Columns * DeviceExtension->Rows)
603 {
604 if (Video[x++] != ' ')
605 {
606 CHECKPOINT1;
607 DoOptimization = FALSE;
608 }
609 /*if (Video[x++] != DeviceExtension->CharAttribute) DoOptimization = FALSE; */
610 }
611 if (DoOptimization)
612 {
613 CHECKPOINT1;
614 AddToSendBuffer(DeviceExtension, 4, ESC, '[', '2', 'J');
615 }
616 }
617 #endif
618 /* add here more optimizations if needed */
619
620 if (!DoOptimization)
621 {
622 for (y = 0; y < ConsoleDraw->SizeY; y++)
623 {
624 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
625 -(int)(ConsoleDraw->Y + y), ';',
626 -(int)(ConsoleDraw->X), 'H');
627 Video = (PUCHAR)(ConsoleDraw + 1);
628 Video = &Video[((ConsoleDraw->Y + y) * /*DeviceExtension->Columns +*/ ConsoleDraw->X) * 2];
629 for (x = 0; x < ConsoleDraw->SizeX; x++)
630 {
631 AddToSendBuffer(DeviceExtension, 1, Video[x * 2]);
632 }
633 }
634 }
635
636 DeviceExtension->LogicalOffset = (
637 (ConsoleDraw->CursorY-1) * DeviceExtension->Columns +
638 (ConsoleDraw->CursorX-1)) * 2;
639 AddToSendBuffer(DeviceExtension, 6, ESC, '[',
640 -(int)(ConsoleDraw->CursorY), ';',
641 -(int)(ConsoleDraw->CursorX), 'H');
642
643 /* flush buffer */
644 AddToSendBuffer(DeviceExtension, 0);
645
646 Irp->IoStatus.Information = 0;
647 Status = STATUS_SUCCESS;
648 break;
649 }
650 #endif
651 default:
652 {
653 DPRINT1("IRP_MJ_DEVICE_CONTROL / unknown ioctl code 0x%lx\n",
654 Stack->Parameters.DeviceIoControl.IoControlCode);
655 /* Call lower driver */
656 IoSkipCurrentIrpStackLocation(Irp);
657 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
658 }
659 }
660
661 if (!NT_SUCCESS(Status))
662 {
663 /* Don't call blue (if any), as we encountered an error */
664 Irp->IoStatus.Status = Status;
665 IoCompleteRequest(Irp, IO_NO_INCREMENT);
666 return Status;
667 }
668 else
669 {
670 /* Call lower driver */
671 IoSkipCurrentIrpStackLocation(Irp);
672 return IoCallDriver(DeviceExtension->Common.LowerDevice, Irp);
673 }
674 }