|
1 | | -#include "HDRController.h" |
| 1 | +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers |
| 2 | + |
| 3 | +#include <windows.h> |
| 4 | + |
2 | 5 |
|
| 6 | +#include "HDRController.h" |
3 | 7 | #include <iostream> |
4 | | -#include "toggle.h" |
5 | 8 | #include "WinUser.h" |
6 | 9 | #include <stdio.h> |
| 10 | +#include <winerror.h> |
| 11 | +#include <wingdi.h> |
7 | 12 |
|
8 | 13 |
|
9 | 14 |
|
10 | | -using namespace core; |
| 15 | +#include <stdint.h> |
| 16 | +#include <cstdlib> |
| 17 | +#include <cstring> |
| 18 | +#include <conio.h> |
11 | 19 |
|
12 | | -static std::unique_ptr<Toggle> hdrToggle; |
13 | 20 |
|
| 21 | +using namespace core; |
14 | 22 |
|
15 | 23 | static void showError(std::string msg) |
16 | 24 | { |
17 | 25 | LPCWSTR lmsg = (LPCWSTR)msg.c_str(); |
18 | 26 |
|
19 | | - MessageBox(NULL, lmsg, L"HDR Switch Error", MB_OK | MB_ICONWARNING); |
| 27 | + MessageBox(NULL, lmsg, L"HDRController Error", MB_OK | MB_ICONWARNING); |
20 | 28 | } |
21 | 29 |
|
22 | | -static void setHdrMode(bool enabled) |
| 30 | + |
| 31 | +static void SetHDR(bool enabled) |
23 | 32 | { |
24 | | - auto status = hdrToggle->setHdrMode(enabled); |
25 | | - if (!status.IsSuccessful) |
| 33 | + uint32_t pathCount, modeCount; |
| 34 | + |
| 35 | + uint8_t set[] = { 0x0A, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x14, 0x81, 0x00, 0x00, |
| 36 | + 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }; |
| 37 | + |
| 38 | + uint8_t request[] = { 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x7C, 0x6F, 0x00, |
| 39 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0xDB, 0x00, |
| 40 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00 }; |
| 41 | + |
| 42 | + if (ERROR_SUCCESS == GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount)) |
26 | 43 | { |
27 | | - auto msg = "Error setting hdr mode:" + status.Message; |
28 | | - showError(msg); |
| 44 | + DISPLAYCONFIG_PATH_INFO* pathsArray = nullptr; |
| 45 | + DISPLAYCONFIG_MODE_INFO* modesArray = nullptr; |
| 46 | + |
| 47 | + const size_t sizePathsArray = pathCount * sizeof(DISPLAYCONFIG_PATH_INFO); |
| 48 | + const size_t sizeModesArray = modeCount * sizeof(DISPLAYCONFIG_MODE_INFO); |
| 49 | + |
| 50 | + pathsArray = static_cast<DISPLAYCONFIG_PATH_INFO*>(std::malloc(sizePathsArray)); |
| 51 | + modesArray = static_cast<DISPLAYCONFIG_MODE_INFO*>(std::malloc(sizeModesArray)); |
| 52 | + |
| 53 | + if (pathsArray != nullptr && modesArray != nullptr) |
| 54 | + { |
| 55 | + std::memset(pathsArray, 0, sizePathsArray); |
| 56 | + std::memset(modesArray, 0, sizeModesArray); |
| 57 | + |
| 58 | + if (ERROR_SUCCESS == QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathsArray, |
| 59 | + &modeCount, modesArray, 0)) |
| 60 | + { |
| 61 | + DISPLAYCONFIG_DEVICE_INFO_HEADER* setPacket = |
| 62 | + reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(set); |
| 63 | + DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket = |
| 64 | + reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(request); |
| 65 | + |
| 66 | + for (int i = 0; i < modeCount; i++) |
| 67 | + { |
| 68 | + if (modesArray[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET) |
| 69 | + { |
| 70 | + setPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart; |
| 71 | + setPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart; |
| 72 | + setPacket->id = modesArray[i].id; |
| 73 | + |
| 74 | + requestPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart; |
| 75 | + requestPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart; |
| 76 | + requestPacket->id = modesArray[i].id; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (ERROR_SUCCESS == DisplayConfigGetDeviceInfo(requestPacket)) |
| 81 | + { |
| 82 | + if (enabled == true) |
| 83 | + { |
| 84 | + set[20] = 1; |
| 85 | + DisplayConfigSetDeviceInfo(setPacket); |
| 86 | + } |
| 87 | + else if (enabled == false) |
| 88 | + { |
| 89 | + set[20] = 0; |
| 90 | + DisplayConfigSetDeviceInfo(setPacket); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + std::free(pathsArray); |
| 95 | + std::free(modesArray); |
| 96 | + } |
29 | 97 | } |
30 | 98 | } |
31 | 99 |
|
32 | | -extern "C" |
| 100 | +static bool HDRIsOn() |
33 | 101 | { |
34 | | - __declspec(dllexport) void SetHDR(bool enabled) |
| 102 | + bool returnValue = false; |
| 103 | + |
| 104 | + uint32_t pathCount, modeCount; |
| 105 | + |
| 106 | + uint8_t set[] = { 0x0A, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x14, 0x81, 0x00, 0x00, |
| 107 | + 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }; |
| 108 | + |
| 109 | + uint8_t request[] = { 0x09, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x7C, 0x6F, 0x00, |
| 110 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0xDB, 0x00, |
| 111 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00 }; |
| 112 | + |
| 113 | + if (ERROR_SUCCESS == GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &pathCount, &modeCount)) |
35 | 114 | { |
36 | | - setHdrMode(enabled); |
| 115 | + DISPLAYCONFIG_PATH_INFO* pathsArray = nullptr; |
| 116 | + DISPLAYCONFIG_MODE_INFO* modesArray = nullptr; |
| 117 | + |
| 118 | + const size_t sizePathsArray = pathCount * sizeof(DISPLAYCONFIG_PATH_INFO); |
| 119 | + const size_t sizeModesArray = modeCount * sizeof(DISPLAYCONFIG_MODE_INFO); |
| 120 | + |
| 121 | + pathsArray = static_cast<DISPLAYCONFIG_PATH_INFO*>(std::malloc(sizePathsArray)); |
| 122 | + modesArray = static_cast<DISPLAYCONFIG_MODE_INFO*>(std::malloc(sizeModesArray)); |
| 123 | + |
| 124 | + if (pathsArray != nullptr && modesArray != nullptr) |
| 125 | + { |
| 126 | + std::memset(pathsArray, 0, sizePathsArray); |
| 127 | + std::memset(modesArray, 0, sizeModesArray); |
| 128 | + |
| 129 | + if (ERROR_SUCCESS == QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &pathCount, pathsArray, |
| 130 | + &modeCount, modesArray, 0)) |
| 131 | + { |
| 132 | + DISPLAYCONFIG_DEVICE_INFO_HEADER* setPacket = |
| 133 | + reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(set); |
| 134 | + DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket = |
| 135 | + reinterpret_cast<DISPLAYCONFIG_DEVICE_INFO_HEADER*>(request); |
| 136 | + |
| 137 | + for (int i = 0; i < modeCount; i++) |
| 138 | + { |
| 139 | + if (modesArray[i].infoType == DISPLAYCONFIG_MODE_INFO_TYPE_TARGET) |
| 140 | + { |
| 141 | + setPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart; |
| 142 | + setPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart; |
| 143 | + setPacket->id = modesArray[i].id; |
| 144 | + |
| 145 | + requestPacket->adapterId.HighPart = modesArray[i].adapterId.HighPart; |
| 146 | + requestPacket->adapterId.LowPart = modesArray[i].adapterId.LowPart; |
| 147 | + requestPacket->id = modesArray[i].id; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if (ERROR_SUCCESS == DisplayConfigGetDeviceInfo(requestPacket)) |
| 152 | + { |
| 153 | + if (request[20] == 0xD1) // HDR is OFF |
| 154 | + { |
| 155 | + returnValue = false; |
| 156 | + } |
| 157 | + else if (request[20] == 0xD3) // HDR is ON |
| 158 | + { |
| 159 | + returnValue = true; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + std::free(pathsArray); |
| 164 | + std::free(modesArray); |
| 165 | + return returnValue; |
| 166 | + } |
37 | 167 | } |
38 | 168 | } |
39 | 169 |
|
| 170 | +extern "C" |
| 171 | +{ |
| 172 | + __declspec(dllexport) void SetHDRState(bool enabled) |
| 173 | + { |
| 174 | + SetHDR(enabled); |
| 175 | + } |
| 176 | + |
| 177 | + __declspec(dllexport) bool GetHDRState() |
| 178 | + { |
| 179 | + return HDRIsOn(); |
| 180 | + } |
| 181 | +} |
0 commit comments