Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ set(LIBDOF_SOURCES
src/general/FilePattern.cpp
src/general/FilePatternList.cpp
src/general/FileReader.cpp
src/general/IOConfigurator.cpp
src/general/MathExtensions.cpp
src/general/StringExtensions.cpp
src/general/analog/AnalogAlpha.cpp
Expand Down
2 changes: 1 addition & 1 deletion include/DOF/DOF.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define LIBDOF_VERSION_MAJOR 0 // X Digits
#define LIBDOF_VERSION_MINOR 4 // Max 2 Digits
#define LIBDOF_VERSION_PATCH 6 // Max 2 Digits
#define LIBDOF_VERSION_PATCH 7 // Max 2 Digits

#define _LIBDOF_STR(x) #x
#define LIBDOF_STR(x) _LIBDOF_STR(x)
Expand Down
13 changes: 3 additions & 10 deletions src/DOF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,22 @@
#include "Log.h"
#include "Logger.h"
#include "Pinball.h"
#include "general/IOConfigurator.h"
#include "general/StringExtensions.h"

#ifdef __HIDAPI__
#include <hidapi/hidapi.h>
#endif

namespace DOF
{

DOF::DOF()
{
#ifdef __HIDAPI__
hid_init();
#endif
IOConfigurator::Initialize();
m_pinball = new Pinball();
}

DOF::~DOF()
{
delete m_pinball;
#ifdef __HIDAPI__
hid_exit();
#endif
IOConfigurator::Shutdown();
}

void DOF::Init(const char* tableFilename, const char* romName)
Expand Down
10 changes: 6 additions & 4 deletions src/cab/out/dudescab/DudesCab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ std::vector<DudesCab::Device*> DudesCab::FindDevices()
else
productName = "<not available>";
#else
std::wstring wstr(cur_dev->product_string);
productName = std::string(wstr.begin(), wstr.end());
wchar_t* wstr = cur_dev->product_string;
while (*wstr)
productName += static_cast<char>(*wstr++);
#endif
}

Expand All @@ -250,8 +251,9 @@ std::vector<DudesCab::Device*> DudesCab::FindDevices()
else
serialNumber = "<not available>";
#else
std::wstring wserial(cur_dev->serial_number);
serialNumber = std::string(wserial.begin(), wserial.end());
wchar_t* wserial = cur_dev->serial_number;
while (*wserial)
serialNumber += static_cast<char>(*wserial++);
#endif
}
else
Expand Down
33 changes: 15 additions & 18 deletions src/cab/out/pac/PacDriveSingleton.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "PacDriveSingleton.h"
#include "../../../Log.h"
#include "../../../general/IOConfigurator.h"
#include "../../../general/StringExtensions.h"
#include <string>
#include <algorithm>

namespace DOF
{
Expand All @@ -15,7 +14,7 @@ PacDriveSingleton& PacDriveSingleton::GetInstance()

PacDriveSingleton::PacDriveSingleton()
: m_numDevices(0)
, m_libusbContext(nullptr)
, m_usbContext(nullptr)
{
Initialize();
}
Expand All @@ -24,13 +23,7 @@ PacDriveSingleton::~PacDriveSingleton() { Shutdown(); }

void PacDriveSingleton::Initialize()
{
int result = libusb_init(&m_libusbContext);
if (result < 0)
{
Log::Exception(StringExtensions::Build("Failed to initialize libusb: {0}", std::to_string(result)));
return;
}

m_usbContext = IOConfigurator::GetUSBContext();
EnumerateDevices();
}

Expand All @@ -56,12 +49,7 @@ void PacDriveSingleton::Shutdown()
}
}
m_usbDevices.clear();

if (m_libusbContext)
{
libusb_exit(m_libusbContext);
m_libusbContext = nullptr;
}
m_usbContext = nullptr;
}
}

Expand Down Expand Up @@ -151,8 +139,14 @@ void PacDriveSingleton::EnumerateDevices()

hid_free_enumeration(devices);

if (!m_usbContext)
{
m_numDevices = static_cast<int>(m_devices.size());
return;
}

libusb_device** usbDevices;
ssize_t deviceCount = libusb_get_device_list(m_libusbContext, &usbDevices);
ssize_t deviceCount = libusb_get_device_list(m_usbContext, &usbDevices);

if (deviceCount > 0)
{
Expand Down Expand Up @@ -577,7 +571,10 @@ void PacDriveSingleton::OpenUsbDevice(int index)
if (m_usbDevices.find(index) != m_usbDevices.end())
return;

libusb_device_handle* handle = libusb_open_device_with_vid_pid(m_libusbContext, device.vendorId, device.productId);
if (!m_usbContext)
return;

libusb_device_handle* handle = libusb_open_device_with_vid_pid(m_usbContext, device.vendorId, device.productId);
if (handle)
{
if (libusb_kernel_driver_active(handle, 0) == 1)
Expand Down
2 changes: 1 addition & 1 deletion src/cab/out/pac/PacDriveSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PacDriveSingleton
std::mutex m_hidDevicesMutex;
int m_numDevices;

libusb_context* m_libusbContext;
libusb_context* m_usbContext;
std::map<int, libusb_device_handle*> m_usbDevices;
std::mutex m_usbDevicesMutex;

Expand Down
51 changes: 51 additions & 0 deletions src/general/IOConfigurator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "IOConfigurator.h"

#include "../Log.h"
#include "StringExtensions.h"

#ifdef __HIDAPI__
#include <hidapi/hidapi.h>
#endif

#include <string>

namespace DOF
{

#ifdef __LIBUSB__
libusb_context* IOConfigurator::s_libusbContext = nullptr;
#endif

void IOConfigurator::Initialize()
{
#ifdef __HIDAPI__
hid_init();
#endif
#ifdef __LIBUSB__
if (s_libusbContext == nullptr)
{
int result = libusb_init(&s_libusbContext);
if (result < 0)
{
Log::Exception(StringExtensions::Build("Failed to initialize libusb: {0}", std::to_string(result)));
s_libusbContext = nullptr;
}
}
#endif
}

void IOConfigurator::Shutdown()
{
#ifdef __LIBUSB__
if (s_libusbContext != nullptr)
{
libusb_exit(s_libusbContext);
s_libusbContext = nullptr;
}
#endif
#ifdef __HIDAPI__
hid_exit();
#endif
}

}
30 changes: 30 additions & 0 deletions src/general/IOConfigurator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "DOF/DOF.h"

#ifdef __LIBUSB__
#include <libusb-1.0/libusb.h>
#endif

namespace DOF
{

class IOConfigurator
{
public:
static void Initialize();
static void Shutdown();

#ifdef __LIBUSB__
static libusb_context* GetUSBContext() { return s_libusbContext; }
#endif

private:
IOConfigurator() = delete;

#ifdef __LIBUSB__
static libusb_context* s_libusbContext;
#endif
};

}
2 changes: 1 addition & 1 deletion src/general/bitmap/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Image::LoadFromFile(const std::string& filename)
int channels;
unsigned char* gifData = stbi_load_gif_from_memory(buffer.data(), static_cast<int>(fileSize), &delays, &m_width, &m_height, &frameCount, &channels, 4);

if (gifData != nullptr && frameCount > 1)
if (gifData != nullptr && frameCount > 0)
{
m_channels = 4;
m_frameCount = frameCount;
Expand Down