Skip to content

Commit

Permalink
Added USB Support for Dualshock 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Denellyne committed Feb 15, 2024
1 parent c4b7b54 commit 7aa1b5b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ bool isDualShoch4Connected(controller& x360Controller) {
hid_free_enumeration(deviceInfo);

if (x360Controller.bluetooth) { //Bluetooth
x360Controller.bluetooth = 0;
x360Controller.bufferSize = 547;
x360Controller.inputBuffer[0] = 0x11; //Data report code
return true;
}
//USB
x360Controller.bluetooth = 1; //Usb has a 2 offset from bluetooth
x360Controller.bufferSize = 64;
x360Controller.inputBuffer[0] = 0x01; //Data report code
return true;
Expand Down
92 changes: 50 additions & 42 deletions src/ControllerIO/Dualshock4/dualshock4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@

extern bool gameProfileSet;

#define isSelectPressed (buttonMapping[11])*(!(x360Controller.inputBuffer[37] & (1 << 7)) & (((((((x360Controller.inputBuffer[39] & 0x0f) << 8) | x360Controller.inputBuffer[38]))) < 800)) ^ (x360Controller.inputBuffer[8] & (1 << 4)))
#define isStartPressed (buttonMapping[11])*(!(x360Controller.inputBuffer[37] & (1 << 7)) & (((((x360Controller.inputBuffer[39]) & 0x0F) << 8) | ((x360Controller.inputBuffer[38]))) >= (800)) ^ (x360Controller.inputBuffer[8] & (1 << 5)))
#define isSelectPressed (buttonMapping[11])*(!(x360Controller.inputBuffer[37- x360Controller.bluetooth * 2] & (1 << 7)) & (((((((x360Controller.inputBuffer[39- x360Controller.bluetooth * 2] & 0x0f) << 8) | x360Controller.inputBuffer[38- x360Controller.bluetooth * 2]))) < 800)) ^ (x360Controller.inputBuffer[8- x360Controller.bluetooth * 2] & (1 << 4)))
#define isStartPressed (buttonMapping[11])*(!(x360Controller.inputBuffer[37- x360Controller.bluetooth * 2] & (1 << 7)) & (((((x360Controller.inputBuffer[39- x360Controller.bluetooth * 2]) & 0x0F) << 8) | ((x360Controller.inputBuffer[38- x360Controller.bluetooth * 2]))) >= (800)) ^ (x360Controller.inputBuffer[8- x360Controller.bluetooth * 2] & (1 << 5)))

extern "C" int returnSmaller(int x); //Assembly Function in src/Assembly Functions/assemblyFunctions.s
constexpr DWORD TITLE_SIZE = 1024;
void inline static setButtons(controller& x360Controller) {

// Normal Order
x360Controller.ControllerState.Gamepad.wButtons = (bool)(x360Controller.inputBuffer[7] & (1 << 4)) ? XINPUT_GAMEPAD_X : 0; //Square
x360Controller.ControllerState.Gamepad.wButtons = (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 4)) ? XINPUT_GAMEPAD_X : 0; //Square

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 5)) ? XINPUT_GAMEPAD_A : 0; //Cross
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 5)) ? XINPUT_GAMEPAD_A : 0; //Cross

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 6)) ? XINPUT_GAMEPAD_B : 0; //Circle
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 6)) ? XINPUT_GAMEPAD_B : 0; //Circle

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 7)) ? XINPUT_GAMEPAD_Y : 0; //Triangle
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 7)) ? XINPUT_GAMEPAD_Y : 0; //Triangle

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 0)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0; //Left Shoulder
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 0)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0; //Left Shoulder

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 1)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0; //Right Shoulder
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 1)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0; //Right Shoulder

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 4)) ? XINPUT_GAMEPAD_BACK : 0; //Select
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 4)) ? XINPUT_GAMEPAD_BACK : 0; //Select

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 5)) ? XINPUT_GAMEPAD_START : 0; //Start
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 5)) ? XINPUT_GAMEPAD_START : 0; //Start

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 6)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0; //Left Thumb
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 6)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0; //Left Thumb

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 7)) ? XINPUT_GAMEPAD_RIGHT_THUMB : 0; //Right thumb
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 7)) ? XINPUT_GAMEPAD_RIGHT_THUMB : 0; //Right thumb
//ControllerState.Gamepad.wButtons += joystick.rgbButtons[12] ? 0x0400 : 0; //Sony Button

switch ((int)(x360Controller.inputBuffer[7] & 0x0F)) {
switch ((int)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & 0x0F)) {
case 0: x360Controller.ControllerState.Gamepad.wButtons += XINPUT_GAMEPAD_DPAD_UP; break;

case 1: x360Controller.ControllerState.Gamepad.wButtons += XINPUT_GAMEPAD_DPAD_UP + XINPUT_GAMEPAD_DPAD_RIGHT; break;
Expand All @@ -59,30 +59,30 @@ void inline static setButtonsGameProfile(controller& x360Controller) {
extern int buttonMapping[12];

// Normal Order
x360Controller.ControllerState.Gamepad.wButtons = (bool)(x360Controller.inputBuffer[7] & (1 << 4)) ? buttonMapping[0] : 0; //Square
x360Controller.ControllerState.Gamepad.wButtons = (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 4)) ? buttonMapping[0] : 0; //Square

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 5)) ? buttonMapping[1] : 0; //Cross
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 5)) ? buttonMapping[1] : 0; //Cross

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 6)) ? buttonMapping[2] : 0; //Circle
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 6)) ? buttonMapping[2] : 0; //Circle

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7] & (1 << 7)) ? buttonMapping[3] : 0; //Triangle
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & (1 << 7)) ? buttonMapping[3] : 0; //Triangle

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 0)) ? buttonMapping[4] : 0; //Left Shoulder
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 0)) ? buttonMapping[4] : 0; //Left Shoulder

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 1)) ? buttonMapping[5] : 0; //Right Shoulder
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 1)) ? buttonMapping[5] : 0; //Right Shoulder

x360Controller.ControllerState.Gamepad.wButtons += isSelectPressed ? buttonMapping[6] : 0;

x360Controller.ControllerState.Gamepad.wButtons += isStartPressed ? buttonMapping[7] : 0;

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 6)) ? buttonMapping[8] : 0; //Left Thumb
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 6)) ? buttonMapping[8] : 0; //Left Thumb

x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8] & (1 << 7)) ? buttonMapping[9] : 0; //Right thumb
x360Controller.ControllerState.Gamepad.wButtons += (bool)(x360Controller.inputBuffer[8 - x360Controller.bluetooth * 2] & (1 << 7)) ? buttonMapping[9] : 0; //Right thumb
//ControllerState.Gamepad.wButtons += joystick.rgbButtons[12] ? 0x0400 : 0; //Sony Button

if (buttonMapping[10] == 1) {

switch ((int)(x360Controller.inputBuffer[7] & 0x0f)) {
switch ((int)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & 0x0f)) {

case 0: x360Controller.ControllerState.Gamepad.sThumbLY = 32767; break; //Up

Expand All @@ -103,7 +103,7 @@ void inline static setButtonsGameProfile(controller& x360Controller) {
return;
}

switch ((int)(x360Controller.inputBuffer[7] & 0x0F)) {
switch ((int)(x360Controller.inputBuffer[7 - x360Controller.bluetooth * 2] & 0x0F)) {
case 0: x360Controller.ControllerState.Gamepad.wButtons += XINPUT_GAMEPAD_DPAD_UP; break;

case 1: x360Controller.ControllerState.Gamepad.wButtons += XINPUT_GAMEPAD_DPAD_UP + XINPUT_GAMEPAD_DPAD_RIGHT; break;
Expand Down Expand Up @@ -133,17 +133,17 @@ void inline getDualShock4Input(controller& x360Controller) {
return;
}

x360Controller.batteryLevel = (x360Controller.inputBuffer[32] & 15) * 12.5;
x360Controller.batteryLevel = (x360Controller.inputBuffer[32 - x360Controller.bluetooth * 2] & 15) * 12.5;

x360Controller.batteryLevel = returnSmaller(x360Controller.batteryLevel);

x360Controller.ControllerState.Gamepad.sThumbLX = ((x360Controller.inputBuffer[3] * 257) - 32768);
x360Controller.ControllerState.Gamepad.sThumbLY = (32767 - (x360Controller.inputBuffer[4] * 257));
x360Controller.ControllerState.Gamepad.sThumbRX = ((x360Controller.inputBuffer[5] * 257) - 32768);
x360Controller.ControllerState.Gamepad.sThumbRY = (32767 - (x360Controller.inputBuffer[6] * 257));
x360Controller.ControllerState.Gamepad.sThumbLX = ((x360Controller.inputBuffer[3 - x360Controller.bluetooth * 2] * 257) - 32768);
x360Controller.ControllerState.Gamepad.sThumbLY = (32767 - (x360Controller.inputBuffer[4 - x360Controller.bluetooth * 2] * 257));
x360Controller.ControllerState.Gamepad.sThumbRX = ((x360Controller.inputBuffer[5 - x360Controller.bluetooth * 2] * 257) - 32768);
x360Controller.ControllerState.Gamepad.sThumbRY = (32767 - (x360Controller.inputBuffer[6 - x360Controller.bluetooth * 2] * 257));

x360Controller.ControllerState.Gamepad.bLeftTrigger = x360Controller.inputBuffer[10];
x360Controller.ControllerState.Gamepad.bRightTrigger = x360Controller.inputBuffer[11];
x360Controller.ControllerState.Gamepad.bLeftTrigger = x360Controller.inputBuffer[10 - x360Controller.bluetooth * 2];
x360Controller.ControllerState.Gamepad.bRightTrigger = x360Controller.inputBuffer[11 - x360Controller.bluetooth * 2];

if (gameProfileSet) {
setButtonsGameProfile(x360Controller);
Expand All @@ -153,6 +153,7 @@ void inline getDualShock4Input(controller& x360Controller) {
setButtons(x360Controller);

}
#include <iostream>
void sendDualShock4OutputReport(controller& x360Controller) {

unsigned char outputHID[334]{};
Expand All @@ -165,14 +166,13 @@ void sendDualShock4OutputReport(controller& x360Controller) {

ZeroMemory(outputHID, 0);

outputHID[0] = 0x11;
outputHID[1] = (byte)(0xC0);

outputHID[3] = 0x07;
outputHID[4] = 0x04;
outputHID[3 - x360Controller.bluetooth * 2] = 0x07;
outputHID[4 - x360Controller.bluetooth * 2] = 0x04;

outputHID[6] = rumble[0]; //Low Rumble
outputHID[7] = rumble[1]; //High Rumble
outputHID[6 - x360Controller.bluetooth * 2] = rumble[0]; //Low Rumble
outputHID[7 - x360Controller.bluetooth * 2] = rumble[1]; //High Rumble

switch (x360Controller.batteryLevel + lightbarOpen + gameProfileSet + profileEdit) { //Chooses Lightbar Profile, if any of those editors are open then it breaks and sets itself to the currently being edited profile
case 0: x360Controller.RGB[0].Index = 1; break;
Expand All @@ -188,19 +188,27 @@ void sendDualShock4OutputReport(controller& x360Controller) {
}

for (int i = 0; i < 3; i++) {
outputHID[8 + i] = x360Controller.RGB[x360Controller.RGB[0].Index].colors[i] * 255;
outputHID[8 + i - x360Controller.bluetooth * 2] = x360Controller.RGB[x360Controller.RGB[0].Index].colors[i] * 255;
x360Controller.RGB[0].colors[i] = x360Controller.RGB[x360Controller.RGB[0].Index].colors[i];
}

//Send Output Report
const UINT32 crc = computeCRC32(outputHID, 74);
if (!x360Controller.bluetooth) {
outputHID[0] = 0x11;
const UINT32 crc = computeCRC32(outputHID, 74);

outputHID[74] = (crc & 0x000000FF);
outputHID[75] = ((crc & 0x0000FF00) >> 8UL);
outputHID[76] = ((crc & 0x00FF0000) >> 16UL);
outputHID[77] = ((crc & 0xFF000000) >> 24UL);

WriteFile(x360Controller.deviceHandle, outputHID, 334, NULL, NULL);
continue;
}
outputHID[0] = 0x05;
WriteFile(x360Controller.deviceHandle, outputHID, 68, NULL, NULL);

outputHID[74] = (crc & 0x000000FF);
outputHID[75] = ((crc & 0x0000FF00) >> 8UL);
outputHID[76] = ((crc & 0x00FF0000) >> 16UL);
outputHID[77] = ((crc & 0xFF000000) >> 24UL);

WriteFile(x360Controller.deviceHandle, outputHID, 334, NULL, NULL);

}
}

0 comments on commit 7aa1b5b

Please sign in to comment.