-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
126 lines (106 loc) · 4.21 KB
/
main.cpp
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
#pragma comment(lib, "Dxva2.lib")
#include <iostream>
#include <windows.h>
#include <vector>
#include <sstream>
#include <physicalmonitorenumerationapi.h>
#include <lowlevelmonitorconfigurationapi.h>
using namespace std;
struct MonitorHandles {
vector<HMONITOR> hvMonitors;
/**
* Callback for existing physical monitors enumerating.
*
* @param hMon Monitor handle.
* @param hdc Not used.
* @param lprcMonitor Not used.
* @param pData Additional data to push.
* @return True.
*/
static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData) {
auto *pThis = reinterpret_cast<MonitorHandles *>(pData);
pThis->hvMonitors.push_back(hMon);
return TRUE;
}
/**
* Iterates through existing physical monitors.
*
* @return BOOL EnumDisplayMonitors(...) result.
*/
BOOL iterate() {
return EnumDisplayMonitors(nullptr, nullptr, (MONITORENUMPROC) MonitorEnum, (LPARAM) this);
}
};
/**
* Prints existing display devices.
*/
static void ListDisplayDevices() {
DWORD dwCurDeviceNumber = 0;
DISPLAY_DEVICE displayDevice = {sizeof(DISPLAY_DEVICE)};
while (EnumDisplayDevices(nullptr, dwCurDeviceNumber, &displayDevice, 0)) {
DWORD dwCurMonitorNumber = 0;
DISPLAY_DEVICE newDisplayDevice = {sizeof(DISPLAY_DEVICE)};
while (EnumDisplayDevices(displayDevice.DeviceName, dwCurMonitorNumber, &newDisplayDevice, 0)) {
std::cout << dwCurDeviceNumber << " - " << newDisplayDevice.DeviceID << std::endl;
dwCurMonitorNumber++;
}
dwCurDeviceNumber++;
}
}
/**
* Parses target @pcTargetPort port to DWORD.
*
* @param pcTargetPort
* @return DWORD representation of @pcTargetPort.
*/
static DWORD ParseTargetPort(char *pcTargetPort) {
unsigned int temp;
stringstream ss;
ss << std::hex << pcTargetPort;
ss >> temp;
return static_cast<DWORD>(temp);
}
int main(int argc, char *argv[]) {
if (argc == 2 && strcmp(argv[1], "--list") == 0) {
ListDisplayDevices();
return 0;
}
if (argc != 5) {
std::cout << "program usage: switch[.exe] --monitor <ID> --code <CODE>" << std::endl;
std::cout << " or: switch[.exe] --list (displays all monitors whose can be set)" << std::endl;
std::cout << " or: switch[.exe] --help" << std::endl;
std::cout << " <ID> - is a monitor code that can be retrieved from `switch[.exe] --list` command;" << std::endl;
std::cout << " <CODE> - is a hex code of your monitor input. table of possible values is given below:" << std::endl;
std::cout << " 01 - D-SUB/VGA;" << std::endl;
std::cout << " 03 - DVI;" << std::endl;
std::cout << " 04 - HDMI;" << std::endl;
std::cout << " 11 - HDMI;" << std::endl;
std::cout << " 0F - DISPLAY PORT." << std::endl;
std::cout << " the possible value is in range from 0 to 12 (in hex). if you don't know the desired value, you can always enumerate through all." << std::endl;
return 1;
}
int iTargetMonitorIdx = strtol(argv[2], nullptr, 0);
DWORD dwTargetPort = ParseTargetPort(argv[4]);
auto monitorHandles = MonitorHandles();
if (!monitorHandles.iterate()) {
std::cout << "cannot enumerate active displays. exiting..." << std::endl;
return 1;
}
auto hvMonitor = monitorHandles.hvMonitors[iTargetMonitorIdx];
DWORD numberOfMonitors;
GetNumberOfPhysicalMonitorsFromHMONITOR(hvMonitor, &numberOfMonitors);
if (GetLastError() != 0) {
std::cout << "error while getting number of physical monitors. exiting..." << std::endl;
return 1;
}
auto pPhysicalMonitors = (LPPHYSICAL_MONITOR) malloc(numberOfMonitors * sizeof(PHYSICAL_MONITOR));
if (GetPhysicalMonitorsFromHMONITOR(hvMonitor, numberOfMonitors, pPhysicalMonitors)) {
for (auto i = 0; i < numberOfMonitors; ++i) {
// 0x0F - DP, 0x11 - HDMI
SetVCPFeature(pPhysicalMonitors[i].hPhysicalMonitor, (BYTE) 0x60 /* vcp input feature code */,
dwTargetPort);
DestroyPhysicalMonitor(pPhysicalMonitors[i].hPhysicalMonitor);
}
}
return 0;
}