mirror of
https://github.com/nefarius/ViGEmBus.git
synced 2025-08-10 00:52:17 +00:00
Refactored code to make it more generic
This commit is contained in:
@@ -64,7 +64,7 @@ typedef struct _VIGEM_TARGET_T
|
||||
USHORT VendorId;
|
||||
USHORT ProductId;
|
||||
VIGEM_TARGET_TYPE Type;
|
||||
DWORD_PTR Notification;
|
||||
FARPROC Notification;
|
||||
|
||||
std::shared_ptr<NotificationRequestPool> pool;
|
||||
|
||||
|
||||
@@ -47,8 +47,8 @@ NotificationRequestPool::NotificationRequestPool(
|
||||
wait_handle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
||||
// create async pending I/O request wrapper
|
||||
requests_.push_back(std::make_unique<XusbNotificationRequest>(
|
||||
client_->hBusDevice,
|
||||
target_->SerialNo,
|
||||
client_,
|
||||
target_,
|
||||
wait_handle
|
||||
));
|
||||
}
|
||||
@@ -113,22 +113,8 @@ void NotificationRequestPool::operator()()
|
||||
// grab associated request
|
||||
const auto req = requests_[index].get();
|
||||
|
||||
// prepare queueing library caller notification callback
|
||||
const boost::function<void(
|
||||
PVIGEM_CLIENT,
|
||||
PVIGEM_TARGET,
|
||||
UCHAR,
|
||||
UCHAR,
|
||||
UCHAR)> pfn = PFN_VIGEM_X360_NOTIFICATION(callback_);
|
||||
|
||||
// submit callback for async yet ordered invocation
|
||||
strand.post(boost::bind(pfn,
|
||||
client_,
|
||||
target_,
|
||||
req->get_large_motor(),
|
||||
req->get_small_motor(),
|
||||
req->get_led_number()
|
||||
));
|
||||
req->post(std::move(strand));
|
||||
|
||||
// submit another pending I/O
|
||||
req->request_async();
|
||||
|
||||
@@ -481,12 +481,10 @@ VIGEM_ERROR vigem_target_x360_register_notification(
|
||||
if (target->SerialNo == 0 || notification == nullptr)
|
||||
return VIGEM_ERROR_INVALID_TARGET;
|
||||
|
||||
if (target->Notification == reinterpret_cast<DWORD_PTR>(notification))
|
||||
if (target->Notification == reinterpret_cast<FARPROC>(notification))
|
||||
return VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED;
|
||||
|
||||
// TODO: tidy up this mess
|
||||
|
||||
target->Notification = reinterpret_cast<DWORD_PTR>(notification);
|
||||
target->Notification = reinterpret_cast<FARPROC>(notification);
|
||||
|
||||
target->pool = std::make_shared<NotificationRequestPool>(
|
||||
vigem,
|
||||
@@ -515,10 +513,10 @@ VIGEM_ERROR vigem_target_ds4_register_notification(
|
||||
if (target->SerialNo == 0 || notification == nullptr)
|
||||
return VIGEM_ERROR_INVALID_TARGET;
|
||||
|
||||
if (target->Notification == reinterpret_cast<DWORD_PTR>(notification))
|
||||
if (target->Notification == reinterpret_cast<FARPROC>(notification))
|
||||
return VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED;
|
||||
|
||||
target->Notification = reinterpret_cast<DWORD_PTR>(notification);
|
||||
target->Notification = reinterpret_cast<FARPROC>(notification);
|
||||
|
||||
std::vector<std::thread> threadList;
|
||||
|
||||
|
||||
@@ -25,26 +25,31 @@ SOFTWARE.
|
||||
|
||||
#include "XusbNotificationRequest.h"
|
||||
#include <winioctl.h>
|
||||
#include <ViGEm/Client.h>
|
||||
#include "Internal.h"
|
||||
#include <boost/bind/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
|
||||
XusbNotificationRequest::XusbNotificationRequest(
|
||||
HANDLE bus,
|
||||
ULONG serial,
|
||||
PVIGEM_CLIENT client,
|
||||
PVIGEM_TARGET target,
|
||||
HANDLE notification
|
||||
) : parent_bus_(bus),
|
||||
) : client_(client),
|
||||
target_(target),
|
||||
payload_(),
|
||||
overlapped_()
|
||||
{
|
||||
memset(&overlapped_, 0, sizeof(OVERLAPPED));
|
||||
overlapped_.hEvent = notification;
|
||||
XUSB_REQUEST_NOTIFICATION_INIT(&payload_, serial);
|
||||
XUSB_REQUEST_NOTIFICATION_INIT(&payload_, target_->SerialNo);
|
||||
}
|
||||
|
||||
bool XusbNotificationRequest::request_async()
|
||||
{
|
||||
// queue request in driver
|
||||
const auto ret = DeviceIoControl(
|
||||
parent_bus_,
|
||||
client_->hBusDevice,
|
||||
IOCTL_XUSB_REQUEST_NOTIFICATION,
|
||||
&payload_,
|
||||
payload_.Size,
|
||||
@@ -59,6 +64,26 @@ bool XusbNotificationRequest::request_async()
|
||||
return (!ret && error == ERROR_IO_PENDING);
|
||||
}
|
||||
|
||||
void XusbNotificationRequest::post(boost::asio::io_service::strand strand) const
|
||||
{
|
||||
// prepare queueing library caller notification callback
|
||||
const boost::function<void(
|
||||
PVIGEM_CLIENT,
|
||||
PVIGEM_TARGET,
|
||||
UCHAR,
|
||||
UCHAR,
|
||||
UCHAR)> pfn = PFN_VIGEM_X360_NOTIFICATION(target_->Notification);
|
||||
|
||||
// submit callback for async yet ordered invocation
|
||||
strand.post(boost::bind(pfn,
|
||||
client_,
|
||||
target_,
|
||||
payload_.LargeMotor,
|
||||
payload_.SmallMotor,
|
||||
payload_.LedNumber
|
||||
));
|
||||
}
|
||||
|
||||
UCHAR XusbNotificationRequest::get_led_number() const
|
||||
{
|
||||
return payload_.LedNumber;
|
||||
|
||||
@@ -27,16 +27,20 @@ SOFTWARE.
|
||||
|
||||
#include <Windows.h>
|
||||
#include "ViGEm/km/BusShared.h"
|
||||
#include <boost/asio.hpp>
|
||||
#include "ViGEm/Client.h"
|
||||
|
||||
class XusbNotificationRequest
|
||||
{
|
||||
HANDLE parent_bus_;
|
||||
PVIGEM_CLIENT client_;
|
||||
PVIGEM_TARGET target_;
|
||||
XUSB_REQUEST_NOTIFICATION payload_;
|
||||
OVERLAPPED overlapped_;
|
||||
|
||||
public:
|
||||
XusbNotificationRequest(HANDLE bus, ULONG serial, HANDLE notification);
|
||||
XusbNotificationRequest(PVIGEM_CLIENT client, PVIGEM_TARGET target, HANDLE notification);
|
||||
bool request_async();
|
||||
void post(boost::asio::io_service::strand strand) const;
|
||||
|
||||
UCHAR get_led_number() const;
|
||||
UCHAR get_large_motor() const;
|
||||
|
||||
Reference in New Issue
Block a user