]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Win/Host/WinGopScreen.c
0e1f1ff205aa85f4ccc33a6dab6e3730ef9e52c7
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinGopScreen.c
1 /** @file
2
3 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 WinGopScreen.c
15
16 Abstract:
17
18 This file produces the graphics abstration of GOP. It is called by
19 WinNtGopDriver.c file which deals with the UEFI 2.0 driver model.
20 This file just does graphics.
21
22
23 **/
24
25 #include "WinGop.h"
26
27 DWORD mTlsIndex = TLS_OUT_OF_INDEXES;
28 DWORD mTlsIndexUseCount = 0; // lets us know when we can free mTlsIndex.
29
30 BOOLEAN
31 WinNtGopConvertParamToEfiKeyShiftState (
32 IN GRAPHICS_PRIVATE_DATA *Private,
33 IN WPARAM *wParam,
34 IN LPARAM *lParam,
35 IN BOOLEAN Flag
36 )
37 {
38 switch (*wParam) {
39 //
40 // BUGBUG: Only GetAsyncKeyState() and GetKeyState() can distinguish
41 // left and right Ctrl, and Shift key.
42 // Neither of the two is defined in EFI_WIN_NT_THUNK_PROTOCOL.
43 // Therefor, we can not set the correct Shift state here.
44 //
45 case VK_SHIFT:
46 if ((*lParam & GOP_EXTENDED_KEY) == GOP_EXTENDED_KEY) {
47 Private->RightShift = Flag;
48 } else {
49 Private->LeftShift = Flag;
50 }
51 return TRUE;
52
53 case VK_LSHIFT:
54 Private->LeftShift = Flag;
55 return TRUE;
56
57 case VK_RSHIFT:
58 Private->RightShift = Flag;
59 return TRUE;
60
61 case VK_CONTROL:
62 if ((*lParam & GOP_EXTENDED_KEY) == GOP_EXTENDED_KEY) {
63 Private->RightCtrl= Flag;
64 } else {
65 Private->LeftCtrl = Flag;
66 }
67 return TRUE;
68
69 case VK_LCONTROL:
70 Private->LeftCtrl = Flag;
71 return TRUE;
72
73 case VK_RCONTROL:
74 Private->RightCtrl = Flag;
75 return TRUE;
76
77 case VK_LWIN:
78 Private->LeftLogo = Flag;
79 return TRUE;
80
81 case VK_RWIN:
82 Private->RightLogo = Flag;
83 return TRUE;
84
85 case VK_APPS:
86 Private->Menu = Flag;
87 return TRUE;
88 //
89 // BUGBUG: PrintScreen/SysRq can not trigger WM_KEYDOWN message,
90 // so SySReq shift state is not supported here.
91 //
92 case VK_PRINT:
93 Private->SysReq = Flag;
94 return TRUE;
95 //
96 // For Alt Keystroke.
97 //
98 case VK_MENU:
99 if ((*lParam & GOP_EXTENDED_KEY) == GOP_EXTENDED_KEY) {
100 Private->RightAlt = Flag;
101 } else {
102 Private->LeftAlt = Flag;
103 }
104 return TRUE;
105
106 default:
107 return FALSE;
108 }
109 }
110
111 BOOLEAN
112 WinNtGopConvertParamToEfiKey (
113 IN GRAPHICS_PRIVATE_DATA *Private,
114 IN WPARAM *wParam,
115 IN LPARAM *lParam,
116 IN EFI_INPUT_KEY *Key
117 )
118 {
119 BOOLEAN Flag;
120 Flag = FALSE;
121 switch (*wParam) {
122 case VK_HOME: Key->ScanCode = SCAN_HOME; Flag = TRUE; break;
123 case VK_END: Key->ScanCode = SCAN_END; Flag = TRUE; break;
124 case VK_LEFT: Key->ScanCode = SCAN_LEFT; Flag = TRUE; break;
125 case VK_RIGHT: Key->ScanCode = SCAN_RIGHT; Flag = TRUE; break;
126 case VK_UP: Key->ScanCode = SCAN_UP; Flag = TRUE; break;
127 case VK_DOWN: Key->ScanCode = SCAN_DOWN; Flag = TRUE; break;
128 case VK_DELETE: Key->ScanCode = SCAN_DELETE; Flag = TRUE; break;
129 case VK_INSERT: Key->ScanCode = SCAN_INSERT; Flag = TRUE; break;
130 case VK_PRIOR: Key->ScanCode = SCAN_PAGE_UP; Flag = TRUE; break;
131 case VK_NEXT: Key->ScanCode = SCAN_PAGE_DOWN; Flag = TRUE; break;
132 case VK_ESCAPE: Key->ScanCode = SCAN_ESC; Flag = TRUE; break;
133
134 case VK_F1: Key->ScanCode = SCAN_F1; Flag = TRUE; break;
135 case VK_F2: Key->ScanCode = SCAN_F2; Flag = TRUE; break;
136 case VK_F3: Key->ScanCode = SCAN_F3; Flag = TRUE; break;
137 case VK_F4: Key->ScanCode = SCAN_F4; Flag = TRUE; break;
138 case VK_F5: Key->ScanCode = SCAN_F5; Flag = TRUE; break;
139 case VK_F6: Key->ScanCode = SCAN_F6; Flag = TRUE; break;
140 case VK_F7: Key->ScanCode = SCAN_F7; Flag = TRUE; break;
141 case VK_F8: Key->ScanCode = SCAN_F8; Flag = TRUE; break;
142 case VK_F9: Key->ScanCode = SCAN_F9; Flag = TRUE; break;
143 case VK_F11: Key->ScanCode = SCAN_F11; Flag = TRUE; break;
144 case VK_F12: Key->ScanCode = SCAN_F12; Flag = TRUE; break;
145
146 case VK_F13: Key->ScanCode = SCAN_F13; Flag = TRUE; break;
147 case VK_F14: Key->ScanCode = SCAN_F14; Flag = TRUE; break;
148 case VK_F15: Key->ScanCode = SCAN_F15; Flag = TRUE; break;
149 case VK_F16: Key->ScanCode = SCAN_F16; Flag = TRUE; break;
150 case VK_F17: Key->ScanCode = SCAN_F17; Flag = TRUE; break;
151 case VK_F18: Key->ScanCode = SCAN_F18; Flag = TRUE; break;
152 case VK_F19: Key->ScanCode = SCAN_F19; Flag = TRUE; break;
153 case VK_F20: Key->ScanCode = SCAN_F20; Flag = TRUE; break;
154 case VK_F21: Key->ScanCode = SCAN_F21; Flag = TRUE; break;
155 case VK_F22: Key->ScanCode = SCAN_F22; Flag = TRUE; break;
156 case VK_F23: Key->ScanCode = SCAN_F23; Flag = TRUE; break;
157 case VK_F24: Key->ScanCode = SCAN_F24; Flag = TRUE; break;
158 case VK_PAUSE: Key->ScanCode = SCAN_PAUSE; Flag = TRUE; break;
159
160 //
161 // Set toggle state
162 //
163 case VK_NUMLOCK:
164 Private->NumLock = (BOOLEAN)(!Private->NumLock);
165 Flag = TRUE;
166 break;
167 case VK_SCROLL:
168 Private->ScrollLock = (BOOLEAN)(!Private->ScrollLock);
169 Flag = TRUE;
170 break;
171 case VK_CAPITAL:
172 Private->CapsLock = (BOOLEAN)(!Private->CapsLock);
173 Flag = TRUE;
174 break;
175 }
176
177 return (WinNtGopConvertParamToEfiKeyShiftState (Private, wParam, lParam, TRUE)) == TRUE ? TRUE : Flag;
178 }
179
180
181 //
182 // GOP Protocol Member Functions
183 //
184
185 /**
186 Change the resolution and resize of the window
187 **/
188 EFI_STATUS
189 EFIAPI
190 WinNtWndSize (
191 IN EMU_GRAPHICS_WINDOW_PROTOCOL *GraphicsIo,
192 IN UINT32 Width,
193 IN UINT32 Height
194 )
195 {
196 RETURN_STATUS RStatus;
197 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION Info;
198 GRAPHICS_PRIVATE_DATA *Private;
199 RECT Rect;
200 BITMAPV4HEADER *VirtualScreenInfo;
201 FRAME_BUFFER_CONFIGURE *FrameBufferConfigure;
202 UINTN FrameBufferConfigureSize;
203
204 Private = GRAPHICS_PRIVATE_DATA_FROM_THIS (GraphicsIo);
205
206 //
207 // Allocate DIB frame buffer directly from NT for performance enhancement
208 // This buffer is the virtual screen/frame buffer.
209 //
210 VirtualScreenInfo = HeapAlloc (
211 GetProcessHeap (),
212 HEAP_ZERO_MEMORY,
213 Width * Height * sizeof (RGBQUAD) + sizeof (BITMAPV4HEADER)
214 );
215 if (VirtualScreenInfo == NULL) {
216 return EFI_OUT_OF_RESOURCES;
217 }
218
219 //
220 // Update the virtual screen info data structure
221 // Use negative Height to make sure screen/buffer are using the same coordinate.
222 //
223 VirtualScreenInfo->bV4Size = sizeof (BITMAPV4HEADER);
224 VirtualScreenInfo->bV4Width = Width;
225 VirtualScreenInfo->bV4Height = -(LONG)Height;
226 VirtualScreenInfo->bV4Planes = 1;
227 VirtualScreenInfo->bV4BitCount = 32;
228 //
229 // uncompressed
230 //
231 VirtualScreenInfo->bV4V4Compression = BI_RGB;
232
233 Info.HorizontalResolution = Width;
234 Info.VerticalResolution = Height;
235 Info.PixelFormat = PixelBlueGreenRedReserved8BitPerColor;
236 Info.PixelsPerScanLine = Width;
237 FrameBufferConfigureSize = 0;
238 RStatus = FrameBufferBltConfigure (VirtualScreenInfo + 1, &Info, NULL, &FrameBufferConfigureSize);
239 ASSERT (RStatus == EFI_BUFFER_TOO_SMALL);
240 FrameBufferConfigure = AllocatePool (FrameBufferConfigureSize);
241 if (FrameBufferConfigure == NULL) {
242 HeapFree (GetProcessHeap (), 0, VirtualScreenInfo);
243 return EFI_OUT_OF_RESOURCES;
244 }
245 RStatus = FrameBufferBltConfigure (VirtualScreenInfo + 1, &Info, FrameBufferConfigure, &FrameBufferConfigureSize);
246 ASSERT_RETURN_ERROR (RStatus);
247
248
249 if (Private->FrameBufferConfigure != NULL) {
250 FreePool (Private->FrameBufferConfigure);
251 }
252 Private->FrameBufferConfigure = FrameBufferConfigure;
253
254 //
255 // Free the old buffer. We do not save the content of the old buffer since the
256 // screen is to be cleared anyway. Clearing the screen is required by the EFI spec.
257 // See UEFI spec -EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode()
258 //
259 if (Private->VirtualScreenInfo != NULL) {
260 HeapFree (GetProcessHeap (), 0, Private->VirtualScreenInfo);
261 }
262 Private->VirtualScreenInfo = VirtualScreenInfo;
263
264 Private->Width = Width;
265 Private->Height = Height;
266
267 //
268 // Use the AdjuctWindowRect fuction to calculate the real width and height
269 // of the new window including the border and caption
270 //
271 Rect.left = 0;
272 Rect.top = 0;
273 Rect.right = Width;
274 Rect.bottom = Height;
275
276 AdjustWindowRect (&Rect, WS_OVERLAPPEDWINDOW, 0);
277
278 Width = Rect.right - Rect.left;
279 Height = Rect.bottom - Rect.top;
280
281 //
282 // Retrieve the original window position information
283 //
284 GetWindowRect (Private->WindowHandle, &Rect);
285
286 //
287 // Adjust the window size
288 //
289 MoveWindow (Private->WindowHandle, Rect.left, Rect.top, (INT32)Width, (INT32)Height, TRUE);
290
291 return EFI_SUCCESS;
292 }
293
294 /**
295 Blt pixels from the rectangle (Width X Height) formed by the BltBuffer
296 onto the graphics screen starting a location (X, Y). (0, 0) is defined as
297 the upper left hand side of the screen. (X, Y) can be outside of the
298 current screen geometry and the BltBuffer will be cliped when it is
299 displayed. X and Y can be negative or positive. If Width or Height is
300 bigger than the current video screen the image will be clipped.
301
302 @param This Protocol instance pointer.
303 @param X X location on graphics screen.
304 @param Y Y location on the graphics screen.
305 @param Width Width of BltBuffer.
306 @param Height Hight of BltBuffer
307 @param BltOperation Operation to perform on BltBuffer and video memory
308 @param BltBuffer Buffer containing data to blt into video buffer.
309 This buffer has a size of
310 Width*Height*sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
311 @param SourceX If the BltOperation is a EfiCopyBlt this is the
312 source of the copy. For other BLT operations this
313 argument is not used.
314 @param SourceX If the BltOperation is a EfiCopyBlt this is the
315 source of the copy. For other BLT operations this
316 argument is not used.
317
318 @retval EFI_SUCCESS The palette is updated with PaletteArray.
319 @retval EFI_INVALID_PARAMETER BltOperation is not valid.
320 @retval EFI_DEVICE_ERROR A hardware error occured writting to the video
321 buffer.
322
323 **/
324 // TODO: SourceY - add argument and description to function comment
325 // TODO: DestinationX - add argument and description to function comment
326 // TODO: DestinationY - add argument and description to function comment
327 // TODO: Delta - add argument and description to function comment
328 EFI_STATUS
329 WinNtWndBlt (
330 IN EMU_GRAPHICS_WINDOW_PROTOCOL *GraphicsIo,
331 IN EFI_UGA_PIXEL *BltBuffer OPTIONAL,
332 IN EFI_UGA_BLT_OPERATION BltOperation,
333 IN EMU_GRAPHICS_WINDOWS__BLT_ARGS *Args
334 )
335 {
336 RETURN_STATUS RStatus;
337 GRAPHICS_PRIVATE_DATA *Private;
338 RECT Rect;
339
340 Private = GRAPHICS_PRIVATE_DATA_FROM_THIS (GraphicsIo);
341 RStatus = FrameBufferBlt (
342 Private->FrameBufferConfigure,
343 BltBuffer,
344 BltOperation,
345 Args->SourceX, Args->SourceY,
346 Args->DestinationX, Args->DestinationY,
347 Args->Width, Args->Height,
348 Args->Delta
349 );
350 if (RETURN_ERROR (RStatus)) {
351 return (EFI_STATUS)RStatus;
352 }
353
354 if (BltOperation != EfiBltVideoToBltBuffer) {
355 //
356 // Mark the area we just blted as Invalid so WM_PAINT will update.
357 //
358 Rect.left = (LONG)Args->DestinationX;
359 Rect.top = (LONG)Args->DestinationY;
360 Rect.right = (LONG)(Args->DestinationX + Args->Width);
361 Rect.bottom = (LONG)(Args->DestinationY + Args->Height);
362 InvalidateRect (Private->WindowHandle, &Rect, FALSE);
363
364 //
365 // Send the WM_PAINT message to the thread that is drawing the window. We
366 // are in the main thread and the window drawing is in a child thread.
367 // There is a child thread per window. We have no CriticalSection or Mutex
368 // since we write the data and the other thread displays the data. While
369 // we may miss some data for a short period of time this is no different than
370 // a write combining on writes to a frame buffer.
371 //
372
373 UpdateWindow (Private->WindowHandle);
374 }
375
376 return EFI_SUCCESS;
377 }
378
379
380
381 /**
382 Win32 Windows event handler.
383
384 See Win32 Book
385
386 @return See Win32 Book
387
388 **/
389 // TODO: hwnd - add argument and description to function comment
390 // TODO: iMsg - add argument and description to function comment
391 // TODO: wParam - add argument and description to function comment
392 // TODO: lParam - add argument and description to function comment
393 LRESULT
394 CALLBACK
395 WinNtGopThreadWindowProc (
396 IN HWND hwnd,
397 IN UINT iMsg,
398 IN WPARAM wParam,
399 IN LPARAM lParam
400 )
401 {
402 GRAPHICS_PRIVATE_DATA *Private;
403 HDC Handle;
404 PAINTSTRUCT PaintStruct;
405 LPARAM Index;
406 EFI_INPUT_KEY Key;
407 BOOLEAN AltIsPress;
408
409 //
410 // Use mTlsIndex global to get a Thread Local Storage version of Private.
411 // This works since each Gop protocol has a unique Private data instance and
412 // a unique thread.
413 //
414 AltIsPress = FALSE;
415 Private = TlsGetValue (mTlsIndex);
416 ASSERT (NULL != Private);
417
418 switch (iMsg) {
419 case WM_PAINT:
420 Handle = BeginPaint (hwnd, &PaintStruct);
421
422 SetDIBitsToDevice (
423 Handle, // Destination Device Context
424 0, // Destination X - 0
425 0, // Destination Y - 0
426 Private->Width, // Width
427 Private->Height, // Height
428 0, // Source X
429 0, // Source Y
430 0, // DIB Start Scan Line
431 Private->Height, // Number of scan lines
432 Private->VirtualScreenInfo + 1, // Address of array of DIB bits
433 (BITMAPINFO *) Private->VirtualScreenInfo, // Address of structure with bitmap info
434 DIB_RGB_COLORS // RGB or palette indexes
435 );
436
437 EndPaint (hwnd, &PaintStruct);
438 return 0;
439
440 //
441 // F10 and the ALT key do not create a WM_KEYDOWN message, thus this special case
442 // WM_SYSKEYDOWN is posted when F10 is pressed or
443 // holds down ALT key and then presses another key.
444 //
445 case WM_SYSKEYDOWN:
446
447 Key.ScanCode = 0;
448 Key.UnicodeChar = CHAR_NULL;
449 switch (wParam) {
450 case VK_F10:
451 Key.ScanCode = SCAN_F10;
452 Key.UnicodeChar = CHAR_NULL;
453 GopPrivateAddKey (Private, Key);
454 return 0;
455 }
456
457 //
458 // If ALT or ALT + modifier key is pressed.
459 //
460 if (WinNtGopConvertParamToEfiKey (Private, &wParam, &lParam, &Key)) {
461 if (Key.ScanCode != 0){
462 //
463 // If ALT is pressed with other ScanCode.
464 // Always revers the left Alt for simple.
465 //
466 Private->LeftAlt = TRUE;
467 }
468 GopPrivateAddKey (Private, Key);
469 //
470 // When Alt is released there is no windoes message, so
471 // clean it after using it.
472 //
473 Private->RightAlt = FALSE;
474 Private->LeftAlt = FALSE;
475 return 0;
476 }
477 AltIsPress = TRUE;
478
479 case WM_CHAR:
480 //
481 // The ESC key also generate WM_CHAR.
482 //
483 if (wParam == 0x1B) {
484 return 0;
485 }
486
487 if (AltIsPress == TRUE) {
488 //
489 // If AltIsPress is true that means the Alt key is pressed.
490 //
491 Private->LeftAlt = TRUE;
492 }
493 for (Index = 0; Index < (lParam & 0xffff); Index++) {
494 if (wParam != 0) {
495 Key.UnicodeChar = (CHAR16) wParam;
496 Key.ScanCode = SCAN_NULL;
497 GopPrivateAddKey (Private, Key);
498 }
499 }
500 if (AltIsPress == TRUE) {
501 //
502 // When Alt is released there is no windoes message, so
503 // clean it after using it.
504 //
505 Private->LeftAlt = FALSE;
506 Private->RightAlt = FALSE;
507 }
508 return 0;
509
510 case WM_SYSKEYUP:
511 //
512 // ALT is pressed with another key released
513 //
514 WinNtGopConvertParamToEfiKeyShiftState (Private, &wParam, &lParam, FALSE);
515 return 0;
516
517 case WM_KEYDOWN:
518 Key.ScanCode = SCAN_NULL;
519 Key.UnicodeChar = CHAR_NULL;
520 //
521 // A value key press will cause a WM_KEYDOWN first, then cause a WM_CHAR
522 // So if there is no modifier key updated, skip the WM_KEYDOWN even.
523 //
524 if (WinNtGopConvertParamToEfiKey (Private, &wParam, &lParam, &Key)) {
525 //
526 // Support the partial keystroke, add all keydown event into the queue.
527 //
528 GopPrivateAddKey (Private, Key);
529 }
530 return 0;
531
532 case WM_KEYUP:
533 WinNtGopConvertParamToEfiKeyShiftState (Private, &wParam, &lParam, FALSE);
534 return 0;
535
536 case WM_CLOSE:
537 //
538 // This close message is issued by user, core is not aware of this,
539 // so don't release the window display resource, just hide the window.
540 //
541 ShowWindow (Private->WindowHandle, SW_HIDE);
542 return 0;
543
544 case WM_DESTROY:
545 DestroyWindow (hwnd);
546 PostQuitMessage (0);
547
548 HeapFree (GetProcessHeap (), 0, Private->VirtualScreenInfo);
549
550 ExitThread (0);
551
552 default:
553 break;
554 };
555
556 return DefWindowProc (hwnd, iMsg, wParam, lParam);
557 }
558
559
560 /**
561 This thread simulates the end of WinMain () aplication. Each Winow nededs
562 to process it's events. The messages are dispatched to
563 WinNtGopThreadWindowProc ().
564 Be very careful sine WinNtGopThreadWinMain () and WinNtGopThreadWindowProc ()
565 are running in a seperate thread. We have to do this to process the events.
566
567 @param lpParameter Handle of window to manage.
568
569 @return if a WM_QUIT message is returned exit.
570
571 **/
572 DWORD
573 WINAPI
574 WinNtGopThreadWinMain (
575 LPVOID lpParameter
576 )
577 {
578 MSG Message;
579 GRAPHICS_PRIVATE_DATA *Private;
580 RECT Rect;
581
582 Private = (GRAPHICS_PRIVATE_DATA *) lpParameter;
583 ASSERT (NULL != Private);
584
585 //
586 // Since each thread has unique private data, save the private data in Thread
587 // Local Storage slot. Then the shared global mTlsIndex can be used to get
588 // thread specific context.
589 //
590 TlsSetValue (mTlsIndex, Private);
591
592 Private->ThreadId = GetCurrentThreadId ();
593
594 Private->WindowsClass.cbSize = sizeof (WNDCLASSEX);
595 Private->WindowsClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
596 Private->WindowsClass.lpfnWndProc = WinNtGopThreadWindowProc;
597 Private->WindowsClass.cbClsExtra = 0;
598 Private->WindowsClass.cbWndExtra = 0;
599 Private->WindowsClass.hInstance = NULL;
600 Private->WindowsClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
601 Private->WindowsClass.hCursor = LoadCursor (NULL, IDC_ARROW);
602 Private->WindowsClass.hbrBackground = (HBRUSH)(UINTN)COLOR_WINDOW;
603 Private->WindowsClass.lpszMenuName = NULL;
604 Private->WindowsClass.lpszClassName = WIN_NT_GOP_CLASS_NAME;
605 Private->WindowsClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
606
607 //
608 // Use 100 x 100 as initial Window size.
609 //
610 Private->Width = 100;
611 Private->Height = 100;
612
613
614 //
615 // This call will fail after the first time, but thats O.K. since we only need
616 // WIN_NT_GOP_CLASS_NAME to exist to create the window.
617 //
618 RegisterClassEx (&Private->WindowsClass);
619
620 //
621 // Setting Rect values to allow for the AdjustWindowRect to provide
622 // us the correct sizes for the client area when doing the CreateWindowEx
623 //
624 Rect.top = 0;
625 Rect.bottom = Private->Height;
626 Rect.left = 0;
627 Rect.right = Private->Width;
628
629 AdjustWindowRect (&Rect, WS_OVERLAPPEDWINDOW, 0);
630
631 Private->WindowHandle = CreateWindowEx (
632 0,
633 WIN_NT_GOP_CLASS_NAME,
634 Private->WindowName,
635 WS_OVERLAPPEDWINDOW,
636 CW_USEDEFAULT,
637 CW_USEDEFAULT,
638 Rect.right - Rect.left,
639 Rect.bottom - Rect.top,
640 NULL,
641 NULL,
642 NULL,
643 (VOID **)&Private
644 );
645
646 //
647 // The reset of this thread is the standard winows program. We need a sperate
648 // thread since we must process the message loop to make windows act like
649 // windows.
650 //
651
652 ShowWindow (Private->WindowHandle, SW_SHOW);
653 UpdateWindow (Private->WindowHandle);
654
655 //
656 // Let the main thread get some work done
657 //
658 ReleaseSemaphore (Private->ThreadInited, 1, NULL);
659
660 //
661 // This is the message loop that all Windows programs need.
662 //
663 while (GetMessage (&Message, Private->WindowHandle, 0, 0)) {
664 TranslateMessage (&Message);
665 DispatchMessage (&Message);
666 }
667
668 return (DWORD)Message.wParam;
669 }
670
671
672 /**
673 TODO: Add function description
674
675 @param Private TODO: add argument description
676 @param HorizontalResolution TODO: add argument description
677 @param VerticalResolution TODO: add argument description
678 @param ColorDepth TODO: add argument description
679 @param RefreshRate TODO: add argument description
680
681 @return TODO: add return values
682
683 **/
684 EFI_STATUS
685 EFIAPI
686 WinNtGraphicsWindowOpen (
687 IN EMU_IO_THUNK_PROTOCOL *This
688 )
689 {
690 DWORD NewThreadId;
691 GRAPHICS_PRIVATE_DATA *Private;
692
693 Private = AllocateZeroPool (sizeof (*Private));
694
695 GopPrivateCreateQ (Private, &Private->QueueForRead);
696
697 Private->GraphicsWindowIo.Size = WinNtWndSize;
698 Private->GraphicsWindowIo.CheckKey = WinNtWndCheckKey;
699 Private->GraphicsWindowIo.GetKey = WinNtWndGetKey;
700 Private->GraphicsWindowIo.KeySetState = WinNtWndKeySetState;
701 Private->GraphicsWindowIo.RegisterKeyNotify = WinNtWndRegisterKeyNotify;
702 Private->GraphicsWindowIo.Blt = WinNtWndBlt;
703 Private->GraphicsWindowIo.CheckPointer = WinNtWndCheckPointer;
704 Private->GraphicsWindowIo.GetPointerState = WinNtWndGetPointerState;
705
706 Private->WindowName = This->ConfigString;
707 //
708 // Initialize a Thread Local Storge variable slot. We use TLS to get the
709 // correct Private data instance into the windows thread.
710 //
711 if (mTlsIndex == TLS_OUT_OF_INDEXES) {
712 ASSERT (0 == mTlsIndexUseCount);
713 mTlsIndex = TlsAlloc ();
714 }
715
716 //
717 // always increase the use count!
718 //
719 mTlsIndexUseCount++;
720
721 Private->ThreadInited = CreateSemaphore (NULL, 0, 1, NULL);
722 Private->ThreadHandle = CreateThread (
723 NULL,
724 0,
725 WinNtGopThreadWinMain,
726 (VOID *) Private,
727 0,
728 &NewThreadId
729 );
730
731 //
732 // The other thread has entered the windows message loop so we can
733 // continue our initialization.
734 //
735 WaitForSingleObject (Private->ThreadInited, INFINITE);
736 CloseHandle (Private->ThreadInited);
737
738 This->Private = Private;
739 This->Interface = &Private->GraphicsWindowIo;
740
741 return EFI_SUCCESS;
742 }
743
744 EFI_STATUS
745 EFIAPI
746 WinNtGraphicsWindowClose (
747 IN EMU_IO_THUNK_PROTOCOL *This
748 )
749 {
750 GRAPHICS_PRIVATE_DATA *Private;
751
752 Private = (GRAPHICS_PRIVATE_DATA *)This->Private;
753
754 //
755 // BugBug: Shutdown GOP Hardware and any child devices.
756 //
757 SendMessage (Private->WindowHandle, WM_DESTROY, 0, 0);
758 CloseHandle (Private->ThreadHandle);
759
760 mTlsIndexUseCount--;
761
762 //
763 // The callback function for another window could still be called,
764 // so we need to make sure there are no more users of mTlsIndex.
765 //
766 if (0 == mTlsIndexUseCount) {
767 ASSERT (TLS_OUT_OF_INDEXES != mTlsIndex);
768
769 TlsFree (mTlsIndex);
770 mTlsIndex = TLS_OUT_OF_INDEXES;
771
772 UnregisterClass (
773 Private->WindowsClass.lpszClassName,
774 Private->WindowsClass.hInstance
775 );
776 }
777
778
779 GopPrivateDestroyQ (Private, &Private->QueueForRead);
780 return EFI_SUCCESS;
781 }
782
783
784 EMU_IO_THUNK_PROTOCOL mWinNtWndThunkIo = {
785 &gEmuGraphicsWindowProtocolGuid,
786 NULL,
787 NULL,
788 0,
789 WinNtGraphicsWindowOpen,
790 WinNtGraphicsWindowClose,
791 NULL
792 };