Skip to content
Closed
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
23 changes: 22 additions & 1 deletion src/cab/out/pac/PacDriveSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,41 @@
namespace DOF
{

PacDriveSingleton* PacDriveSingleton::s_pInstance = nullptr;

PacDriveSingleton& PacDriveSingleton::GetInstance()
{
static PacDriveSingleton instance;
return instance;
}

void PacDriveSingleton::ClearDevices()
{
if (s_pInstance)
s_pInstance->Shutdown();
}

void PacDriveSingleton::ReacquireContext()
{
// The singleton outlives any one DOF, and Shutdown() dropped its context and device
// list. Re-enumerate, or a later DOF silently finds no Pac devices.
if (s_pInstance)
s_pInstance->Initialize();
}

PacDriveSingleton::PacDriveSingleton()
: m_numDevices(0)
, m_usbContext(nullptr)
{
s_pInstance = this;
Initialize();
}

PacDriveSingleton::~PacDriveSingleton() { Shutdown(); }
PacDriveSingleton::~PacDriveSingleton()
{
Shutdown();
s_pInstance = nullptr;
}

void PacDriveSingleton::Initialize()
{
Expand Down
3 changes: 3 additions & 0 deletions src/cab/out/pac/PacDriveSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class PacDriveSingleton
};

static PacDriveSingleton& GetInstance();
static void ClearDevices();
static void ReacquireContext();

std::vector<int> PacLed64GetIdList();
int PacLed64GetIndexForDeviceId(int id);
Expand Down Expand Up @@ -80,6 +82,7 @@ class PacDriveSingleton
int m_numDevices;

libusb_context* m_usbContext;
static PacDriveSingleton* s_pInstance;
std::map<int, libusb_device_handle*> m_usbDevices;
std::mutex m_usbDevicesMutex;

Expand Down
10 changes: 10 additions & 0 deletions src/general/IOConfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "../cab/out/pspico/PinscapePico.h"
#endif

#ifdef __LIBUSB__
#include "../cab/out/pac/PacDriveSingleton.h"
#endif

#include <string>

namespace DOF
Expand All @@ -33,12 +37,18 @@ void IOConfigurator::Initialize()
s_libusbContext = nullptr;
}
}

PacDriveSingleton::ReacquireContext();
#endif
}

void IOConfigurator::Shutdown()
{
#ifdef __LIBUSB__
// PacDriveSingleton is static, so its destructor runs at process exit - long after
// libusb_exit() below frees the context its handles point into. Close them first.
PacDriveSingleton::ClearDevices();

if (s_libusbContext != nullptr)
{
libusb_exit(s_libusbContext);
Expand Down
Loading