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
|
#include "brightness.h"
#include "detection/displayserver/displayserver.h"
#include "common/debug.h"
#include "common/library.h"
#include "common/mallocHelper.h"
#include "common/windows/wmi.h"
#include "common/windows/unicode.h"
#include <inttypes.h>
#include <winternl.h>
#include <windows.h>
NTSYSAPI NTSTATUS WINAPI GetPhysicalMonitors(
_In_ UNICODE_STRING* pstrDeviceName,
_In_ DWORD dwPhysicalMonitorArraySize,
_Out_ DWORD* pdwNumPhysicalMonitorHandlesInArray,
_Out_ HANDLE* phPhysicalMonitorArray);
typedef enum _MC_VCP_CODE_TYPE {
MC_MOMENTARY,
MC_SET_PARAMETER
} MC_VCP_CODE_TYPE,
*LPMC_VCP_CODE_TYPE;
NTSYSAPI NTSTATUS WINAPI DDCCIGetVCPFeature(
_In_ HANDLE hMonitor,
_In_ DWORD dwVCPCode,
_Out_opt_ LPMC_VCP_CODE_TYPE pvct,
_Out_ DWORD* pdwCurrentValue,
_Out_opt_ DWORD* pdwMaximumValue);
NTSYSAPI NTSTATUS WINAPI DestroyPhysicalMonitorInternal(
_In_ HANDLE hMonitor);
NTSTATUS WINAPI GetPhysicalMonitorDescription(
_In_ HANDLE hMonitor,
_In_ DWORD dwPhysicalMonitorDescriptionSizeInChars,
_Out_ LPWSTR szPhysicalMonitorDescription);
static const char* detectWithWmi(FFlist* result) {
FF_DEBUG("WMI: start detection");
// https://github.com/tpn/winsdk-10/blob/master/Include/10.0.16299.0/km/wmicore.mof#L21200
const GUID WmiMonitorBrightnessGuid = {
0xd43412ac, 0x67f9, 0x4fbb, { 0xa0, 0x81, 0x17, 0x52, 0xa2, 0xc3, 0x3e, 0x84 }
};
FF_AUTO_CLOSE_WMI_BLOCK HANDLE hBlock = NULL;
ULONG status = WmiOpenBlock(&WmiMonitorBrightnessGuid, WMIGUID_QUERY, &hBlock);
if (status != 0) {
FF_DEBUG("WMI: WmiOpenBlock failed: %s", ffDebugWin32Error(status));
return "WmiOpenBlock() failed";
}
ULONG bufferSize = 0;
status = WmiQueryAllDataW(hBlock, &bufferSize, NULL);
if (status != ERROR_SUCCESS && status != ERROR_INSUFFICIENT_BUFFER) {
FF_DEBUG("WMI: first WmiQueryAllDataW failed, bufferSize=%lu: %s", bufferSize, ffDebugWin32Error(status));
return "WmiQueryAllDataW() failed";
}
FF_DEBUG("WMI: initial query bufferSize=%lu", bufferSize);
if (bufferSize == 0) {
FF_DEBUG("WMI: WmiQueryAllDataW returned empty buffer");
return "WmiQueryAllDataW() returned no data";
}
FF_AUTO_FREE PWNODE_ALL_DATA pAllData = (PWNODE_ALL_DATA) malloc(bufferSize);
status = WmiQueryAllDataW(hBlock, &bufferSize, pAllData);
if (status != ERROR_SUCCESS) {
FF_DEBUG("WMI: second WmiQueryAllDataW failed, bufferSize=%lu: %s", bufferSize, ffDebugWin32Error(status));
return "WmiQueryAllDataW() failed";
}
if (bufferSize < sizeof(WNODE_ALL_DATA)) {
FF_DEBUG("WMI: insufficient buffer for WNODE_ALL_DATA, bufferSize=%lu", bufferSize);
return "WmiQueryAllDataW() returned insufficient data for WNODE_ALL_DATA";
}
FF_DEBUG("WMI: instanceCount=%lu, flags=0x%lX", pAllData->InstanceCount, pAllData->WnodeHeader.Flags);
PULONG pNameOffsets = (PULONG) ((PUCHAR) pAllData + pAllData->OffsetInstanceNameOffsets);
for (ULONG i = 0; i < pAllData->InstanceCount; i++) {
ULONG dataOffset = 0;
ULONG dataLength = 0;
if (pAllData->WnodeHeader.Flags & WNODE_FLAG_FIXED_INSTANCE_SIZE) {
dataLength = pAllData->FixedInstanceSize;
dataOffset = pAllData->DataBlockOffset + i * dataLength;
} else {
dataOffset = pAllData->OffsetInstanceDataAndLength[i].OffsetInstanceData;
dataLength = pAllData->OffsetInstanceDataAndLength[i].LengthInstanceData;
}
if (dataLength == 0 || dataOffset >= bufferSize || dataLength > bufferSize - dataOffset) {
FF_DEBUG("WMI: skip invalid instance %lu (dataOffset=%lu, dataLength=%lu, bufferSize=%lu)", i, dataOffset, dataLength, bufferSize);
continue;
}
USHORT nameCharsCount = *(PUSHORT) ((PUCHAR) pAllData + pNameOffsets[i]) / sizeof(WCHAR);
PCWSTR pNameChars = (PCWSTR) ((PUCHAR) pAllData + (pNameOffsets[i] + sizeof(USHORT)));
PUCHAR pDataBlock = (PUCHAR) pAllData + dataOffset;
UCHAR currentBrightness = pDataBlock[0];
FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result);
brightness->max = 100;
brightness->min = 0;
brightness->current = currentBrightness;
brightness->builtin = true;
ffStrbufInitNWS(&brightness->name, nameCharsCount, pNameChars);
ffStrbufSubstrAfterFirstC(&brightness->name, '\\');
ffStrbufSubstrBeforeFirstC(&brightness->name, '\\');
FF_DEBUG("WMI: detected builtin display '%s', current=%u", brightness->name.chars, (unsigned) currentBrightness);
}
FF_DEBUG("WMI: finished detection, total results=%u", result->length);
return NULL;
}
static const char* detectWithDdcci(const FFDisplayServerResult* displayServer, FFlist* result) {
FF_DEBUG("DDC/CI: start detection, displayCount=%u", displayServer->displays.length);
void* gdi32 = ffLibraryGetModule(L"gdi32.dll");
if (!gdi32) {
FF_DEBUG("DDC/CI: failed to load gdi32.dll");
return "ffLibraryGetModule(gdi32.dll) failed";
}
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(gdi32, GetPhysicalMonitors)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(gdi32, DDCCIGetVCPFeature)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(gdi32, DestroyPhysicalMonitorInternal)
FF_LIST_FOR_EACH (FFDisplayResult, display, displayServer->displays) {
if (display->type == FF_DISPLAY_TYPE_BUILTIN) {
FF_DEBUG("DDC/CI: skip builtin display id=%" PRIu64, display->id);
continue;
}
MONITORINFOEXW mi;
mi.cbSize = sizeof(mi);
if (!GetMonitorInfoW((HMONITOR) (uintptr_t) display->id, (LPMONITORINFO) &mi)) {
FF_DEBUG("DDC/CI: GetMonitorInfoW failed for display id=%" PRIu64 ": %s", display->id, ffDebugWin32Error(GetLastError()));
continue;
}
UNICODE_STRING deviceName = {
.Length = (USHORT) (wcslen(mi.szDevice) * sizeof(wchar_t)),
.MaximumLength = 0,
.Buffer = mi.szDevice,
};
HANDLE physicalMonitor;
DWORD monitorCount = 0;
NTSTATUS monitorStatus = ffGetPhysicalMonitors(&deviceName, 1, &monitorCount, &physicalMonitor);
if (NT_SUCCESS(monitorStatus) && monitorCount >= 1) {
DWORD curr = 0, max = 0;
if (NT_SUCCESS(ffDDCCIGetVCPFeature(physicalMonitor, FF_DDC_CI_LUMINANCE_OPCODE, NULL, &curr, &max))) {
FFBrightnessResult* brightness = FF_LIST_ADD(FFBrightnessResult, *result);
if (display->name.length > 0) {
ffStrbufInitCopy(&brightness->name, &display->name);
} else {
FF_LIBRARY_LOAD_SYMBOL_LAZY(gdi32, GetPhysicalMonitorDescription)
if (ffGetPhysicalMonitorDescription) {
wchar_t description[128 /*MUST be PHYSICAL_MONITOR_DESCRIPTION_SIZE*/];
if (NT_SUCCESS(ffGetPhysicalMonitorDescription(physicalMonitor, ARRAY_SIZE(description), description))) {
ffStrbufInitWS(&brightness->name, description);
}
}
if (brightness->name.length == 0) {
ffStrbufSetNWS(&brightness->name, deviceName.Length / 2, deviceName.Buffer);
}
}
brightness->max = max;
brightness->min = 0;
brightness->current = curr;
brightness->builtin = false;
FF_DEBUG("DDC/CI: detected external display '%s', current=%u, max=%u", brightness->name.chars, (unsigned) curr, (unsigned) max);
} else {
FF_DEBUG("DDC/CI: DDCCIGetVCPFeature failed for monitor '%ls': %s", deviceName.Buffer, ffDebugWin32Error(GetLastError()));
}
ffDestroyPhysicalMonitorInternal(physicalMonitor);
} else {
FF_DEBUG("DDC/CI: GetPhysicalMonitors failed for '%ls', status=0x%08X, monitorCount=%lu, error=%s", deviceName.Buffer, (unsigned) monitorStatus, monitorCount, ffDebugWin32Error(GetLastError()));
}
}
FF_DEBUG("DDC/CI: finished detection, total results=%u", result->length);
return NULL;
}
static bool hasBuiltinDisplay(const FFDisplayServerResult* displayServer) {
FF_LIST_FOR_EACH (FFDisplayResult, display, displayServer->displays) {
if (display->type == FF_DISPLAY_TYPE_BUILTIN || display->type == FF_DISPLAY_TYPE_UNKNOWN) {
return true;
}
}
return false;
}
const char* ffDetectBrightness(FF_A_UNUSED FFBrightnessOptions* options, FFlist* result) {
const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
FF_DEBUG("start, displayCount=%u", displayServer->displays.length);
if (hasBuiltinDisplay(displayServer)) {
FF_DEBUG("builtin display detected, trying WMI");
detectWithWmi(result);
}
if (options->ddcciSleep != FF_BRIGHTNESS_DDCCI_SLEEP_SKIP && result->length < displayServer->displays.length) {
FF_DEBUG("resultCount=%u < displayCount=%u, trying DDC/CI", result->length, displayServer->displays.length);
detectWithDdcci(displayServer, result);
}
FF_DEBUG("finished, resultCount=%u", result->length);
return NULL;
}
|