diff --git a/src/Internal.h b/src/Internal.h index 19f9e10..0520691 100644 --- a/src/Internal.h +++ b/src/Internal.h @@ -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 pool; diff --git a/src/NotificationRequestPool.cpp b/src/NotificationRequestPool.cpp index f429938..89c9b08 100644 --- a/src/NotificationRequestPool.cpp +++ b/src/NotificationRequestPool.cpp @@ -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( - 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 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(); diff --git a/src/ViGEmClient.cpp b/src/ViGEmClient.cpp index e4f2c98..d42ef63 100644 --- a/src/ViGEmClient.cpp +++ b/src/ViGEmClient.cpp @@ -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(notification)) + if (target->Notification == reinterpret_cast(notification)) return VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED; - // TODO: tidy up this mess - - target->Notification = reinterpret_cast(notification); + target->Notification = reinterpret_cast(notification); target->pool = std::make_shared( 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(notification)) + if (target->Notification == reinterpret_cast(notification)) return VIGEM_ERROR_CALLBACK_ALREADY_REGISTERED; - target->Notification = reinterpret_cast(notification); + target->Notification = reinterpret_cast(notification); std::vector threadList; diff --git a/src/XusbNotificationRequest.cpp b/src/XusbNotificationRequest.cpp index 9929c12..a0b75b1 100644 --- a/src/XusbNotificationRequest.cpp +++ b/src/XusbNotificationRequest.cpp @@ -25,26 +25,31 @@ SOFTWARE. #include "XusbNotificationRequest.h" #include +#include +#include "Internal.h" +#include +#include 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 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; diff --git a/src/XusbNotificationRequest.h b/src/XusbNotificationRequest.h index 479d20e..84ec8c2 100644 --- a/src/XusbNotificationRequest.h +++ b/src/XusbNotificationRequest.h @@ -27,16 +27,20 @@ SOFTWARE. #include #include "ViGEm/km/BusShared.h" +#include +#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;