Skip to content

Commit 37005ff

Browse files
committed
misc: move usb lifecycle into new IOConfigurator
1 parent 2bae863 commit 37005ff

9 files changed

Lines changed: 109 additions & 35 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ set(LIBDOF_SOURCES
9999
src/general/FilePattern.cpp
100100
src/general/FilePatternList.cpp
101101
src/general/FileReader.cpp
102+
src/general/IOConfigurator.cpp
102103
src/general/MathExtensions.cpp
103104
src/general/StringExtensions.cpp
104105
src/general/analog/AnalogAlpha.cpp

include/DOF/DOF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#define LIBDOF_VERSION_MAJOR 0 // X Digits
44
#define LIBDOF_VERSION_MINOR 4 // Max 2 Digits
5-
#define LIBDOF_VERSION_PATCH 6 // Max 2 Digits
5+
#define LIBDOF_VERSION_PATCH 7 // Max 2 Digits
66

77
#define _LIBDOF_STR(x) #x
88
#define LIBDOF_STR(x) _LIBDOF_STR(x)

src/DOF.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,22 @@
99
#include "Log.h"
1010
#include "Logger.h"
1111
#include "Pinball.h"
12+
#include "general/IOConfigurator.h"
1213
#include "general/StringExtensions.h"
1314

14-
#ifdef __HIDAPI__
15-
#include <hidapi/hidapi.h>
16-
#endif
17-
1815
namespace DOF
1916
{
2017

2118
DOF::DOF()
2219
{
23-
#ifdef __HIDAPI__
24-
hid_init();
25-
#endif
20+
IOConfigurator::Initialize();
2621
m_pinball = new Pinball();
2722
}
2823

2924
DOF::~DOF()
3025
{
3126
delete m_pinball;
32-
#ifdef __HIDAPI__
33-
hid_exit();
34-
#endif
27+
IOConfigurator::Shutdown();
3528
}
3629

3730
void DOF::Init(const char* tableFilename, const char* romName)

src/cab/out/dudescab/DudesCab.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ std::vector<DudesCab::Device*> DudesCab::FindDevices()
232232
else
233233
productName = "<not available>";
234234
#else
235-
std::wstring wstr(cur_dev->product_string);
236-
productName = std::string(wstr.begin(), wstr.end());
235+
wchar_t* wstr = cur_dev->product_string;
236+
while (*wstr)
237+
productName += static_cast<char>(*wstr++);
237238
#endif
238239
}
239240

@@ -250,8 +251,9 @@ std::vector<DudesCab::Device*> DudesCab::FindDevices()
250251
else
251252
serialNumber = "<not available>";
252253
#else
253-
std::wstring wserial(cur_dev->serial_number);
254-
serialNumber = std::string(wserial.begin(), wserial.end());
254+
wchar_t* wserial = cur_dev->serial_number;
255+
while (*wserial)
256+
serialNumber += static_cast<char>(*wserial++);
255257
#endif
256258
}
257259
else

src/cab/out/pac/PacDriveSingleton.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "PacDriveSingleton.h"
2-
#include "../../../Log.h"
2+
#include "../../../general/IOConfigurator.h"
33
#include "../../../general/StringExtensions.h"
44
#include <string>
5-
#include <algorithm>
65

76
namespace DOF
87
{
@@ -15,7 +14,7 @@ PacDriveSingleton& PacDriveSingleton::GetInstance()
1514

1615
PacDriveSingleton::PacDriveSingleton()
1716
: m_numDevices(0)
18-
, m_libusbContext(nullptr)
17+
, m_usbContext(nullptr)
1918
{
2019
Initialize();
2120
}
@@ -24,13 +23,7 @@ PacDriveSingleton::~PacDriveSingleton() { Shutdown(); }
2423

2524
void PacDriveSingleton::Initialize()
2625
{
27-
int result = libusb_init(&m_libusbContext);
28-
if (result < 0)
29-
{
30-
Log::Exception(StringExtensions::Build("Failed to initialize libusb: {0}", std::to_string(result)));
31-
return;
32-
}
33-
26+
m_usbContext = IOConfigurator::GetUSBContext();
3427
EnumerateDevices();
3528
}
3629

@@ -56,12 +49,7 @@ void PacDriveSingleton::Shutdown()
5649
}
5750
}
5851
m_usbDevices.clear();
59-
60-
if (m_libusbContext)
61-
{
62-
libusb_exit(m_libusbContext);
63-
m_libusbContext = nullptr;
64-
}
52+
m_usbContext = nullptr;
6553
}
6654
}
6755

@@ -151,8 +139,14 @@ void PacDriveSingleton::EnumerateDevices()
151139

152140
hid_free_enumeration(devices);
153141

142+
if (!m_usbContext)
143+
{
144+
m_numDevices = static_cast<int>(m_devices.size());
145+
return;
146+
}
147+
154148
libusb_device** usbDevices;
155-
ssize_t deviceCount = libusb_get_device_list(m_libusbContext, &usbDevices);
149+
ssize_t deviceCount = libusb_get_device_list(m_usbContext, &usbDevices);
156150

157151
if (deviceCount > 0)
158152
{
@@ -577,7 +571,10 @@ void PacDriveSingleton::OpenUsbDevice(int index)
577571
if (m_usbDevices.find(index) != m_usbDevices.end())
578572
return;
579573

580-
libusb_device_handle* handle = libusb_open_device_with_vid_pid(m_libusbContext, device.vendorId, device.productId);
574+
if (!m_usbContext)
575+
return;
576+
577+
libusb_device_handle* handle = libusb_open_device_with_vid_pid(m_usbContext, device.vendorId, device.productId);
581578
if (handle)
582579
{
583580
if (libusb_kernel_driver_active(handle, 0) == 1)

src/cab/out/pac/PacDriveSingleton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class PacDriveSingleton
7979
std::mutex m_hidDevicesMutex;
8080
int m_numDevices;
8181

82-
libusb_context* m_libusbContext;
82+
libusb_context* m_usbContext;
8383
std::map<int, libusb_device_handle*> m_usbDevices;
8484
std::mutex m_usbDevicesMutex;
8585

src/general/IOConfigurator.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "IOConfigurator.h"
2+
3+
#include "../Log.h"
4+
#include "StringExtensions.h"
5+
6+
#ifdef __HIDAPI__
7+
#include <hidapi/hidapi.h>
8+
#endif
9+
10+
#include <string>
11+
12+
namespace DOF
13+
{
14+
15+
#ifdef __LIBUSB__
16+
libusb_context* IOConfigurator::s_libusbContext = nullptr;
17+
#endif
18+
19+
void IOConfigurator::Initialize()
20+
{
21+
#ifdef __HIDAPI__
22+
hid_init();
23+
#endif
24+
#ifdef __LIBUSB__
25+
if (s_libusbContext == nullptr)
26+
{
27+
int result = libusb_init(&s_libusbContext);
28+
if (result < 0)
29+
{
30+
Log::Exception(StringExtensions::Build("Failed to initialize libusb: {0}", std::to_string(result)));
31+
s_libusbContext = nullptr;
32+
}
33+
}
34+
#endif
35+
}
36+
37+
void IOConfigurator::Shutdown()
38+
{
39+
#ifdef __LIBUSB__
40+
if (s_libusbContext != nullptr)
41+
{
42+
libusb_exit(s_libusbContext);
43+
s_libusbContext = nullptr;
44+
}
45+
#endif
46+
#ifdef __HIDAPI__
47+
hid_exit();
48+
#endif
49+
}
50+
51+
}

src/general/IOConfigurator.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include "DOF/DOF.h"
4+
5+
#ifdef __LIBUSB__
6+
#include <libusb-1.0/libusb.h>
7+
#endif
8+
9+
namespace DOF
10+
{
11+
12+
class IOConfigurator
13+
{
14+
public:
15+
static void Initialize();
16+
static void Shutdown();
17+
18+
#ifdef __LIBUSB__
19+
static libusb_context* GetUSBContext() { return s_libusbContext; }
20+
#endif
21+
22+
private:
23+
IOConfigurator() = delete;
24+
25+
#ifdef __LIBUSB__
26+
static libusb_context* s_libusbContext;
27+
#endif
28+
};
29+
30+
}

src/general/bitmap/Image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void Image::LoadFromFile(const std::string& filename)
6767
int channels;
6868
unsigned char* gifData = stbi_load_gif_from_memory(buffer.data(), static_cast<int>(fileSize), &delays, &m_width, &m_height, &frameCount, &channels, 4);
6969

70-
if (gifData != nullptr && frameCount > 1)
70+
if (gifData != nullptr && frameCount > 0)
7171
{
7272
m_channels = 4;
7373
m_frameCount = frameCount;

0 commit comments

Comments
 (0)