Skip to content

Commit

Permalink
nsyshid: Add Emulated Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
deReeperJosh committed Oct 26, 2023
1 parent 11d92d8 commit 3ebadc0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Cafe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ add_library(CemuCafe
OS/libs/nsyshid/AttachDefaultBackends.cpp
OS/libs/nsyshid/Whitelist.cpp
OS/libs/nsyshid/Whitelist.h
OS/libs/nsyshid/BackendEmulated.cpp
OS/libs/nsyshid/BackendEmulated.h
OS/libs/nsyshid/BackendLibusb.cpp
OS/libs/nsyshid/BackendLibusb.h
OS/libs/nsyshid/BackendWindowsHID.cpp
Expand Down
9 changes: 9 additions & 0 deletions src/Cafe/OS/libs/nsyshid/AttachDefaultBackends.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "nsyshid.h"
#include "Backend.h"
#include "BackendEmulated.h"

#if NSYSHID_ENABLE_BACKEND_LIBUSB

Expand Down Expand Up @@ -37,5 +38,13 @@ namespace nsyshid::backend
}
}
#endif // NSYSHID_ENABLE_BACKEND_WINDOWS_HID
// add emulated backend
{
auto backendEmulated = std::make_shared<backend::emulated::BackendEmulated>();
if (backendEmulated->IsInitialisedOk())
{
AttachBackend(backendEmulated);
}
}
}
} // namespace nsyshid::backend
2 changes: 2 additions & 0 deletions src/Cafe/OS/libs/nsyshid/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ namespace nsyshid

std::shared_ptr<Device> FindDevice(std::function<bool(const std::shared_ptr<Device>&)> isWantedDevice);

bool FindDeviceById(uint16 vendorId, uint16 productId);

bool IsDeviceWhitelisted(uint16 vendorId, uint16 productId);

// called from OnAttach() - attach devices that your backend can see here
Expand Down
29 changes: 29 additions & 0 deletions src/Cafe/OS/libs/nsyshid/BackendEmulated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "BackendEmulated.h"
#include "Skylander.h"
#include "config/CemuConfig.h"

namespace nsyshid::backend::emulated
{
BackendEmulated::BackendEmulated()
{
cemuLog_logDebug(LogType::Force, "nsyshid::BackendEmulated: emulated backend initialised");
}

BackendEmulated::~BackendEmulated() = default;

bool BackendEmulated::IsInitialisedOk()
{
return true;
}

void BackendEmulated::AttachVisibleDevices()
{
if (GetConfig().emulated_usb_devices.emulate_skylander_portal && !FindDeviceById(0x1430, 0x0150))
{
cemuLog_logDebug(LogType::Force, "Attaching Emulated Portal");
// Add Skylander Portal
auto device = std::make_shared<SkylanderPortalDevice>();
AttachDevice(device);
}
}
} // namespace nsyshid::backend::emulated
22 changes: 22 additions & 0 deletions src/Cafe/OS/libs/nsyshid/BackendEmulated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "nsyshid.h"
#include "Backend.h"

namespace nsyshid::backend::emulated
{
class BackendEmulated : public nsyshid::Backend {
public:
BackendEmulated();
~BackendEmulated();

bool IsInitialisedOk() override;

protected:
void AttachVisibleDevices() override;

private:
// Found Devices
bool m_foundSkylander = false;
bool m_foundInfinity = false;
bool m_foundDimensions = false;
};
} // namespace nsyshid::backend::emulated
26 changes: 13 additions & 13 deletions src/Cafe/OS/libs/nsyshid/nsyshid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,6 @@ namespace nsyshid
device->m_productId);
}

void AttachEmulatedDevices()
{
if (GetConfig().emulated_usb_devices.emulate_skylander_portal && !backendList.front()->m_foundSkylander)
{
cemuLog_logDebug(LogType::Force, "Attaching Emulated Portal");
// Add Skylander Portal
auto device = std::make_shared<SkylanderPortalDevice>();
AttachDevice(device);
}
}

void export_HIDAddClient(PPCInterpreter_t* hCPU)
{
ppcDefineParamTypePtr(hidClient, HIDClient_t, 0);
Expand Down Expand Up @@ -773,6 +762,19 @@ namespace nsyshid
return nullptr;
}

bool Backend::FindDeviceById(uint16 vendorId, uint16 productId)
{
std::lock_guard<std::recursive_mutex> lock(this->m_devicesMutex);
for (const auto& device : m_devices)
{
if (device->m_vendorId == vendorId && device->m_productId == productId)
{
return true;
}
}
return false;
}

bool Backend::IsDeviceWhitelisted(uint16 vendorId, uint16 productId)
{
return Whitelist::GetInstance().IsDeviceWhitelisted(vendorId, productId);
Expand Down Expand Up @@ -854,7 +856,5 @@ namespace nsyshid
Whitelist::GetInstance();

AttachDefaultBackends();

AttachEmulatedDevices();
}
} // namespace nsyshid

0 comments on commit 3ebadc0

Please sign in to comment.