Skip to content

Commit 8712e48

Browse files
committed
Support for LEDs on Xbox 360 controllers
1 parent ef1b2c6 commit 8712e48

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Xb2XInput/XboxController.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,32 @@ void CALLBACK XboxController::OnVigemNotification(PVIGEM_CLIENT Client, PVIGEM_T
395395
LargeMotor = SmallMotor = 0;
396396

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

402403
{
403404
std::lock_guard<std::mutex> guard(usb_mutex_);
404405
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
405-
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, sizeof(XboxOutputReport), 1000);
406+
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, controller.output_prev_.bSize, 1000);
407+
}
408+
409+
memset(&controller.output_prev_, 0, sizeof(XboxOutputReport));
410+
controller.output_prev_.bReportId = XBOX_OUTPUT_REPORT_ID_LED;
411+
controller.output_prev_.bSize = 2 + sizeof(controller.output_prev_.LED);
412+
switch (LedNumber) {
413+
case 0: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_1; break;
414+
case 1: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_2; break;
415+
case 2: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_3; break;
416+
case 3: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ON_4; break;
417+
default: controller.output_prev_.LED.bAnimationId = LED_ANIMATION_ID_ALL_OFF;
418+
}
419+
420+
{
421+
std::lock_guard<std::mutex> guard(usb_mutex_);
422+
libusb_control_transfer(controller.usb_handle_, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE,
423+
HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | 0x00, 0, (unsigned char*)&controller.output_prev_, controller.output_prev_.bSize, 1000);
406424
}
407425

408426
break;

Xb2XInput/XboxController.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ typedef struct _OGXINPUT_RUMBLE
4141
WORD wRightMotorSpeed;
4242
} OGXINPUT_RUMBLE, *POGXINPUT_RUMBLE;
4343

44+
typedef struct _OGXINPUT_LED
45+
{
46+
BYTE bAnimation;
47+
} OGINPUT_LED;
48+
4449
typedef struct _OGXINPUT_GAMEPAD
4550
{
4651
WORD wButtons;
@@ -58,11 +63,26 @@ struct XboxInputReport {
5863
OGXINPUT_GAMEPAD Gamepad;
5964
};
6065

66+
#define XBOX_OUTPUT_REPORT_ID_RUMBLE 0
67+
#define XBOX_OUTPUT_REPORT_ID_LED 1
68+
#define LED_ANIMATION_ID_ALL_OFF 0
69+
#define LED_ANIMATION_ID_ON_1 6
70+
#define LED_ANIMATION_ID_ON_2 7
71+
#define LED_ANIMATION_ID_ON_3 8
72+
#define LED_ANIMATION_ID_ON_4 9
73+
74+
struct XboxOutputReportLED {
75+
BYTE bAnimationId;
76+
};
77+
6178
struct XboxOutputReport {
6279
BYTE bReportId;
6380
BYTE bSize;
6481

65-
OGXINPUT_RUMBLE Rumble;
82+
union {
83+
OGXINPUT_RUMBLE Rumble;
84+
XboxOutputReportLED LED;
85+
};
6686
};
6787

6888
struct Deadzone {

0 commit comments

Comments
 (0)