summaryrefslogtreecommitdiff
path: root/Nt32Pkg/WinNtConsoleDxe/ConsoleIn.c
blob: 4f00f8b15d5d584b1913a16e373086c0bb7bf8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*++

Copyright (c) 2006 - 2007, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

Module Name:

  ConsoleIn.c

Abstract:

  Console based on Win32 APIs.

  This file attaches a SimpleTextIn protocol to a previously open window.

  The constructor for this protocol depends on an open window. Currently
  the SimpleTextOut protocol creates a window when it's constructor is called.
  Thus this code must run after the constructor for the SimpleTextOut
  protocol

--*/
//
// The package level header files this module uses
//
#include <Uefi.h>
#include <WinNtDxe.h>
//
// The protocols, PPI and GUID defintions for this module
//
#include <Protocol/SimpleTextIn.h>
#include <Protocol/WinNtIo.h>
#include <Protocol/SimpleTextOut.h>
#include <Protocol/ComponentName.h>
#include <Protocol/DriverBinding.h>
//
// The Library classes this module consumes
//
#include <Library/DebugLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiDriverEntryPoint.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
#include "Console.h"

//
// Private worker functions
//
STATIC
EFI_STATUS
WinNtSimpleTextInCheckKey (
  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA  *Private
  );

EFI_STATUS
EFIAPI
WinNtSimpleTextInReset (
  IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL          *This,
  IN BOOLEAN                              ExtendedVerification
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  This                  - TODO: add argument description
  ExtendedVerification  - TODO: add argument description

Returns:

  EFI_SUCCESS - TODO: Add description for return value

--*/
{
  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;

  Private = WIN_NT_SIMPLE_TEXT_IN_PRIVATE_DATA_FROM_THIS (This);
  return EFI_SUCCESS;
}

STATIC
EFI_STATUS
WinNtConvertInputRecordToEfiKey (
  IN  INPUT_RECORD    *InputRecord,
  OUT EFI_INPUT_KEY   *Key
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  InputRecord - TODO: add argument description
  Key         - TODO: add argument description

Returns:

  EFI_NOT_READY - TODO: Add description for return value
  EFI_NOT_READY - TODO: Add description for return value
  EFI_NOT_READY - TODO: Add description for return value
  EFI_SUCCESS - TODO: Add description for return value

--*/
{
  //
  // Make sure InputRecord is an event that represents a keypress
  //
  if (InputRecord->EventType == KEY_EVENT) {
    if (!InputRecord->Event.KeyEvent.bKeyDown) {
      return EFI_NOT_READY;
    }
  } else {
    return EFI_NOT_READY;
  }

  //
  // Check to see if we should return a scan code in place of Unicode character.
  //
  Key->ScanCode     = 0;
  Key->UnicodeChar  = 0;
  if ((InputRecord->Event.KeyEvent.dwControlKeyState & (NUMLOCK_ON | ENHANCED_KEY)) != NUMLOCK_ON) {
    //
    // Only check these scan codes if num lock is off.
    //
    switch (InputRecord->Event.KeyEvent.wVirtualScanCode) {
      case 0x48: Key->ScanCode = SCAN_UP;         break;
      case 0x50: Key->ScanCode = SCAN_DOWN;       break;
      case 0x4d: Key->ScanCode = SCAN_RIGHT;      break;
      case 0x4b: Key->ScanCode = SCAN_LEFT;       break;
      case 0x47: Key->ScanCode = SCAN_HOME;       break;
      case 0x4F: Key->ScanCode = SCAN_END;        break;
      case 0x52: Key->ScanCode = SCAN_INSERT;     break;
      case 0x53: Key->ScanCode = SCAN_DELETE;     break;
      case 0x49: Key->ScanCode = SCAN_PAGE_UP;    break;
      case 0x51: Key->ScanCode = SCAN_PAGE_DOWN;  break;
    }
  }

  switch (InputRecord->Event.KeyEvent.wVirtualScanCode) {
    case 0x3b: Key->ScanCode = SCAN_F1;   break;
    case 0x3c: Key->ScanCode = SCAN_F2;   break;
    case 0x3d: Key->ScanCode = SCAN_F3;   break;
    case 0x3e: Key->ScanCode = SCAN_F4;   break;
    case 0x3f: Key->ScanCode = SCAN_F5;   break;
    case 0x40: Key->ScanCode = SCAN_F6;   break;
    case 0x41: Key->ScanCode = SCAN_F7;   break;
    case 0x42: Key->ScanCode = SCAN_F8;   break;
    case 0x43: Key->ScanCode = SCAN_F9;   break;
    case 0x44: Key->ScanCode = SCAN_F10;  break;
    case 0x01: Key->ScanCode = SCAN_ESC;  break;
  }

  //
  // If there's a scan code pass it, and don't pass the char code
  //
  if (Key->ScanCode == 0) {
    Key->UnicodeChar = InputRecord->Event.KeyEvent.uChar.UnicodeChar;
    if (Key->UnicodeChar == 0) {
      return EFI_NOT_READY;
    }
  }

  return EFI_SUCCESS;
}

STATIC
EFI_STATUS
EFIAPI
WinNtSimpleTextInReadKeyStroke (
  IN EFI_SIMPLE_TEXT_INPUT_PROTOCOL          *This,
  OUT EFI_INPUT_KEY                       *Key
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  This  - TODO: add argument description
  Key   - TODO: add argument description

Returns:

  EFI_DEVICE_ERROR - TODO: Add description for return value
  EFI_NOT_READY - TODO: Add description for return value

--*/
{
  EFI_STATUS                      Status;
  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;
  INPUT_RECORD                    InputRecord;
  DWORD                           NtEventCount;

  Private = WIN_NT_SIMPLE_TEXT_IN_PRIVATE_DATA_FROM_THIS (This);

  Status  = WinNtSimpleTextInCheckKey (Private);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  do {

    if (!Private->WinNtThunk->ReadConsoleInput (Private->NtInHandle, &InputRecord, 1, &NtEventCount)) {
      return EFI_DEVICE_ERROR;
    }

    if (NtEventCount == 0) {
      return EFI_NOT_READY;
    }

    //
    // Convert the Input Record to an EFI Keystroke.
    //
    Status = WinNtConvertInputRecordToEfiKey (&InputRecord, Key);
  } while (EFI_ERROR (Status));

  return Status;
}

STATIC
VOID
EFIAPI
WinNtSimpleTextInWaitForKey (
  IN EFI_EVENT          Event,
  IN VOID               *Context
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  Event   - TODO: add argument description
  Context - TODO: add argument description

Returns:

  TODO: add return values

--*/
{
  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private;
  EFI_STATUS                      Status;

  Private = (WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *) Context;
  Status  = WinNtSimpleTextInCheckKey (Private);
  if (!EFI_ERROR (Status)) {
    gBS->SignalEvent (Event);
  }
}

STATIC
EFI_STATUS
WinNtSimpleTextInCheckKey (
  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA   *Private
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  Private - TODO: add argument description

Returns:

  TODO: add return values

--*/
{
  INPUT_RECORD  *InputRecord;
  DWORD         NtEventCount;
  DWORD         ActualNtEventCount;
  EFI_STATUS    Status;
  BOOLEAN       Success;
  UINTN         Index;
  EFI_INPUT_KEY Key;

  InputRecord   = NULL;
  NtEventCount  = 0;
  Private->WinNtThunk->GetNumberOfConsoleInputEvents (Private->NtInHandle, &NtEventCount);
  if (NtEventCount == 0) {
    Status = EFI_NOT_READY;
    goto Done;
  }

  InputRecord = AllocatePool (sizeof (INPUT_RECORD) * NtEventCount);
  if (InputRecord == NULL) {
    Status = EFI_NOT_READY;
    goto Done;
  }

  Success = (BOOLEAN) Private->WinNtThunk->PeekConsoleInput (
                                            Private->NtInHandle,
                                            InputRecord,
                                            NtEventCount,
                                            &ActualNtEventCount
                                            );
  if (!Success) {
    Status = EFI_NOT_READY;
    goto Done;
  }

  Status = EFI_NOT_READY;
  for (Index = 0; Index < (UINTN) ActualNtEventCount; Index++) {
    //
    // Convert the Input Record to an EFI Keystroke.
    //
    Status = WinNtConvertInputRecordToEfiKey (&InputRecord[Index], &Key);
    if (!EFI_ERROR (Status)) {
      Status = EFI_SUCCESS;
      goto Done;
    }
  }

Done:
  if (InputRecord != NULL) {
    FreePool (InputRecord);
  }

  return Status;
}

EFI_STATUS
WinNtSimpleTextInAttachToWindow (
  IN  WIN_NT_SIMPLE_TEXT_PRIVATE_DATA *Private
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  Private - TODO: add argument description

Returns:

  TODO: add return values

--*/
{
  EFI_STATUS  Status;

  Private->NtInHandle                 = Private->WinNtThunk->GetStdHandle (STD_INPUT_HANDLE);

  Private->SimpleTextIn.Reset         = WinNtSimpleTextInReset;
  Private->SimpleTextIn.ReadKeyStroke = WinNtSimpleTextInReadKeyStroke;

  Status = gBS->CreateEvent (
                  EVT_NOTIFY_WAIT,
                  TPL_NOTIFY,
                  WinNtSimpleTextInWaitForKey,
                  Private,
                  &Private->SimpleTextIn.WaitForKey
                  );
  ASSERT_EFI_ERROR (Status);

  return Status;
}