-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDesktopListFunctions.cc
More file actions
163 lines (138 loc) · 4.27 KB
/
DesktopListFunctions.cc
File metadata and controls
163 lines (138 loc) · 4.27 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
#include "DesktopListFunctions.h"
#include <nan.h>
#include <windows.h>
#include <iostream>
#include <string.h>
#include <DXGI.h>
#include <D3DCommon.h>
#include <D3D11.h>
#include <list>
#include "WinAsyncWorker.h"
using Nan::Callback;
using v8::Function;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Value;
using v8::Array;
using Nan::AsyncQueueWorker;
using Nan::AsyncWorker;
using Nan::Callback;
using Nan::HandleScope;
using Nan::New;
using Nan::Set;
using Nan::Null;
using Nan::To;
class GetDesktopsWorker: public AsyncWorker {
public:
GetDesktopsWorker(Callback *callback)
: AsyncWorker(callback) {
};
~GetDesktopsWorker() {
};
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute() {
ID3D11Device *device;
D3D_DRIVER_TYPE DriverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
HRESULT hr;
// Create device
for (UINT DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
{
hr = D3D11CreateDevice(nullptr, DriverTypes[DriverTypeIndex], nullptr, 0, NULL, 0,
D3D11_SDK_VERSION, &device, NULL, NULL);
if (SUCCEEDED(hr))
{
// Device creation success, no need to loop anymore
break;
}
}
if (!chk(hr, "can't open d3d device")) return;
IDXGIDevice* dxgiDevice = nullptr;
hr = device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice));
if (!chk(hr, "can't open dxgi device")) return;
IDXGIAdapter* dxgiAdapter = nullptr;
hr = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&dxgiAdapter));
if (!chk(hr, "can't open dxgi adapter")) return;
IDXGIFactory * dxgiFactory = nullptr;
hr = dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&dxgiFactory);
if (!chk(hr, "can't open dxgi factory")) return;
UINT i = 0;
IDXGIAdapter * a;
while (dxgiFactory->EnumAdapters(i, &a) != DXGI_ERROR_NOT_FOUND) {
UINT j = 0;
IDXGIOutput * pOutput;
while (a->EnumOutputs(j, &pOutput) != DXGI_ERROR_NOT_FOUND)
{
DXGI_OUTPUT_DESC desc = { 0 };
HRESULT hr = pOutput->GetDesc(&desc);
if (SUCCEEDED(hr)) {
if (desc.AttachedToDesktop) {
// MONITORINFOEX mi;
// ZeroMemory(&mi, sizeof(mi));
// mi.cbSize = sizeof(mi);
// GetMonitorInfoA(desc.Monitor, &mi);
// std::cout << mi.szDevice << " " << desc.Monitor << std::endl;
std::string id("desktop:");
id.append(std::to_string(i));
id.append(":");
id.append(std::to_string(j));
screens.push_back(id);
}
}
pOutput->Release();
++j;
}
a->Release();
++i;
}
chk(hr, "could not find monitor");
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback() {
HandleScope scope;
Local<Array> result = Nan::New<Array>(screens.size());
int screenNr = 1;
for (std::list<std::string>::iterator it = screens.begin(); it != screens.end(); ++it) {
Local<Object> obj = Nan::New<Object>();
std::string id = *it;
std::string label("Screen ");
label.append(std::to_string(screenNr));
Set(obj, New("id").ToLocalChecked(), New(id).ToLocalChecked());
Set(obj, New("label").ToLocalChecked(),New(label).ToLocalChecked());
Set(obj, New("type").ToLocalChecked(), New("desktop").ToLocalChecked());
Nan::Set(result, screenNr-1, obj);
screenNr++;
}
Local<Value> argv[] = { Null() , result };
callback->Call(2, argv);
}
protected:
std::list<std::string> screens;
bool chk(HRESULT hresult, std::string & msg) {
std::string mymsg(msg);
if (hresult != NOERROR) {
mymsg.append(errno_to_text(hresult));
SetErrorMessage(mymsg.c_str());
return false;
}
return true;
}
bool chk(HRESULT hresult, const char * msg) {
return chk(hresult, std::string(msg));
}
};
NAN_METHOD(getDesktops) {
Callback *callback = new Callback(info[0].As<Function>());
AsyncQueueWorker(new GetDesktopsWorker(callback));
}