-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMonitor.cpp
More file actions
176 lines (150 loc) · 5.9 KB
/
Monitor.cpp
File metadata and controls
176 lines (150 loc) · 5.9 KB
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
#include "Monitor.hpp"
void ListDisplaySettings(int index, DWORD flags)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
if(EnumDisplayDevices(NULL, index, &dd, 0))
{
DISPLAY_DEVICE monitor;
monitor.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(dd.DeviceName, index, &monitor, 0);
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(dd.DeviceName, flags, &dm);
printf("Device name: %s\n", dd.DeviceName);
printf("Monitor name: %s\n", monitor.DeviceID);
printf("Refresh rate, in hertz: %lu\n", dm.dmDisplayFrequency);
printf("Color depth: %lu\n", dm.dmBitsPerPel);
printf("Location: %lu, %lu\n", dm.dmPosition.x, dm.dmPosition.y);
printf("Screen resolution, in pixels: %lu x %lu\n",
dm.dmPelsWidth, dm.dmPelsHeight);
printf("Is Primary: %s\n", dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE ? "True" : "False");
printf("Is Active: %s\n", dd.StateFlags & DISPLAY_DEVICE_ACTIVE ? "True" : "False");
printf("Is Attached: %s\n", dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP ? "True" : "False");
printf("Is Mirroring: %s\n\n\n", dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER ? "True" : "False");
++index;
}
}
void EnumerateDisplays(void(*onDisplay)(DISPLAY_DEVICE* dsp_device, DEVMODE* default_mode))
{
DWORD dsp_num = 0;
DISPLAY_DEVICE dsp_device = {0};
dsp_device.cb = sizeof(DISPLAY_DEVICE);
while (EnumDisplayDevices(NULL, dsp_num, &dsp_device, 0))
{
DEVMODE default_mode = {0};
default_mode.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(dsp_device.DeviceName, ENUM_REGISTRY_SETTINGS, &default_mode))
{
std::printf("Failed to Enumerate Display Settings for: %s -- %lu\n", dsp_device.DeviceName, GetLastError());
}
onDisplay(&dsp_device, &default_mode);
memset(&dsp_device, 0, sizeof(dsp_device));
dsp_device.cb = sizeof(dsp_device);
++dsp_num;
}
}
BOOL CALLBACK MonitorProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
MONITORINFOEX info;
memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
GetMonitorInfo(hMonitor, &info);
bool (*onMonitor)(HMONITOR, MONITORINFOEX*) = reinterpret_cast<bool (*)(HMONITOR hMonitor, MONITORINFOEX* info)>(dwData);
return onMonitor(hMonitor, &info);
}
void EnumerateMonitors(bool (*onMonitor)(HMONITOR hMonitor, MONITORINFOEX* info))
{
EnumDisplayMonitors(NULL, NULL, &MonitorProc, reinterpret_cast<LPARAM>(onMonitor));
}
std::string MonitorName(unsigned char* edid_data)
{
static unsigned char name[] = {0x00, 0x00, 0x00, 0xFC, 0x00};
static unsigned char header[] = {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00};
auto find_info = [&](int s, int e, unsigned char* data, int len) -> void*
{
auto it = std::search(&edid_data[s], &edid_data[e], &data[0], &data[len]);
if (it != &edid_data[e])
return it;
return nullptr;
};
if (!find_info(0, sizeof(header), &header[0], sizeof(header)))
return std::string();
void* o = find_info(54, 125, &name[0], sizeof(name));
if (o)
{
std::string str = std::string((char*)o + sizeof(name), 12);
str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return str;
}
return std::string();
}
MonitorDisplay::MonitorDisplay() : SetDisplayConfig(NULL)
{
HMODULE mod = GetModuleHandle("User32.dll");
SetDisplayConfig = reinterpret_cast<decltype(SetDisplayConfig)>(GetProcAddress(mod, "SetDisplayConfig"));
}
std::string MonitorDisplay::GetDisplayInfo(DEV_BROADCAST_DEVICEINTERFACE* dev_interface)
{
std::string info(dev_interface->dbcc_name);
std::string::size_type first = info.find("\\\\?\\");
std::string::size_type last = info.find_last_of('#');
if (first != std::string::npos && last != std::string::npos)
{
info.erase(last);
info.erase(first, 4);
std::replace(info.begin(), info.end(), '#', '\\');
return info;
}
return std::string();
}
std::string MonitorDisplay::ReadRegistryKey(HKEY Root, std::string SubRoot, std::string KeyToRead)
{
HKEY hKey;
if (RegOpenKeyEx(Root, SubRoot.c_str(), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD dwSize = 0;
DWORD KeyType = REG_BINARY;
RegQueryValueEx(hKey, KeyToRead.c_str(), NULL, &KeyType, NULL, &dwSize);
std::string Result = std::string(dwSize, '\0');
if (RegQueryValueEx(hKey, KeyToRead.c_str(), NULL, &KeyType, reinterpret_cast<std::uint8_t*>(&Result[0]), &dwSize) == ERROR_SUCCESS)
{
Result.resize(dwSize);
RegCloseKey(hKey);
return Result;
}
RegCloseKey(hKey);
}
return std::string();
}
void MonitorDisplay::Extend()
{
SetDisplayConfig(0, NULL, 0, NULL, SDC_APPLY | SDC_TOPOLOGY_EXTEND);
}
void MonitorDisplay::Clone()
{
SetDisplayConfig(0, NULL, 0, NULL, SDC_APPLY | SDC_TOPOLOGY_CLONE);
}
void MonitorDisplay::Internal()
{
SetDisplayConfig(0, NULL, 0, NULL, SDC_APPLY | SDC_TOPOLOGY_INTERNAL);
}
void MonitorDisplay::External()
{
SetDisplayConfig(0, NULL, 0, NULL, SDC_APPLY | SDC_TOPOLOGY_EXTERNAL);
}
void MonitorDisplay::TurnOn()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
}
void MonitorDisplay::TurnOff()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}
std::string MonitorDisplay::Name(DEV_BROADCAST_DEVICEINTERFACE* dev_interface)
{
std::string info = GetDisplayInfo(dev_interface);
info = ReadRegistryKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Enum\\" + info + "\\Device Parameters", "EDID");
return MonitorName(reinterpret_cast<unsigned char*>(&info[0]));
}