Skip to content

Commit

Permalink
Support for LEDs on Xbox 360 controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
db3000 committed Oct 11, 2021
1 parent ef1b2c6 commit 8712e48
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
22 changes: 20 additions & 2 deletions Xb2XInput/XboxController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,32 @@ void CALLBACK XboxController::OnVigemNotification(PVIGEM_CLIENT Client, PVIGEM_T
LargeMotor = SmallMotor = 0;

memset(&controller.output_prev_, 0, sizeof(XboxOutputReport));
controller.output_prev_.bSize = sizeof(XboxOutputReport);
controller.output_prev_.bReportId = XBOX_OUTPUT_REPORT_ID_RUMBLE;
controller.output_prev_.bSize = 2 + sizeof(controller.output_prev_.Rumble);
controller.output_prev_.Rumble.wLeftMotorSpeed = _byteswap_ushort(LargeMotor); // why do these need to be byteswapped???
controller.output_prev_.Rumble.wRightMotorSpeed = _byteswap_ushort(SmallMotor);

{
std::lock_guard<std::mutex> guard(usb_mutex_);
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, sizeof(XboxOutputReport), 1000);
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, controller.output_prev_.bSize, 1000);
}

memset(&controller.output_prev_, 0, sizeof(XboxOutputReport));
controller.output_prev_.bReportId = XBOX_OUTPUT_REPORT_ID_LED;
controller.output_prev_.bSize = 2 + sizeof(controller.output_prev_.LED);
switch (LedNumber) {
case 0: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_1; break;
case 1: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_2; break;
case 2: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_3; break;
case 3: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_4; break;
default: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ALL_OFF;
}

{
std::lock_guard<std::mutex> guard(usb_mutex_);
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, controller.output_prev_.bSize, 1000);
}

break;
Expand Down
22 changes: 21 additions & 1 deletion Xb2XInput/XboxController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ typedef struct _OGXINPUT_RUMBLE
WORD wRightMotorSpeed;
} OGXINPUT_RUMBLE, *POGXINPUT_RUMBLE;

typedef struct _OGXINPUT_LED
{
BYTE bAnimation;
} OGINPUT_LED;

typedef struct _OGXINPUT_GAMEPAD
{
WORD wButtons;
Expand All @@ -58,11 +63,26 @@ struct XboxInputReport {
OGXINPUT_GAMEPAD Gamepad;
};

#define XBOX_OUTPUT_REPORT_ID_RUMBLE 0
#define XBOX_OUTPUT_REPORT_ID_LED 1
#define LED_ANIMATION_ID_ALL_OFF 0
#define LED_ANIMATION_ID_ON_1 6
#define LED_ANIMATION_ID_ON_2 7
#define LED_ANIMATION_ID_ON_3 8
#define LED_ANIMATION_ID_ON_4 9

struct XboxOutputReportLED {
BYTE bAnimationId;
};

struct XboxOutputReport {
BYTE bReportId;
BYTE bSize;

OGXINPUT_RUMBLE Rumble;
union {
OGXINPUT_RUMBLE Rumble;
XboxOutputReportLED LED;
};
};

struct Deadzone {
Expand Down

0 comments on commit 8712e48

Please sign in to comment.