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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
#include "module.hpp"
#include "compat/library-path.hpp"
#include "compat/sleep.hpp"
#include "compat/run-in-thread.hpp"
#include <cstddef>
#include <thread>
#include <QCoreApplication>
#include <QMessageBox>
#include <libusb.h>
using namespace options;
#ifdef __GNUG__
# pragma clang diagnostic ignored "-Wcast-qual"
#endif
int device_count()
{
libusb_context * ctx = nullptr;
libusb_device **list = nullptr;
ssize_t sz = 0;
int rc = 0, cnt = 0;
constexpr int vendor_id = 0x1415;
constexpr int product_id = 0x2000;
rc = libusb_init(&ctx);
if (rc)
goto end;
sz = libusb_get_device_list(ctx, &list);
if (sz < 0)
goto end;
for (int i = 0; i < sz; ++i) {
libusb_device *device = list[i];
libusb_device_descriptor desc = {};
if (libusb_get_device_descriptor(device, &desc))
goto end;
if (desc.idVendor == vendor_id && desc.idProduct == product_id)
cnt++;
}
end:
if (list)
libusb_free_device_list(list, 1);
if (ctx)
libusb_exit(ctx);
return cnt;
}
#ifdef __linux__
# include <unistd.h>
#endif
bool check_device_exists()
{
#ifdef __linux__
// don't show when system driver exists
if (!access("/sys/module/gspca_ov534", R_OK|X_OK))
return false;
#endif
static bool ret = device_count() > 0;
return ret;
}
static const QString camera_name = QStringLiteral("PS3 Eye open driver");
std::vector<QString> ps3eye_camera_::camera_names() const
{
if (check_device_exists())
return { camera_name };
else
return {};
}
std::unique_ptr<camera> ps3eye_camera_::make_camera(const QString& name)
{
if (name == camera_name && check_device_exists())
return std::make_unique<ps3eye_camera>();
else
return {};
}
static bool show_dialog_()
{
(new dialog)->show();
return true;
}
bool ps3eye_camera_::show_dialog(const QString&)
{
return show_dialog_();
}
bool ps3eye_camera_::can_show_dialog(const QString& name)
{
return name == camera_name && check_device_exists();
}
ps3eye_camera::ps3eye_camera()
{
if (!shm.success())
return;
static const QString library_path(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH);
wrapper.setWorkingDirectory(library_path);
#ifdef _WIN32
wrapper.setProgram("\"ps3eye-subprocess.exe\"");
// workaround apparent Qt 5.15.2 bug -sh 20210817
wrapper.setProcessChannelMode(QProcess::ForwardedChannels);
#else
wrapper.setProgram("ps3eye-subprocess");
#endif
}
ps3eye_camera::~ps3eye_camera()
{
stop();
}
void ps3eye_camera::stop()
{
open = false;
if (wrapper.state() != QProcess::NotRunning)
{
volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
ptr.in.do_exit = 1;
std::atomic_thread_fence(std::memory_order_seq_cst);
wrapper.waitForFinished(1000);
if (wrapper.state() != QProcess::NotRunning)
wrapper.kill();
wrapper.waitForFinished(1000);
}
}
bool ps3eye_camera::start(info& args)
{
if (!shm.success())
return false;
stop();
volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
QString error;
using mode = ps3eye::shm_in::mode;
open = false;
fr = {};
fr.channels = args.num_channels == 1 ? 1 : 3;
fr.channel_size = 1;
if (!args.width || args.width > 320)
{
ptr.in.resolution = mode::vga;
fr.width = 640; fr.height = 480;
}
else
{
ptr.in.resolution = mode::qvga;
fr.width = 320; fr.height = 240;
}
ptr.in.auto_gain = false;
ptr.in.framerate = (uint8_t)std::clamp(args.fps, 30, 187);
ptr.in.gain = (uint8_t)s.gain;
ptr.in.exposure = (uint8_t)s.exposure;
ptr.in.channels = args.num_channels == 1 ? 1 : 3;
sleep_ms = std::clamp(int(std::floor(450./ptr.in.framerate)), 1, 10);
wrapper.start();
constexpr int sleep_ms = 10, max_sleeps = 2000/sleep_ms;
for (int i = 0; i < max_sleeps; i++)
{
if (ptr.out.timecode > 0)
goto ok;
portable::sleep(sleep_ms);
}
if (ptr.out.error_string[0] == '\0')
error = QString{};
else
error = QString::fromLatin1((const char*)ptr.out.error_string,
strnlen((const char*)ptr.out.error_string, sizeof(ptr.out.error_string)));
run_in_thread_async(qApp, [error = std::move(error)] {
dialog::show_open_failure_msgbox(error);
});
return false;
ok:
open = true;
return true;
}
std::tuple<const frame&, bool> ps3eye_camera::get_frame()
{
auto volatile* ptr = (ps3eye::shm*)shm.ptr();
if (shm.success() && open)
{
int elapsed = std::min((int)std::ceil(t.elapsed_ms()), 100);
portable::sleep(sleep_ms - elapsed);
if (unsigned tc = ptr->out.timecode; tc != timecode)
{
timecode = tc;
goto ok;
}
}
for (int i = 0; i < 2000; i++)
{
if (unsigned tc = ptr->out.timecode; tc != timecode)
{
timecode = tc;
goto ok;
}
portable::sleep(1);
}
stop();
return { fr, false };
static_assert(offsetof(decltype(ptr->out), data_640x480) == offsetof(decltype(ptr->out), data_320x240));
ok:
t.start();
memcpy(data, (unsigned char*)ptr->out.data_640x480,sizeof(ptr->out.data_640x480));
fr.data = data;
return { fr, true };
}
bool ps3eye_camera::show_dialog()
{
return show_dialog_();
}
OTR_REGISTER_CAMERA(ps3eye_camera_)
dialog::dialog(QWidget* parent) : QWidget(parent)
{
ui.setupUi(this);
adjustSize(); setFixedSize(size());
setAttribute(Qt::WA_DeleteOnClose);
t.setInterval(500); t.setSingleShot(true);
tie_setting(s.exposure, ui.exposure_slider);
tie_setting(s.gain, ui.gain_slider);
ui.exposure_label->setValue((int)*s.exposure);
ui.gain_label->setValue((int)*s.gain);
connect(&s.exposure, value_::value_changed<slider_value>(), this, [this](const slider_value&) { t.stop(); t.start(); });
connect(&s.gain, value_::value_changed<slider_value>(), this, [this](const slider_value&) { t.stop(); t.start(); });
connect(ui.exposure_slider, &QSlider::valueChanged, ui.exposure_label, &QSpinBox::setValue);
connect(ui.gain_slider, &QSlider::valueChanged, ui.gain_label, &QSpinBox::setValue);
connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &dialog::do_ok);
connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &dialog::do_cancel);
connect(&t, &QTimer::timeout, this, [this] { s.set_exposure(); s.set_gain(); });
}
void dialog::show_open_failure_msgbox(const QString& error)
{
const QString& error_ = error.isNull() ? tr("Unknown error") : error;
QMessageBox::critical(nullptr,
tr("Can't open camera"),
tr("PS3 Eye driver error: %1").arg(error_),
QMessageBox::Close);
}
// XXX copypasta -sh 20200329
void settings::set_gain()
{
if (!shm.success())
return;
auto& ptr = *(ps3eye::shm volatile*)shm.ptr();
ptr.in.gain = (unsigned char)*gain;
++ptr.in.settings_updated;
std::atomic_thread_fence(std::memory_order_seq_cst);
}
void settings::set_exposure()
{
if (!shm.success())
return;
auto& ptr = *(ps3eye::shm volatile*)shm.ptr();
ptr.in.exposure = (unsigned char)*exposure;
++ptr.in.settings_updated;
std::atomic_thread_fence(std::memory_order_seq_cst);
}
|