forked from inspirit/PS3EYEDriver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmgr.cpp
130 lines (106 loc) · 2.88 KB
/
mgr.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
127
128
129
130
#undef NDEBUG
#include <cassert>
#include "mgr.hpp"
#include "internal.hpp"
#include "ps3eye.hpp"
#include <libusb.h>
namespace ps3eye::detail {
extern volatile bool _ps3eye_debug_status;
enum {
vendor_id = 0x1415,
product_id = 0x2000,
};
usb_manager::usb_manager()
{
libusb_init(&usb_context);
libusb_set_option(usb_context,
LIBUSB_OPTION_LOG_LEVEL,
_ps3eye_debug_status ? LIBUSB_LOG_LEVEL_INFO : LIBUSB_LOG_LEVEL_NONE);
}
usb_manager::~usb_manager()
{
//ps3eye_debug("usb_manager destructor");
if (update_thread.joinable())
stop_xfer_thread();
libusb_exit(usb_context);
}
usb_manager& usb_manager::instance()
{
static usb_manager ret;
return ret;
}
void usb_manager::camera_started()
{
assert(usb_context);
if (active_camera_count.fetch_add(1) == 0)
start_xfer_thread();
}
void usb_manager::camera_stopped()
{
if (active_camera_count.fetch_sub(1) == 1)
stop_xfer_thread();
}
void usb_manager::start_xfer_thread()
{
update_thread = std::thread(&usb_manager::xfer_callback, this);
}
void usb_manager::stop_xfer_thread()
{
exit_signaled = true;
update_thread.join();
// Reset the exit signal flag.
// If we don't and we call start_xfer_thread() again, xfer_callback
// will exit immediately.
exit_signaled = false;
}
void usb_manager::xfer_callback()
{
struct timeval tv { 0, 100 * 1000 /* ms */ };
while (!(exit_signaled.load(std::memory_order_relaxed)))
libusb_handle_events_timeout_completed(usb_context, &tv, nullptr);
}
std::vector<std::shared_ptr<camera>> usb_manager::list_devices()
{
std::vector<std::shared_ptr<camera>> list;
libusb_device* dev;
libusb_device** devs;
libusb_device_handle* devhandle;
int i = 0;
int cnt;
cnt = (int)libusb_get_device_list(usb_context, &devs);
if (cnt < 0)
{
ps3eye_debug("Error Device scan");
}
cnt = 0;
while ((dev = devs[i++]) != nullptr)
{
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(dev, &desc);
if (desc.idVendor == vendor_id && desc.idProduct == product_id)
{
int err = libusb_open(dev, &devhandle);
if (err == 0)
{
libusb_close(devhandle);
list.push_back(std::make_shared<camera>(dev));
libusb_ref_device(dev);
cnt++;
}
else
ps3eye_debug("failed open device: %d", err);
}
}
libusb_free_device_list(devs, 1);
return list;
}
void usb_manager::set_debug(bool value)
{
if (value == _ps3eye_debug_status)
return;
_ps3eye_debug_status = value;
libusb_set_option(usb_context,
LIBUSB_OPTION_LOG_LEVEL,
value ? LIBUSB_LOG_LEVEL_INFO : LIBUSB_LOG_LEVEL_NONE);
}
} // ns ps3eye::detail