From 2e534e77b6bd14fcf93eae59560f60a5020fd550 Mon Sep 17 00:00:00 2001 From: David Miller Date: Wed, 25 Sep 2019 20:09:43 -0400 Subject: [PATCH] was waiting for swap in wrong area, hopefully fixed dualshock4 for Gamepad input --- src/SDL_error.c | 6 ++ src/SDL_log.c | 5 +- src/joystick/SDL_gamecontrollerdb.h | 16 +++ src/joystick/ps4/SDL_sysjoystick.c | 154 +++++++++++++++++++++++----- src/video/ps4/SDL_ps4video.c | 19 ++-- 5 files changed, 165 insertions(+), 35 deletions(-) diff --git a/src/SDL_error.c b/src/SDL_error.c index f3f481bd1b..ef5ff18b98 100644 --- a/src/SDL_error.c +++ b/src/SDL_error.c @@ -122,7 +122,9 @@ SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) } va_end(ap); +#ifndef PS4 // Yeah this is stupid but whatever if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) { +#endif /* If we are in debug mode, print out an error message * Avoid stomping on the static buffer in GetError, just * in case this is called while processing a ShowMessageBox to @@ -131,7 +133,11 @@ SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) char errmsg[SDL_ERRBUFIZE]; SDL_GetErrorMsg(errmsg, sizeof(errmsg)); SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg); + + printf(" FFS - %s \n", errmsg); +#ifndef PS4 } +#endif return -1; } diff --git a/src/SDL_log.c b/src/SDL_log.c index aadabe4715..67167a4985 100644 --- a/src/SDL_log.c +++ b/src/SDL_log.c @@ -267,10 +267,12 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list char *message; size_t len; +#ifndef PS4 /* Nothing to do if we don't have an output function */ if (!SDL_log_function) { return; } +#endif /* Make sure we don't exceed array bounds */ if ((int)priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) { @@ -301,8 +303,9 @@ SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list #ifdef PS4 printf(message); // just fucking do it -#endif +#else SDL_log_function(SDL_log_userdata, category, priority, message); +#endif SDL_stack_free(message); } diff --git a/src/joystick/SDL_gamecontrollerdb.h b/src/joystick/SDL_gamecontrollerdb.h index cd2b10ed89..61176a4308 100644 --- a/src/joystick/SDL_gamecontrollerdb.h +++ b/src/joystick/SDL_gamecontrollerdb.h @@ -594,6 +594,22 @@ static const char *s_ControllerMappings [] = #endif #if defined(SDL_JOYSTICK_EMSCRIPTEN) "default,Standard Gamepad,a:b0,b:b1,back:b8,dpdown:b13,dpleft:b14,dpright:b15,dpup:b12,guide:b16,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,", +#endif +#if defined(SDL_JOYSTICK_PS4) + "030000004c050000a00b000011010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "030000004c050000a00b000011810000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + "030000004c050000c405000011010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "030000004c050000c405000011810000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + "030000004c050000cc09000000010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "030000004c050000cc09000011010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "030000004c050000cc09000011810000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + "050000004c050000c405000000010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "050000004c050000c405000000810000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + "050000004c050000cc09000000010000,PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,", + "050000004c050000cc09000000810000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + "050000004c050000cc09000001800000,PS4 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b3,y:b2,", + + #endif "hidapi,*,a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,", NULL diff --git a/src/joystick/ps4/SDL_sysjoystick.c b/src/joystick/ps4/SDL_sysjoystick.c index 9759668c30..6b8e3eab1a 100644 --- a/src/joystick/ps4/SDL_sysjoystick.c +++ b/src/joystick/ps4/SDL_sysjoystick.c @@ -20,6 +20,10 @@ */ #include "../../SDL_internal.h" +#ifdef __INTELLISENSE__ +#define SDL_JOYSTICK_PS4 1 +#endif + #if SDL_JOYSTICK_PS4 /* This is the PSP implementation of the SDL joystick API */ @@ -44,23 +48,15 @@ #endif #define MAX_PADS 4 +#define DS4_BTN_COUNT 20 +#define DS4_AXE_COUNT 6 // lsX/Y, rsX/Y, L2, R2 +#define DS4_HAT_COUNT 1 uint32_t nPads = 0; SceUserServiceUserId userId, pad_users[MAX_PADS]; SceUserServiceLoginUserIdList userIdList; -#define DS4_BTN_COUNT 16 - - -const uint32_t ds4_map[DS4_BTN_COUNT] = { - SCE_PAD_BUTTON_CROSS, SCE_PAD_BUTTON_CIRCLE, SCE_PAD_BUTTON_SQUARE, SCE_PAD_BUTTON_TRIANGLE, - SCE_PAD_BUTTON_L1, SCE_PAD_BUTTON_R1, SCE_PAD_BUTTON_L2, SCE_PAD_BUTTON_R2, - SCE_PAD_BUTTON_OPTIONS, SCE_PAD_BUTTON_L3, SCE_PAD_BUTTON_R3, - SCE_PAD_BUTTON_UP, SCE_PAD_BUTTON_DOWN, SCE_PAD_BUTTON_LEFT, SCE_PAD_BUTTON_RIGHT, - SCE_PAD_BUTTON_TOUCH_PAD -}; - int PS4_JoystickGetCount(void) @@ -141,9 +137,21 @@ SDL_JoystickGUID PS4_JoystickGetDeviceGUID(int device_index) SDL_JoystickGUID guid; SDL_zero(guid); +#if 0 *(int*)&guid.data[0] = 0x20200420; *(int*)&guid.data[4] = pad_users[device_index]; *(int*)&guid.data[10] = device_index; +#if 0 + guid.data[14] = (Uint8)'h'; // use HID mapping h - 104 - 0x68 +#endif +#else + + ((Uint32*)guid.data)[0] = 0x00000005; // 05000000; + ((Uint32*)guid.data)[0] = 0x00004c05; // 4c050000; + ((Uint32*)guid.data)[0] = 0x000009cc; // cc090000; + ((Uint32*)guid.data)[0] = 0x00008001; // 01800000; + // "050000004c050000cc09000001800000,PS4 Controller, +#endif return guid; } @@ -175,9 +183,9 @@ int PS4_JoystickOpen(SDL_Joystick *joystick, int device_index) int32_t handle = PS4_JoystickGetDeviceInstanceID(device_index); // They are already open , if (handle > 0) { - joystick->nbuttons = 16; - joystick->naxes = 2; - joystick->nhats = 0; + joystick->nbuttons = DS4_BTN_COUNT; + joystick->naxes = DS4_AXE_COUNT; + joystick->nhats = DS4_HAT_COUNT; /* Create the joystick data structure */ joystick->instance_id = handle; @@ -188,10 +196,11 @@ int PS4_JoystickOpen(SDL_Joystick *joystick, int device_index) } SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); #endif + return 0; } + printf("PS4_JoystickOpen(idx: %d) failed to get handled!\n", device_index); - - return 0; + return -1; } int PS4_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) @@ -209,9 +218,6 @@ int PS4_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uin */ void PS4_JoystickUpdate(SDL_Joystick *joystick) { - static uint32_t s_btns=0; - uint32_t m_btns=0; - ScePadData data; // printf("(((((((((( JS UPDATE %X ))))))))) \n", joystick->instance_id); @@ -222,18 +228,46 @@ void PS4_JoystickUpdate(SDL_Joystick *joystick) return; } + + + + //a:b0,b:b1,x:b3,y:b2,leftshoulder:b4,rightshoulder:b5 + //back:b8,start:b9,guide:b10,leftstick:b11,rightstick:b12, + //dpdown:h0.4, dpleft:h0.8, dpright:h0.2, dpup:h0.1, + //leftx:a0,lefty:a1,lefttrigger:a2,rightx:a3,righty:a4,righttrigger:a5, + + const static uint32_t ds4_map[DS4_BTN_COUNT] = { + SCE_PAD_BUTTON_CROSS, SCE_PAD_BUTTON_CIRCLE, SCE_PAD_BUTTON_SQUARE, SCE_PAD_BUTTON_TRIANGLE, SCE_PAD_BUTTON_L1, SCE_PAD_BUTTON_R1, + 0, /* b6? */ 0, /* b7? */ 0, /* no share/back btn atm */ + SCE_PAD_BUTTON_OPTIONS, 0 /* no guide button atm */, SCE_PAD_BUTTON_L3, SCE_PAD_BUTTON_R3, + SCE_PAD_BUTTON_UP, SCE_PAD_BUTTON_DOWN, SCE_PAD_BUTTON_LEFT, SCE_PAD_BUTTON_RIGHT, + SCE_PAD_BUTTON_TOUCH_PAD, SCE_PAD_BUTTON_L2, SCE_PAD_BUTTON_R2, + }; + +#if 0 + static uint32_t s_btns=0; + uint32_t m_btns=0; + m_btns = data.buttons ^ s_btns; s_btns = data.buttons; +#else + Uint32 s_btns = data.buttons; +#endif - if (m_btns) { +// if (m_btns) { for (uint32_t bn = 0; bn < DS4_BTN_COUNT; bn++) { - if (m_btns & ds4_map[bn]) { - if (!SDL_PrivateJoystickButton(joystick, bn, s_btns & ds4_map[bn] ? SDL_PRESSED : SDL_RELEASED)) - ; // printf("$$$$$$$$$$$$$$ UPDATE BUTTON %d NOT POSTED \n", bn); - } + // if (m_btns & ds4_map[bn]) { + //if (! + SDL_PrivateJoystickButton(joystick, bn, (s_btns & ds4_map[bn]) ? SDL_PRESSED : SDL_RELEASED); + // printf("$$$$$$$$$$$$$$ UPDATE BUTTON %d NOT POSTED \n", bn); + // } } - } - +// } + +#define aS16(axe) -32768 + ((axe) << 8) + +#define setA(n,axe) { Sint16 tmp = aS16(axe); if (tmp < -1000 || tmp > 1000) { SDL_PrivateJoystickAxis(joystick, (n), tmp); } } +#if 0 Sint16 lX = -32768 + (data.leftStick.x << 8); //-128 + data.leftStick.x; Sint16 lY = -32768 + (data.leftStick.y << 8); //-128 + data.leftStick.y; @@ -243,11 +277,75 @@ void PS4_JoystickUpdate(SDL_Joystick *joystick) // **FIXME** get actual deadzone and scale properly // if (lX < -1000 || lX > 1000) SDL_PrivateJoystickAxis(joystick, 0, lX); if (lY < -1000 || lY > 1000) SDL_PrivateJoystickAxis(joystick, 1, lY); - if (rX < -1000 || rX > 1000) SDL_PrivateJoystickAxis(joystick, 2, rX); - if (rY < -1000 || rY > 1000) SDL_PrivateJoystickAxis(joystick, 3, rY); + // L2.analogue data.analogButtons.l2; + if (rX < -1000 || rX > 1000) SDL_PrivateJoystickAxis(joystick, 4, rX); + if (rY < -1000 || rY > 1000) SDL_PrivateJoystickAxis(joystick, 5, rY); + // R2.analogue data.analogButtons.r2; +#endif + + setA(0, data.leftStick.x); + setA(1, data.leftStick.y); + setA(2, data.analogButtons.l2); + setA(4, data.rightStick.x); + setA(5, data.rightStick.y); + setA(6, data.analogButtons.r2); + + Uint8 hat = SDL_HAT_CENTERED; + if (s_btns & SCE_PAD_BUTTON_UP) hat |= SDL_HAT_UP; + if (s_btns & SCE_PAD_BUTTON_DOWN) hat |= SDL_HAT_DOWN; + if (s_btns & SCE_PAD_BUTTON_LEFT) hat |= SDL_HAT_LEFT; + if (s_btns & SCE_PAD_BUTTON_RIGHT) hat |= SDL_HAT_RIGHT; + SDL_PrivateJoystickHat(joystick, 0, hat); } +#if 0 + + static WORD s_XInputButtons[] = { + XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y, + XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_START, + XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB, + XINPUT_GAMEPAD_GUIDE + }; + WORD wButtons = pXInputState->Gamepad.wButtons; + Uint8 button; + Uint8 hat = SDL_HAT_CENTERED; + + SDL_PrivateJoystickAxis(joystick, 0, pXInputState->Gamepad.sThumbLX); + SDL_PrivateJoystickAxis(joystick, 1, ~pXInputState->Gamepad.sThumbLY); + SDL_PrivateJoystickAxis(joystick, 2, ((int)pXInputState->Gamepad.bLeftTrigger * 257) - 32768); + SDL_PrivateJoystickAxis(joystick, 3, pXInputState->Gamepad.sThumbRX); + SDL_PrivateJoystickAxis(joystick, 4, ~pXInputState->Gamepad.sThumbRY); + SDL_PrivateJoystickAxis(joystick, 5, ((int)pXInputState->Gamepad.bRightTrigger * 257) - 32768); + + for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) { + SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED); + } + + if (wButtons & XINPUT_GAMEPAD_DPAD_UP) { + hat |= SDL_HAT_UP; + } + if (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) { + hat |= SDL_HAT_DOWN; + } + if (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) { + hat |= SDL_HAT_LEFT; + } + if (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) { + hat |= SDL_HAT_RIGHT; + } + SDL_PrivateJoystickHat(joystick, 0, hat); +#endif + + + + + + + + + + /* Function to close a joystick after use */ void PS4_JoystickClose(SDL_Joystick *joystick) { diff --git a/src/video/ps4/SDL_ps4video.c b/src/video/ps4/SDL_ps4video.c index 6670ea45c7..ead8de971e 100644 --- a/src/video/ps4/SDL_ps4video.c +++ b/src/video/ps4/SDL_ps4video.c @@ -21,6 +21,9 @@ #include "../../SDL_internal.h" +#if __INTELLISENSE__ +#define SDL_VIDEO_DRIVER_PS4 1 +#endif #if SDL_VIDEO_DRIVER_PS4 @@ -75,7 +78,7 @@ INLINE static uint8_t* CurrentBuffer(_THIS) { return GetBuffer(_this, VData->cur INLINE static uint8_t* NextBuffer(_THIS) { return GetBuffer(_this, ((VData->currBuffer + 1) % VOUT_NUM_BUFFERS)); } - +#if 1 INLINE static int32_t IsFlipPending(_THIS) { return sceVideoOutIsFlipPending(Handle(_this)) > 0; } @@ -89,14 +92,14 @@ INLINE static void WaitOnFlip(_THIS) { //); // assert } } - +#else //INLINE bool GetFlipStatus(_THIS,SceVideoOutFlipStatus *status) { // return SCE_OK == sceVideoOutGetFlipStatus(Handle(), status); //} // sceVideoOutGetResolutionStatus -#if 0 -INLINE static void WaitOnFlip(size_t arg) { + +INLINE static void WaitOnFlip(_THIS, size_t arg) { int out = 0; SceKernelEvent ev; SceVideoOutFlipStatus status; @@ -105,7 +108,9 @@ INLINE static void WaitOnFlip(size_t arg) { GetFlipStatus(&status); if (status.flipArg >= arg) return; - (SCE_OK == sceKernelWaitEqueue(flipQueue, &ev, 1, &out, 0)); // assert + if (SCE_OK != sceKernelWaitEqueue(VData->flipQueue, &ev, 1, &out, 0)) { + printf("ERROR sceKernelWaitEqueue() failed!\n"); return; + }; // assert } } #endif @@ -119,6 +124,8 @@ INLINE static void SubmitFlip(_THIS) //s64 buffer = -1, u64 arg = 0) //printf("SubmitFlip() Buffer[%d] %p \n", currBuffer, (u8*)CurrentBuffer()); VData->currBuffer = ((VData->currBuffer + 1) % BufferCount(_this)); + + WaitOnFlip(_this); } @@ -211,7 +218,7 @@ static int vout_init(_THIS) VData->attr.width = VData->width; VData->attr.height = VData->height; VData->attr.aspectRatio = SCE_VIDEO_OUT_ASPECT_RATIO_16_9; - VData->attr.pixelFormat = SCE_VIDEO_OUT_PIXEL_FORMAT_A8R8G8B8_SRGB; + VData->attr.pixelFormat = SCE_VIDEO_OUT_PIXEL_FORMAT_A8B8G8R8_SRGB; VData->attr.tilingMode = SCE_VIDEO_OUT_TILING_MODE_LINEAR; // (!tile) ? : 0; // change to tiled later, GpuMode must be NEO for pro VData->attr.pitchInPixel= VData->width;