Skip to content

Commit

Permalink
Improvements: Added Xbox button usage and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
r57zone committed Nov 9, 2021
1 parent 007b12f commit 1133910
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
5 changes: 2 additions & 3 deletions README.RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
## Xbox контроллер
Кнопка "Назад" на Xbox контроллере эмулирует нажатие тачпада на Sony DualShock 4.

Кнопка "Поделиться" привязана на одновременное нажатие кнопок "Назад" и "Старт" или на клавишу "F12".
Кнопка "PS Поделиться" привязана на одновременное нажатие кнопок "Назад" и "Старт" и на клавишу "F12".

Кнопка "PS" привязана на одновременное нажатие кнопок "Назад" и "LB" (левый бампер) или на клавишу "F2".
Кнопка "PS" привязана на кнопку "Xbox", но для её использования нужно в настройках "Xbox Game Bar" отключить использованией этой кнопки. Также кнопка "PS" привязана на одновременное нажатие кнопок "Назад" и "LB" (левый бампер) и на клавишу "F2".

Потрясти геймпад (гироскоп) можно нажав "Back" и "RB" (правый бампер).

Expand Down Expand Up @@ -77,7 +77,6 @@ L3 (нажатие стика) | Shift
R3 (нажатие стика) | Средняя кнопка мыши
Touchpad свайп вверх, вниз, влево, вправо | 7, 8, 9, 0
Touchpad вверх, вниз, влево, вправо, вниз | U, J, H, K, N
Touchpad второе касание справа | I
Потрясти геймпад | T
PS | F2

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Simple application to emulate the Sony DualShock 4 gamepad using an Xbox control
## Xbox controller
The "Back" button on the Xbox controller emulating pressing the touchpad on a Sony DualShock 4.

The "Share" button is binded to the simultaneous pressing of the "Back" and "Start" buttons or to the "F12" key.
The "PS Share" button is binded to the simultaneous pressing of the "Back" and "Start" buttons or to the "F12" key.

The "PS" button is binded to the simultaneous pressing of the "Back" and "LB" buttons (left bumper) or to the "F2" key.
The "PS" button is binded to the "Xbox" button, but to use it, you need to disable the use of this button in the "Xbox Game Bar" settings. Also, the "PS" button is tied to the simultaneous pressing of the "Back" and "LB" buttons (left bumper) and the "F2" key.

You can shake (gyro) the controller by pressing "Back" and "RB" (right bumper).

Expand Down Expand Up @@ -78,7 +78,6 @@ L3 (pressing the stick) | Shift
R3 (pressing the stick) | Middle mouse button
Touchpad swipe up, down, left, right | 7, 8, 9, 0
Touchpad up, center, left, right, down | U, J, H, K, N
Touchpad second touch on the right | I
Shake the gamepad | T
PS | F2

Expand Down
56 changes: 25 additions & 31 deletions Source/DS4Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define XINPUT_GAMEPAD_DPAD_DOWN 0x0002
#define XINPUT_GAMEPAD_DPAD_LEFT 0x0004
#define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008
#define XINPUT_GAMEPAD_GUIDE 0x0400
#define XINPUT_GAMEPAD_START 0x0010
#define XINPUT_GAMEPAD_BACK 0x0020
#define XINPUT_GAMEPAD_LEFT_THUMB 0x0040
Expand Down Expand Up @@ -196,7 +197,7 @@ SHORT DeadZoneXboxAxis(SHORT StickAxis, float Percent)

int main(int argc, char **argv)
{
SetConsoleTitle("DS4Emulator 1.7.4");
SetConsoleTitle("DS4Emulator 1.7.5");

CIniReader IniFile("Config.ini"); // Config

Expand Down Expand Up @@ -235,6 +236,10 @@ int main(int argc, char **argv)
}
}
float MotionSens = IniFile.ReadFloat("Motion", "Sens", 100) * 0.01;
int InverseXStatus = IniFile.ReadBoolean("Motion", "InverseX", false) == false ? 1 : -1 ;
int InverseYStatus = IniFile.ReadBoolean("Motion", "InverseY", false) == false ? 1 : -1 ;
int InverseZStatus = IniFile.ReadBoolean("Motion", "InverseZ", false) == false ? 1 : -1 ;

SleepTimeOutMotion = IniFile.ReadInteger("Motion", "SleepTimeOut", 1);

#define OCR_NORMAL 32512
Expand All @@ -249,7 +254,7 @@ int main(int argc, char **argv)

// Config parameters
int KEY_ID_EXIT = IniFile.ReadInteger("Main", "ExitBtn", 192); // "~" by default for RU, US and not for UK
int KEY_ID_STOP_CENTERING = IniFile.ReadInteger("KeyboardMouse", "StopÑenteringKey", 'C');
int KEY_ID_STOP_CENTERING = IniFile.ReadInteger("KeyboardMouse", "StopCenteringKey", 'C');

bool InvertX = IniFile.ReadBoolean("Main", "InvertX", false);
bool InvertY = IniFile.ReadBoolean("Main", "InvertY", false);
Expand All @@ -265,7 +270,7 @@ int main(int argc, char **argv)
float DeadZoneRightStickY = IniFile.ReadFloat("Xbox", "DeadZoneRightStickY", 0);

int SleepTimeOutKB = IniFile.ReadInteger("KeyboardMouse", "SleepTimeOut", 2);
std::string WindowTitle = IniFile.ReadString("KeyboardMouse", "ActivateOnlyInWindow", "PlayStation™Now");
std::string WindowTitle = IniFile.ReadString("KeyboardMouse", "ActivateOnlyInWindow", "PlayStationNow");
std::string WindowTitle2 = IniFile.ReadString("KeyboardMouse", "ActivateOnlyInWindow2", "PS4 Remote Play");
int FullScreenTopOffset = IniFile.ReadInteger("KeyboardMouse", "FullScreenTopOffset", -50);
bool HideTaskBar = IniFile.ReadBoolean("KeyboardMouse", "HideTaskBarInFullScreen", true);
Expand Down Expand Up @@ -313,7 +318,6 @@ int main(int argc, char **argv)
int KEY_ID_TOUCHPAD_LEFT = IniFile.ReadInteger("Keys", "TOUCHPAD_LEFT", 'H');
int KEY_ID_TOUCHPAD_RIGHT = IniFile.ReadInteger("Keys", "TOUCHPAD_RIGHT", 'K');
int KEY_ID_TOUCHPAD_CENTER = IniFile.ReadInteger("Keys", "TOUCHPAD_CENTER", 'J');
int KEY_ID_TOUCHPAD_SECOND_RIGHT = IniFile.ReadInteger("Keys", "TOUCHPAD_SECOND_RIGHT", 'J');

const auto client = vigem_alloc();
auto ret = vigem_connect(client);
Expand All @@ -329,8 +333,9 @@ int main(int argc, char **argv)

// Load library and scan Xbox gamepads
hDll = LoadLibrary("xinput1_3.dll"); // x360ce support
if (hDll == NULL) hDll = LoadLibrary("xinput1_4.dll");
if (hDll != NULL) {
MyXInputGetState = (_XInputGetState)GetProcAddress(hDll, "XInputGetState");
MyXInputGetState = (_XInputGetState)GetProcAddress(hDll, (LPCSTR)100); // "XInputGetState"); // Ordinal 100 is the same as XInputGetState but supports the Guide button. Taken here https://github.com/speps/XInputDotNet/blob/master/XInputInterface/GamePad.cpp
MyXInputSetState = (_XInputSetState)GetProcAddress(hDll, "XInputSetState");
if (MyXInputGetState == NULL || MyXInputSetState == NULL)
hDll = NULL;
Expand Down Expand Up @@ -449,6 +454,7 @@ int main(int argc, char **argv)
myPState.Gamepad.wButtons &= ~XINPUT_GAMEPAD_BACK; myPState.Gamepad.wButtons &= ~XINPUT_GAMEPAD_LEFT_SHOULDER;
report.bSpecial |= DS4_SPECIAL_BUTTON_PS;
}
if (myPState.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) report.bSpecial |= DS4_SPECIAL_BUTTON_PS;

// Motion shaking
if (myPState.Gamepad.wButtons & XINPUT_GAMEPAD_BACK && myPState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) {
Expand Down Expand Up @@ -529,8 +535,10 @@ int main(int argc, char **argv)

// Touchpad swipes
if (report.bSpecial & DS4_SPECIAL_BUTTON_TOUCHPAD) {
if (!TouchPadPressedWhenSwiping && (report.bThumbRX != 127 || report.bThumbRY != 129) )
if (!TouchPadPressedWhenSwiping && (report.bThumbRX != 127 || report.bThumbRY != 129) ) {
report.bSpecial &= ~DS4_SPECIAL_BUTTON_TOUCHPAD;
if (myPState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) { report.wButtons &= ~DS4_BUTTON_THUMB_RIGHT; report.bSpecial |= DS4_SPECIAL_BUTTON_TOUCHPAD; }
}
TouchX = 960; TouchY = 471;
if (report.bThumbRX > 127)
TouchX = 320 + (report.bThumbRX - 127) * 10;
Expand Down Expand Up @@ -577,7 +585,7 @@ int main(int argc, char **argv)
if (InvertY)
DeltaMouseY = DeltaMouseY * -1;

// Are there better options? / Åñòü âàðèàíò ëó÷øå?
// Are there better options? / Есть вариант лучше?
if (DeltaMouseX > 0)
report.bThumbRX = 128 + trunc( Clamp(DeltaMouseX * mouseSensetiveX, 0, 127) );
if (DeltaMouseX < 0)
Expand Down Expand Up @@ -692,13 +700,6 @@ int main(int argc, char **argv)
}
}

if ((GetAsyncKeyState(KEY_ID_TOUCHPAD_SECOND_RIGHT) & 0x8000) != 0) { // Bad temporary solution for Infamous: Second Son https://youtu.be/i7w4G1CIdog?t=667
report.sCurrentTouch.bIsUpTrackingNum2 = 0;
report.sCurrentTouch.bTouchData2[0] = 1600 & 0xFF;
report.sCurrentTouch.bTouchData2[1] = ((1600 >> 8) & 0x0F) | ((471 & 0x0F) << 4);
report.sCurrentTouch.bTouchData2[2] = (471 >> 4) & 0xFF;
}

if (BuffPreviousTouch[1].bIsUpTrackingNum1 == 0) {
//printf("2: prev 2 touched\r\n");
report.sPreviousTouch[1] = BuffPreviousTouch[1];
Expand All @@ -714,12 +715,12 @@ int main(int argc, char **argv)
BuffPreviousTouch[0].bIsUpTrackingNum1 = 0x80;
}

// Probably the wrong way, but it works, temporary workaround / Âåðîÿòíî íåâåðíûé ïóòü, íî îí ðàáîòàåò, ïîäîéäåò êàê âðåìåííîå ðåøåíèå
// Probably the wrong way, but it works, temporary workaround / Вероятно неверный путь, но он работает, подойдет как временное решение
if (TouchX != 0 || TouchY != 0) {
report.bTouchPacketsN = 1;

if (AllowIncTouchIndex) {
if (TouchIndex < 255) // Is this the right way? / Âåðíûé ëè ýòî ïóòü?
if (TouchIndex < 255) // Is this the right way? / Верный ли это путь?
TouchIndex++;
else
TouchIndex = 0;
Expand All @@ -741,12 +742,12 @@ int main(int argc, char **argv)

report.sCurrentTouch.bPacketCounter = TouchIndex;

report.wAccelX = trunc( Clamp(AccelX * 1638.35, -32767, 32767) ) * 1 * MotionSens; // freepie accel max 19.61, min -20, short -32,768 to 32,767
report.wAccelY = trunc( Clamp(AccelY * 1638.35, -32767, 32767) ) * -1 * MotionSens;
report.wAccelZ = trunc( Clamp(AccelZ * 1638.35, -32767, 32767) ) * 1 * MotionSens;
report.wGyroX = trunc( Clamp(GyroX * 2376.7, -32767, 32767) ) * 1 * MotionSens; // freepie max gyro 10, min -10.09
report.wGyroY = trunc( Clamp(GyroY * 2376.7, -32767, 32767) ) * -1 * MotionSens;
report.wGyroZ = trunc( Clamp(GyroZ * 2376.7, -32767, 32767) ) * 1 * MotionSens; // if ((GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0) printf("%d\t%d\t%d\t%d\t%d\t%d\t\n", report.wAccelX, report.wAccelY, report.wAccelZ, report.wGyroX, report.wGyroY, report.wGyroZ);
report.wAccelX = trunc( Clamp(AccelX * 1638.35, -32767, 32767) ) * InverseXStatus * MotionSens; // freepie accel max 19.61, min -20, short -32,768 to 32,767
report.wAccelY = trunc( Clamp(AccelY * 1638.35, -32767, 32767) ) * InverseYStatus * MotionSens;
report.wAccelZ = trunc( Clamp(AccelZ * 1638.35, -32767, 32767) ) * InverseZStatus * MotionSens;
report.wGyroX = trunc( Clamp(GyroX * 2376.7, -32767, 32767) ) * InverseXStatus * MotionSens; // freepie max gyro 10, min -10.09
report.wGyroY = trunc( Clamp(GyroY * 2376.7, -32767, 32767) ) * InverseYStatus * MotionSens;
report.wGyroZ = trunc( Clamp(GyroZ * 2376.7, -32767, 32767) ) * InverseZStatus * MotionSens; // if ((GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0) printf("%d\t%d\t%d\t%d\t%d\t%d\t\n", report.wAccelX, report.wAccelY, report.wAccelZ, report.wGyroX, report.wGyroY, report.wGyroZ);

// Motion shaking
if (MotionShaking) {
Expand All @@ -763,17 +764,10 @@ int main(int argc, char **argv)
// Multi mode keys
if ((GetAsyncKeyState(KEY_ID_PS) & 0x8000) != 0)
report.bSpecial |= DS4_SPECIAL_BUTTON_PS;
if ((GetAsyncKeyState(KEY_ID_TOUCHPAD_SECOND_RIGHT) & 0x8000) != 0) { // Bad temporary solution for Infamous: Second Son https://youtu.be/i7w4G1CIdog?t=667
report.bTouchPacketsN = 2;
report.sCurrentTouch.bIsUpTrackingNum2 = 0;
report.sCurrentTouch.bTouchData2[0] = 1600 & 0xFF;
report.sCurrentTouch.bTouchData2[1] = ((1600 >> 8) & 0x0F) | ((471 & 0x0F) << 4);
report.sCurrentTouch.bTouchData2[2] = (471 >> 4) & 0xFF;
}

// if ((GetAsyncKeyState(VK_NUMPAD0) & 0x8000) != 0) system("cls");
curTimeStamp++; if (curTimeStamp > 65535) curTimeStamp = 0; // ?

curTimeStamp++; if (curTimeStamp > 65535) curTimeStamp = 0; // Is it right? / Правильно ли это?
report.wTimestamp = curTimeStamp;

ret = vigem_target_ds4_update_ex(client, ds4, report);
Expand Down

0 comments on commit 1133910

Please sign in to comment.