|
17 | 17 | #include "RefreshWaiter.h" |
18 | 18 |
|
19 | 19 |
|
20 | | -RefreshWaiter::RefreshWaiter() |
| 20 | +namespace { |
| 21 | + |
| 22 | + struct FindGuidContext |
| 23 | + { |
| 24 | + const std::string* monitorName; |
| 25 | + GUID guid; |
| 26 | + bool found; |
| 27 | + }; |
| 28 | + |
| 29 | + // https://learn.microsoft.com/en-us/windows/win32/api/ddraw/nc-ddraw-lpddenumcallbackexa |
| 30 | + BOOL WINAPI FindGuidForMonitor(GUID* pGUID, LPSTR, |
| 31 | + LPSTR, LPVOID pContext, HMONITOR mointorHandle) |
| 32 | + { |
| 33 | + FindGuidContext* ctx = static_cast<FindGuidContext*>(pContext); |
| 34 | + if (pGUID == 0 || mointorHandle == 0) |
| 35 | + return TRUE; |
| 36 | + |
| 37 | + MONITORINFOEXA info; |
| 38 | + info.cbSize = sizeof(info); |
| 39 | + if (GetMonitorInfoA(mointorHandle, &info) && *ctx->monitorName == info.szDevice) |
| 40 | + { |
| 41 | + ctx->guid = *pGUID; |
| 42 | + ctx->found = true; |
| 43 | + return FALSE; |
| 44 | + } |
| 45 | + return TRUE; |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +RefreshWaiter::RefreshWaiter() : |
| 52 | + hDDrawLib_(0), |
| 53 | + directDrawCreate_(0), |
| 54 | + directDrawEnumerateEx_(0), |
| 55 | + directDraw_(0) |
21 | 56 | { |
22 | | - hDDrawLib_ = LoadLibraryA("ddraw.dll"); |
23 | | - if (!hDDrawLib_) |
24 | | - return; |
25 | | - |
26 | | - directDrawCreate_ = (DirectDrawCreateFunc)GetProcAddress(hDDrawLib_, |
27 | | - "DirectDrawCreate"); |
28 | | - if (!directDrawCreate_) |
29 | | - return; |
30 | | - |
31 | | - HRESULT hr = directDrawCreate_(0, &directDraw_, 0); |
32 | | - if (hr != DD_OK) |
33 | | - { |
34 | | - directDraw_ = 0; |
35 | | - return; |
36 | | - } |
| 57 | + hDDrawLib_ = LoadLibraryA("ddraw.dll"); |
| 58 | + if (!hDDrawLib_) |
| 59 | + return; |
| 60 | + |
| 61 | + directDrawCreate_ = (DirectDrawCreateFunc)GetProcAddress(hDDrawLib_, |
| 62 | + "DirectDrawCreate"); |
| 63 | + directDrawEnumerateEx_ = (DirectDrawEnumerateExFunc)GetProcAddress(hDDrawLib_, |
| 64 | + "DirectDrawEnumerateExA"); |
| 65 | + if (!directDrawCreate_) |
| 66 | + return; |
| 67 | + |
| 68 | + HRESULT hr = directDrawCreate_(0, &directDraw_, 0); |
| 69 | + if (hr != DD_OK) |
| 70 | + { |
| 71 | + directDraw_ = 0; |
| 72 | + return; |
| 73 | + } |
37 | 74 | } |
38 | 75 |
|
39 | 76 |
|
40 | 77 | RefreshWaiter::~RefreshWaiter() |
41 | 78 | { |
42 | | - if (directDraw_) |
43 | | - directDraw_->Release(); |
| 79 | + if (directDraw_) |
| 80 | + directDraw_->Release(); |
| 81 | + |
| 82 | + if (hDDrawLib_) |
| 83 | + FreeLibrary(hDDrawLib_); |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +int RefreshWaiter::SetMonitor(const std::string& monitorName) |
| 88 | +{ |
| 89 | + if (!directDrawCreate_) |
| 90 | + return 1; |
| 91 | + |
| 92 | + GUID* pGuid = 0; |
| 93 | + FindGuidContext ctx; |
| 94 | + ctx.monitorName = &monitorName; |
| 95 | + ctx.found = false; |
| 96 | + |
| 97 | + if (!monitorName.empty()) |
| 98 | + { |
| 99 | + if (!directDrawEnumerateEx_) |
| 100 | + return 1; |
| 101 | + |
| 102 | + directDrawEnumerateEx_(FindGuidForMonitor, &ctx, DDENUM_ATTACHEDSECONDARYDEVICES); |
| 103 | + if (!ctx.found) |
| 104 | + return 1; |
| 105 | + |
| 106 | + pGuid = &ctx.guid; |
| 107 | + } |
| 108 | + |
| 109 | + IDirectDraw* newDirectDraw = 0; |
| 110 | + HRESULT hr = directDrawCreate_(pGuid, &newDirectDraw, 0); |
| 111 | + if (hr != DD_OK) |
| 112 | + return (int)hr; |
44 | 113 |
|
45 | | - if (hDDrawLib_) |
46 | | - FreeLibrary(hDDrawLib_); |
| 114 | + if (directDraw_) |
| 115 | + directDraw_->Release(); |
| 116 | + directDraw_ = newDirectDraw; |
| 117 | + return 0; |
47 | 118 | } |
48 | 119 |
|
49 | 120 |
|
50 | 121 | int RefreshWaiter::WaitForVerticalBlank() |
51 | 122 | { |
52 | | - if (!directDraw_) |
53 | | - return 1; |
| 123 | + if (!directDraw_) |
| 124 | + return 1; |
54 | 125 |
|
55 | | - HRESULT result = directDraw_->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); |
| 126 | + HRESULT result = directDraw_->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, 0); |
56 | 127 |
|
57 | | - if (result == DD_OK) |
58 | | - { |
59 | | - return 0; |
60 | | - } |
61 | | - return (int) result; |
| 128 | + if (result == DD_OK) |
| 129 | + { |
| 130 | + return 0; |
| 131 | + } |
| 132 | + return (int) result; |
62 | 133 |
|
63 | 134 | } |
0 commit comments