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