--- /dev/null
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+ ConsoleIn.c\r
+\r
+Abstract:\r
+\r
+ Console based on Win32 APIs.\r
+\r
+ This file attaches a SimpleTextIn protocol to a previously open window.\r
+\r
+ The constructor for this protocol depends on an open window. Currently\r
+ the SimpleTextOut protocol creates a window when it's constructor is called.\r
+ Thus this code must run after the constructor for the SimpleTextOut\r
+ protocol\r
+\r
+--*/\r
+//\r
+// The package level header files this module uses\r
+//\r
+#include <Uefi.h>\r
+#include <WinNtDxe.h>\r
+//\r
+// The protocols, PPI and GUID defintions for this module\r
+//\r
+#include <Protocol/SimpleTextIn.h>\r
+#include <Protocol/WinNtIo.h>\r
+#include <Protocol/SimpleTextOut.h>\r
+#include <Protocol/ComponentName.h>\r
+#include <Protocol/DriverBinding.h>\r
+//\r
+// The Library classes this module consumes\r
+//\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include "Console.h"\r
+\r
+//\r
+// Private worker functions\r
+//\r
+STATIC\r
+EFI_STATUS\r
+WinNtSimpleTextInCheckKey (\r
+ WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private\r
+ );\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtSimpleTextInReset (\r
+ IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
+ IN BOOLEAN ExtendedVerification\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ This - TODO: add argument description\r
+ ExtendedVerification - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ EFI_SUCCESS - TODO: Add description for return value\r
+\r
+--*/\r
+{\r
+ WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;\r
+\r
+ Private = WIN_NT_SIMPLE_TEXT_IN_PRIVATE_DATA_FROM_THIS (This);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+WinNtConvertInputRecordToEfiKey (\r
+ IN INPUT_RECORD *InputRecord,\r
+ OUT EFI_INPUT_KEY *Key\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ InputRecord - TODO: add argument description\r
+ Key - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ EFI_NOT_READY - TODO: Add description for return value\r
+ EFI_NOT_READY - TODO: Add description for return value\r
+ EFI_NOT_READY - TODO: Add description for return value\r
+ EFI_SUCCESS - TODO: Add description for return value\r
+\r
+--*/\r
+{\r
+ //\r
+ // Make sure InputRecord is an event that represents a keypress\r
+ //\r
+ if (InputRecord->EventType == KEY_EVENT) {\r
+ if (!InputRecord->Event.KeyEvent.bKeyDown) {\r
+ return EFI_NOT_READY;\r
+ }\r
+ } else {\r
+ return EFI_NOT_READY;\r
+ }\r
+\r
+ //\r
+ // Check to see if we should return a scan code in place of Unicode character.\r
+ //\r
+ Key->ScanCode = 0;\r
+ Key->UnicodeChar = 0;\r
+ if ((InputRecord->Event.KeyEvent.dwControlKeyState & (NUMLOCK_ON | ENHANCED_KEY)) != NUMLOCK_ON) {\r
+ //\r
+ // Only check these scan codes if num lock is off.\r
+ //\r
+ switch (InputRecord->Event.KeyEvent.wVirtualScanCode) {\r
+ case 0x48: Key->ScanCode = SCAN_UP; break;\r
+ case 0x50: Key->ScanCode = SCAN_DOWN; break;\r
+ case 0x4d: Key->ScanCode = SCAN_RIGHT; break;\r
+ case 0x4b: Key->ScanCode = SCAN_LEFT; break;\r
+ case 0x47: Key->ScanCode = SCAN_HOME; break;\r
+ case 0x4F: Key->ScanCode = SCAN_END; break;\r
+ case 0x52: Key->ScanCode = SCAN_INSERT; break;\r
+ case 0x53: Key->ScanCode = SCAN_DELETE; break;\r
+ case 0x49: Key->ScanCode = SCAN_PAGE_UP; break;\r
+ case 0x51: Key->ScanCode = SCAN_PAGE_DOWN; break;\r
+ }\r
+ }\r
+\r
+ switch (InputRecord->Event.KeyEvent.wVirtualScanCode) {\r
+ case 0x3b: Key->ScanCode = SCAN_F1; break;\r
+ case 0x3c: Key->ScanCode = SCAN_F2; break;\r
+ case 0x3d: Key->ScanCode = SCAN_F3; break;\r
+ case 0x3e: Key->ScanCode = SCAN_F4; break;\r
+ case 0x3f: Key->ScanCode = SCAN_F5; break;\r
+ case 0x40: Key->ScanCode = SCAN_F6; break;\r
+ case 0x41: Key->ScanCode = SCAN_F7; break;\r
+ case 0x42: Key->ScanCode = SCAN_F8; break;\r
+ case 0x43: Key->ScanCode = SCAN_F9; break;\r
+ case 0x44: Key->ScanCode = SCAN_F10; break;\r
+ case 0x01: Key->ScanCode = SCAN_ESC; break;\r
+ }\r
+\r
+ //\r
+ // If there's a scan code pass it, and don't pass the char code\r
+ //\r
+ if (Key->ScanCode == 0) {\r
+ Key->UnicodeChar = InputRecord->Event.KeyEvent.uChar.UnicodeChar;\r
+ if (Key->UnicodeChar == 0) {\r
+ return EFI_NOT_READY;\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtSimpleTextInReadKeyStroke (\r
+ IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This,\r
+ OUT EFI_INPUT_KEY *Key\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ This - TODO: add argument description\r
+ Key - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ EFI_DEVICE_ERROR - TODO: Add description for return value\r
+ EFI_NOT_READY - TODO: Add description for return value\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+ WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;\r
+ INPUT_RECORD InputRecord;\r
+ DWORD NtEventCount;\r
+\r
+ Private = WIN_NT_SIMPLE_TEXT_IN_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ Status = WinNtSimpleTextInCheckKey (Private);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ do {\r
+\r
+ if (!Private->WinNtThunk->ReadConsoleInput (Private->NtInHandle, &InputRecord, 1, &NtEventCount)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ if (NtEventCount == 0) {\r
+ return EFI_NOT_READY;\r
+ }\r
+\r
+ //\r
+ // Convert the Input Record to an EFI Keystroke.\r
+ //\r
+ Status = WinNtConvertInputRecordToEfiKey (&InputRecord, Key);\r
+ } while (EFI_ERROR (Status));\r
+\r
+ return Status;\r
+}\r
+\r
+STATIC\r
+VOID\r
+EFIAPI\r
+WinNtSimpleTextInWaitForKey (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ Event - TODO: add argument description\r
+ Context - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ TODO: add return values\r
+\r
+--*/\r
+{\r
+ WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;\r
+ EFI_STATUS Status;\r
+\r
+ Private = (WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *) Context;\r
+ Status = WinNtSimpleTextInCheckKey (Private);\r
+ if (!EFI_ERROR (Status)) {\r
+ gBS->SignalEvent (Event);\r
+ }\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+WinNtSimpleTextInCheckKey (\r
+ WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ Private - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ TODO: add return values\r
+\r
+--*/\r
+{\r
+ INPUT_RECORD *InputRecord;\r
+ DWORD NtEventCount;\r
+ DWORD ActualNtEventCount;\r
+ EFI_STATUS Status;\r
+ BOOLEAN Success;\r
+ UINTN Index;\r
+ EFI_INPUT_KEY Key;\r
+\r
+ InputRecord = NULL;\r
+ NtEventCount = 0;\r
+ Private->WinNtThunk->GetNumberOfConsoleInputEvents (Private->NtInHandle, &NtEventCount);\r
+ if (NtEventCount == 0) {\r
+ Status = EFI_NOT_READY;\r
+ goto Done;\r
+ }\r
+\r
+ InputRecord = AllocatePool (sizeof (INPUT_RECORD) * NtEventCount);\r
+ if (InputRecord == NULL) {\r
+ Status = EFI_NOT_READY;\r
+ goto Done;\r
+ }\r
+\r
+ Success = (BOOLEAN) Private->WinNtThunk->PeekConsoleInput (\r
+ Private->NtInHandle,\r
+ InputRecord,\r
+ NtEventCount,\r
+ &ActualNtEventCount\r
+ );\r
+ if (!Success) {\r
+ Status = EFI_NOT_READY;\r
+ goto Done;\r
+ }\r
+\r
+ Status = EFI_NOT_READY;\r
+ for (Index = 0; Index < (UINTN) ActualNtEventCount; Index++) {\r
+ //\r
+ // Convert the Input Record to an EFI Keystroke.\r
+ //\r
+ Status = WinNtConvertInputRecordToEfiKey (&InputRecord[Index], &Key);\r
+ if (!EFI_ERROR (Status)) {\r
+ Status = EFI_SUCCESS;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+Done:\r
+ if (InputRecord != NULL) {\r
+ FreePool (InputRecord);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+WinNtSimpleTextInAttachToWindow (\r
+ IN WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ Private - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ TODO: add return values\r
+\r
+--*/\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ Private->NtInHandle = Private->WinNtThunk->GetStdHandle (STD_INPUT_HANDLE);\r
+\r
+ Private->SimpleTextIn.Reset = WinNtSimpleTextInReset;\r
+ Private->SimpleTextIn.ReadKeyStroke = WinNtSimpleTextInReadKeyStroke;\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_WAIT,\r
+ TPL_NOTIFY,\r
+ WinNtSimpleTextInWaitForKey,\r
+ Private,\r
+ &Private->SimpleTextIn.WaitForKey\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ return Status;\r
+}\r