-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstraintFunctions.cc
More file actions
189 lines (163 loc) · 4.34 KB
/
ConstraintFunctions.cc
File metadata and controls
189 lines (163 loc) · 4.34 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
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "ConstraintFunctions.h"
#include "WinAsyncWorker.h"
#include <string>
#include <cstdint>
#include <cinttypes>
#include <iostream>
using Nan::AsyncQueueWorker;
using Nan::AsyncWorker;
using Nan::Callback;
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 GetConstraintWorker : public WinAsyncWorker
{
public:
GetConstraintWorker(Callback *callback)
: WinAsyncWorker(callback){};
~GetConstraintWorker()
{
if (constraint)
{
delete constraint;
}
};
// 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()
{
HKEY hkey = NULL;
if (!chk(RegOpen(KEY_READ, &hkey), "Can't open registry"))
{
return;
}
this->readData(hkey);
chk(RegClose(hkey), "Can't close registry");
}
// 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<Object> obj = Nan::New<Object>();
Set(obj, New("width").ToLocalChecked(), New(constraint->width));
Set(obj, New("height").ToLocalChecked(), New(constraint->height));
Set(obj, New("fps").ToLocalChecked(), New(constraint->fps));
Local<Value> argv[] = {
Null(), obj};
callback->Call(2, argv);
}
protected:
ConstraintEntity *constraint;
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));
}
bool chkGetDWord(HKEY hkey, char *key, uint32_t *data)
{
std::string msg("Can't read from registry (");
msg.append(key);
msg.append(") ");
return chk(getDWord(hkey, key, data), msg);
}
bool chkPutDWord(HKEY hkey, char *key, uint32_t data)
{
std::string msg("Can't write to registry (");
msg.append(key);
msg.append(") ");
return chk(putDWord(hkey, key, data), msg);
}
void readData(HKEY hkey)
{
constraint = new ConstraintEntity();
if (!chkGetDWord(hkey, "CaptureWidth", &constraint->width))
return;
if (!chkGetDWord(hkey, "CaptureHeight", &constraint->height))
return;
if (!chkGetDWord(hkey, "CaptureFPS", &constraint->fps))
return;
}
};
class SetConstraintWorker : public GetConstraintWorker
{
public:
SetConstraintWorker(ConstraintEntity *constraint, Callback *callback)
: GetConstraintWorker(callback)
{
this->constraint = constraint;
};
~SetConstraintWorker(){};
// 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()
{
HKEY hkey = NULL;
if (!chk(RegOpen(KEY_ALL_ACCESS, &hkey), "Can't open registry for write"))
{
return;
}
if (constraint->width != 0)
{
if (!chkPutDWord(hkey, "CaptureWidth", constraint->width))
return;
}
if (constraint->height != 0)
{
if (!chkPutDWord(hkey, "CaptureHeight", constraint->height))
return;
}
if (constraint->fps != 0)
{
if (!chkPutDWord(hkey, "CaptureFPS", constraint->fps))
return;
}
delete (constraint);
readData(hkey);
chk(RegClose(hkey), "Can't close registry");
}
};
NAN_METHOD(getConstraints)
{
Callback *callback = new Callback(info[0].As<Function>());
AsyncQueueWorker(new GetConstraintWorker(callback));
}
NAN_METHOD(setConstraints)
{
auto constraint = new ConstraintEntity();
Nan::Maybe<uint32_t> width = Nan::To<uint32_t>(info[0]);
constraint->width = width.ToChecked();
Nan::Maybe<uint32_t> height = Nan::To<uint32_t>(info[1]);
constraint->height = height.ToChecked();
Nan::Maybe<uint32_t> fps = Nan::To<uint32_t>(info[2]);
constraint->fps = fps.ToChecked();
Callback *callback = new Callback(info[3].As<Function>());
AsyncQueueWorker(new SetConstraintWorker(constraint, callback));
}