-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win32.lua
201 lines (157 loc) · 4.65 KB
/
Win32.lua
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
local ffi = require "ffi"
local C = ffi.C
local bit = require "bit"
local band = bit.band
require "Win32Types"
require "win_user32"
ffi.cdef[[
// These are used to indicate the DeviceState flag from the
// EnumDisplayDevices call
enum {
DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x00000001,
DISPLAY_DEVICE_MULTI_DRIVER = 0x00000002,
DISPLAY_DEVICE_PRIMARY_DEVICE = 0x00000004,
DISPLAY_DEVICE_MIRRORING_DRIVER = 0x00000008,
DISPLAY_DEVICE_VGA_COMPATIBLE = 0x00000010,
DISPLAY_DEVICE_REMOVABLE = 0x00000020,
DISPLAY_DEVICE_TS_COMPATIBLE = 0x00200000,
DISPLAY_DEVICE_DISCONNECT = 0x02000000,
DISPLAY_DEVICE_REMOTE = 0x04000000,
DISPLAY_DEVICE_MODESPRUNED = 0x08000000,
};
]]
ffi.cdef[[
typedef struct tagMONITORINFOA {
DWORD cbSize;
RECT rcMonitor;
RECT rcWork;
DWORD dwFlags;
} MONITORINFOA, *LPMONITORINFOA;
enum {
CCHDEVICENAME = 32
};
typedef struct tagMONITORINFOEXA {
DWORD cbSize;
RECT rcMonitor;
RECT rcWork;
DWORD dwFlags;
TCHAR szDevice[CCHDEVICENAME];
} MONITORINFOEXA, *LPMONITORINFOEXA;
typedef struct {
DWORD cb;
TCHAR DeviceName[32];
TCHAR DeviceString[128];
DWORD StateFlags;
TCHAR DeviceID[128];
TCHAR DeviceKey[128];
} DISPLAY_DEVICEA, *PDISPLAY_DEVICEA;
]]
ffi.cdef[[
typedef BOOL (*MONITORENUMPROC)(HMONITOR hMonitor, HDC hdcMonitor, RECT * lprcMonitor, LPARAM dwData);
BOOL EnumDisplayMonitors(HDC hdc, const RECT * lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData);
BOOL GetMonitorInfoA(HMONITOR hMonitor, MONITORINFOEXA * lpmi);
HMONITOR MonitorFromPoint(POINT pt, DWORD dwFlags);
HMONITOR MonitorFromRect(const RECT * lprc, DWORD dwFlags);
HMONITOR MonitorFromWindow( HWND hwnd, DWORD dwFlags);
BOOL EnumDisplayDevicesA(LPCTSTR lpDevice, DWORD iDevNum, PDISPLAY_DEVICEA lpDisplayDevice, DWORD dwFlags);
]]
MonitorInfo = nil
MonitorInfo_mt = {
__tostring = function(self)
local str = string.format("%s\n%s", ffi.string(self.szDevice), tostring(self.rcMonitor))
return str
end,
__index = {
Init = function(self)
cbSize = ffi.sizeof("MONITORINFOEXA")
end,
}
}
MonitorInfo = ffi.metatype("MONITORINFOEXA", MonitorInfo_mt)
DisplayDevice = nil
DisplayDevice_mt = {
__tostring = function(self)
local str = string.format("Adaptor: %s\nName: %s\nID: %s\nKey: %s",
ffi.string(self.DeviceName),
ffi.string(self.DeviceString),
ffi.string(self.DeviceID),
ffi.string(self.DeviceKey))
return str
end,
__index = {
Init = function(self)
cb = ffi.sizeof("DISPLAY_DEVICEA")
end,
GetName = function(self)
return ffi.string(self.DeviceName)
end,
GetDescription = function(self)
return ffi.string(self.DeviceString)
end,
IsDesktop = function(self)
local result = band(self.StateFlags, C.DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)>0
return result
end,
IsPrimary = function(self)
local result = band(self.StateFlags, C.DISPLAY_DEVICE_PRIMARY_DEVICE)>0
return result
end,
}
}
DisplayDevice = ffi.metatype("DISPLAY_DEVICEA", DisplayDevice_mt)
function GetPhysicalMonitors()
local monitors = {}
function enummonitor(hMonitor, hdcMonitor, lprcMonitor, dwData)
-- Allocate a structure to hold the monitor info
local monitor = ffi.new("MONITORINFOEXA[1]")
monitor[0].cbSize = ffi.sizeof("MONITORINFOEXA")
-- Get the monitor structure
if C.GetMonitorInfoA(hMonitor, monitor) > 0 then
monitor = monitor[0]
table.insert(monitors, monitor)
end
return 1
end
C.EnumDisplayMonitors(nil, nil, enummonitor, 0)
return monitors
end
function GetMonitorForDisplayDevice(adaptor)
local nextdevice = ffi.new("DISPLAY_DEVICEA[1]")
nextdevice[0].cb = ffi.sizeof("DISPLAY_DEVICEA")
if C.EnumDisplayDevicesA(adaptor:GetName(), 0, nextdevice, 0) > 0 then
local monitor = nextdevice[0]
print("MONITOR")
print(monitor)
else
print("No Monitors found for device: ", adaptor:GetName())
end
print()
print()
end
function GetDisplayAdaptors()
local devNum = 0
-- Enumerate all the display devices
local nextdevice = ffi.new("DISPLAY_DEVICEA[1]")
nextdevice[0].cb = ffi.sizeof("DISPLAY_DEVICEA")
while C.EnumDisplayDevicesA(nil, devNum, nextdevice, 0) > 0 do
local adaptor = nextdevice[0]
print("Name: ", adaptor:GetName())
print("Description: ", adaptor:GetDescription())
print("IS DESKTOP: ", adaptor:IsDesktop())
print("IS PRIMARY: ", adaptor:IsPrimary())
print()
GetMonitorForDisplayDevice(adaptor)
-- Allocate another device for next time through the loop
nextdevice = ffi.new("DISPLAY_DEVICEA[1]")
nextdevice[0].cb = ffi.sizeof("DISPLAY_DEVICEA")
devNum = devNum + 1
end
end
function printMonitors(monitors)
for i=1,#monitors do
print(monitors[i])
end
end
--local monitors = GetPhysicalMonitors()
--printMonitors(monitors)
GetDisplayAdaptors()