Merge branch 'features' of https://github.com/SysRay/psOff_public into work

This commit is contained in:
Martin Baliet 2024-03-17 12:19:19 +01:00
commit 920d3a38b7
31 changed files with 584 additions and 80 deletions

24
.vscode/settings.json vendored
View File

@ -15,19 +15,25 @@
],
"ccls.index.threads": 0,
"cmake.buildDirectory": "${workspaceFolder}/_build",
"cmake.installPrefix": "_Install",
"cmake.sourceDirectory": "${workspaceFolder}",
"C_Cpp.default.intelliSenseMode": "windows-clang-x64",
"C_Cpp.default.cppStandard": "c++20",
"C_Cpp.autoAddFileAssociations": false,
"cmake.generator": "Ninja",
"cmake.generator": "Ninja",
"cmake.sourceDirectory": "${workspaceFolder}",
"cmake.buildDirectory": "${workspaceFolder}/_build/_Release",
"cmake.configureSettings": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_INSTALL_PREFIX": "${workspaceFolder}/_build/_Install",
"CMAKE_C_COMPILER": "clang-cl.exe",
"CMAKE_CXX_COMPILER": "clang-cl.exe"
},
"C_Cpp.default.compilerPath": "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\Llvm\\bin\\clang-cl.exe",
"C_Cpp.default.intelliSenseMode": "windows-clang-x64",
"C_Cpp.default.cppStandard": "c++20",
"C_Cpp.autoAddFileAssociations": false,
"editor.tabSize": 2,
"editor.insertSpaces": true,
"git.ignoredRepositories": [
"third_party/boost",
"third_party/optick",
"third_party/magic_enum",
"third_part/SDL2"
]
"third_part/SDL2"
]
}

View File

@ -37,9 +37,9 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # Move all libs here post-build, ex
#
include_directories(BEFORE
${CMAKE_INSTALL_PREFIX}/development/include
${PRJ_SRC_DIR}/third_party/SDL2/include
${PRJ_SRC_DIR}/modules_include
${PRJ_SRC_DIR}/tools/logging
${PRJ_SRC_DIR}/tools/config
${PRJ_SRC_DIR}
${CMAKE_BINARY_DIR}/third_party/install/include
)
@ -52,6 +52,7 @@ link_directories(BEFORE
)
add_subdirectory(tools/logging) # include before link_libraries
add_subdirectory(tools/config) # include before link_libraries
add_subdirectory(tools/dll2Nids)
add_dependencies(dll2Nids third_party)

View File

@ -353,8 +353,50 @@ size_t readv(int handle, const SceKernelIovec* iov, int iovcnt) {
size_t writev(int handle, const SceKernelIovec* iov, int iovcnt) {
LOG_USE_MODULE(filesystem);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
if (handle < FILE_DESCRIPTOR_MIN) {
if (handle == 0) {
size_t count = 0;
for (int n = 0; n < iovcnt; n++) {
count += ::fwrite(iov[n].iov_base, 1, iov[n].iov_len, stdout);
}
return count;
}
return getErr(ErrCode::_EPERM);
}
auto file = accessFileManager().getFile(handle);
if (file == nullptr) {
return getErr(ErrCode::_EBADF);
}
if (!(*file)) {
LOG_TRACE(L"file end");
return 0;
}
size_t count = 0;
for (int n = 0; n < iovcnt; n++) {
auto* item = &iov[n];
if (item->iov_base == nullptr || item->iov_len == 0) continue;
auto const start = file->tellp();
file->write((const char*)item->iov_base, item->iov_len);
LOG_TRACE(L"KernelWrite[%d]: 0x%08llx:%llu write(%lld)", handle, (uint64_t)item->iov_base, item->iov_len, start);
count = file->tellp() - start;
if (!(*file)) {
LOG_TRACE(L"file end");
break;
}
}
return count;
}
int fchmod(int fd, SceKernelMode mode) {
@ -624,4 +666,4 @@ int64_t lwfsLseek(int fd, int64_t offset, int whence) {
size_t lwfsWrite(int fd, const void* buf, size_t nbytes) {
return Ok;
}
} // namespace filesystem
} // namespace filesystem

View File

@ -9,4 +9,5 @@ target_include_directories(videoout PRIVATE
${Vulkan_INCLUDE_DIRS}
${PRJ_SRC_DIR}/third_party/optick/src
${PRJ_SRC_DIR}/third_party/magic_enum/include
)
${PRJ_SRC_DIR}/third_party/SDL2/include
)

View File

@ -8,6 +8,10 @@ add_library(${libName} SHARED entry.cpp)
add_dependencies(${libName} third_party)
target_link_libraries(${libName} PRIVATE SDL2)
target_link_libraries(${libName} PRIVATE config.lib SDL2 libboost_thread)
target_compile_definitions(${libName} PRIVATE BOOST_ALL_NO_LIB WIN32_LEAN_AND_MEAN)
target_include_directories(${libName} PRIVATE
${PRJ_SRC_DIR}/third_party/SDL2/include
)
setupModule(${libName})

View File

@ -1,5 +1,6 @@
#include "common.h"
#include "logging.h"
#include "tools/config/config.h"
#include "types.h"
#include <SDL.h>
@ -22,10 +23,13 @@ struct PortOut {
int channelsNum = 0;
int volume[8] = {};
SDL_AudioDeviceID device = 0;
SDL_AudioFormat sdlFormat = AUDIO_F32;
std::vector<uint8_t> mixedAudio;
float volumeModifier;
};
struct Pimpl {
std::mutex mutexInt;
boost::mutex mutexInt;
std::array<PortOut, PORT_OUT_MAX> portsOut;
Pimpl() = default;
};
@ -39,15 +43,25 @@ int writeOut(Pimpl* pimpl, int32_t handle, const void* ptr) {
auto& port = pimpl->portsOut[handle - 1];
if (!port.open || ptr == nullptr) return 0;
port.lastOutputTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
const size_t bytesize = port.samplesNum * port.sampleSize * port.channelsNum;
port.lastOutputTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
const size_t bytesize_1ch = port.samplesNum * port.sampleSize;
const size_t bytesize = bytesize_1ch * port.channelsNum;
const int maxVolume = SDL_MIX_MAXVOLUME * port.volumeModifier;
auto& mixed = port.mixedAudio;
std::fill(mixed.begin(), mixed.end(), 0);
for (size_t i = 0; i < port.channelsNum; i++) {
auto ch_offset = i * bytesize_1ch;
SDL_MixAudioFormat(mixed.data() + ch_offset, ((const uint8_t*)ptr) + ch_offset, port.sdlFormat, bytesize_1ch,
maxVolume * ((float)port.volume[i] / SCE_AUDIO_VOLUME_0DB));
}
if (SDL_GetAudioDeviceStatus(port.device) != SDL_AUDIO_PLAYING) SDL_PauseAudioDevice(port.device, 0);
while (SDL_GetQueuedAudioSize(port.device) > bytesize * 2)
SDL_Delay(0);
return SDL_QueueAudio(port.device, ptr, bytesize);
return SDL_QueueAudio(port.device, mixed.data(), bytesize);
}
} // namespace
@ -65,7 +79,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutOpen(int32_t userId, SceAudioOutPortType type
LOG_TRACE(L"%S", __FUNCTION__);
auto pimpl = getData();
std::unique_lock const lock(pimpl->mutexInt);
boost::unique_lock const lock(pimpl->mutexInt);
for (int id = 0; id < pimpl->portsOut.size(); id++) {
auto& port = pimpl->portsOut[id];
@ -79,67 +93,87 @@ EXPORT SYSV_ABI int32_t sceAudioOutOpen(int32_t userId, SceAudioOutPortType type
port.format = format;
port.lastOutputTime = 0;
SDL_AudioFormat sampleFormat;
switch (format) {
case SceAudioOutParamFormat::S16_MONO:
sampleFormat = AUDIO_S16SYS;
port.sdlFormat = AUDIO_S16;
port.channelsNum = 1;
port.sampleSize = 2;
break;
case SceAudioOutParamFormat::FLOAT_MONO:
sampleFormat = AUDIO_F32SYS;
port.sdlFormat = AUDIO_F32;
port.channelsNum = 1;
port.sampleSize = 4;
break;
case SceAudioOutParamFormat::S16_STEREO:
sampleFormat = AUDIO_S16SYS;
port.sdlFormat = AUDIO_S16;
port.channelsNum = 2;
port.sampleSize = 2;
break;
case SceAudioOutParamFormat::FLOAT_STEREO:
sampleFormat = AUDIO_F32SYS;
port.sdlFormat = AUDIO_F32;
port.channelsNum = 2;
port.sampleSize = 4;
break;
case SceAudioOutParamFormat::S16_8CH:
sampleFormat = AUDIO_S16SYS;
port.sdlFormat = AUDIO_S16;
port.channelsNum = 8;
port.sampleSize = 2;
break;
case SceAudioOutParamFormat::FLOAT_8CH:
sampleFormat = AUDIO_F32SYS;
port.sdlFormat = AUDIO_F32;
port.channelsNum = 8;
port.sampleSize = 4;
break;
case SceAudioOutParamFormat::S16_8CH_STD:
sampleFormat = AUDIO_S16SYS;
port.sdlFormat = AUDIO_S16;
port.channelsNum = 8;
port.sampleSize = 2;
break;
case SceAudioOutParamFormat::FLOAT_8CH_STD:
sampleFormat = AUDIO_F32SYS;
port.sdlFormat = AUDIO_F32;
port.channelsNum = 8;
port.sampleSize = 4;
break;
}
for (int i = 0; i < port.channelsNum; i++) {
port.volume[i] = 32768;
}
SDL_AudioSpec fmt {.freq = static_cast<int>(freq),
.format = sampleFormat,
.format = port.sdlFormat,
.channels = static_cast<uint8_t>(port.channelsNum),
.samples = static_cast<uint16_t>(port.samplesNum),
.callback = nullptr,
.userdata = nullptr};
char* dname;
SDL_AudioSpec fmt_curr;
SDL_GetDefaultAudioInfo(&dname, &fmt_curr, 0);
const char* dname;
auto [lock, jData] = accessConfig()->accessModule(ConfigSaveFlags::AUDIO);
try {
(*jData)["volume"].get_to(port.volumeModifier);
} catch (const json::exception& e) {
LOG_ERR(L"Invalid audio volume setting: %S", e.what());
}
for (int i = 0; i < port.channelsNum; i++) {
port.volume[i] = SCE_AUDIO_VOLUME_0DB;
}
if ((*jData)["device"] == "[default]") {
SDL_AudioSpec fmt_curr;
SDL_GetDefaultAudioInfo((char**)&dname, &fmt_curr, 0);
} else {
try {
std::string jdname;
(*jData)["device"].get_to(jdname);
dname = jdname.c_str();
} catch (const json::exception& e) {
LOG_ERR(L"Invalid audio device name: %S", e.what());
dname = NULL;
}
}
LOG_INFO(L"Opening audio device: %S\n", dname);
auto devId = SDL_OpenAudioDevice(dname, 0, &fmt, NULL, 0);
if (devId <= 0) return devId;
port.mixedAudio.resize(port.sampleSize * port.samplesNum * port.channelsNum);
SDL_PauseAudioDevice(devId, 0);
port.device = devId;
@ -153,13 +187,14 @@ EXPORT SYSV_ABI int32_t sceAudioOutClose(int32_t handle) {
LOG_TRACE(L"%S", __FUNCTION__);
auto pimpl = getData();
std::unique_lock const lock(pimpl->mutexInt);
boost::unique_lock const lock(pimpl->mutexInt);
auto& port = pimpl->portsOut[handle - 1];
if (port.open) {
port.open = false;
if (port.device > 0) {
port.mixedAudio.clear();
SDL_CloseAudioDevice(port.device);
}
}
@ -169,7 +204,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutClose(int32_t handle) {
EXPORT SYSV_ABI int32_t sceAudioOutOutput(int32_t handle, const void* ptr) {
auto pimpl = getData();
// std::unique_lock const lock(pimpl->mutexInt);
// boost::unique_lock const lock(pimpl->mutexInt);
return writeOut(pimpl, handle, ptr);
}
@ -177,7 +212,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutSetVolume(int32_t handle, int32_t flag, int32
LOG_USE_MODULE(libSceAudioOut);
auto pimpl = getData();
std::unique_lock const lock(pimpl->mutexInt);
boost::unique_lock const lock(pimpl->mutexInt);
auto& port = pimpl->portsOut[handle - 1];
if (!port.open) return Err::INVALID_PORT;
@ -205,7 +240,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutSetVolume(int32_t handle, int32_t flag, int32
EXPORT SYSV_ABI int32_t sceAudioOutOutputs(SceAudioOutOutputParam* param, uint32_t num) {
auto pimpl = getData();
// std::unique_lock const lock(pimpl->mutexInt); // dont block, causes audio artifacts
// boost::unique_lock const lock(pimpl->mutexInt); // dont block, causes audio artifacts
for (uint32_t i = 0; i < num; i++) {
if (auto err = writeOut(pimpl, param[i].handle, param[i].ptr); err != 0) return err;
}
@ -215,7 +250,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutOutputs(SceAudioOutOutputParam* param, uint32
EXPORT SYSV_ABI int32_t sceAudioOutGetLastOutputTime(int32_t handle, uint64_t* outputTime) {
auto pimpl = getData();
std::unique_lock const lock(pimpl->mutexInt);
boost::unique_lock const lock(pimpl->mutexInt);
auto& port = pimpl->portsOut[handle - 1];
if (!port.open) return Err::INVALID_PORT;
@ -230,7 +265,7 @@ EXPORT SYSV_ABI int32_t sceAudioOutSetMixLevelPadSpk(int32_t handle, int32_t mix
EXPORT SYSV_ABI int32_t sceAudioOutGetPortState(int32_t handle, SceAudioOutPortState* state) {
auto pimpl = getData();
std::unique_lock const lock(pimpl->mutexInt);
boost::unique_lock const lock(pimpl->mutexInt);
auto& port = pimpl->portsOut[handle - 1];

View File

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.24)
include(../setupModule.cmake)
set(libName libSceErrorDialog)
project(${libName})
add_library(${libName} SHARED entry.cpp)
setupModule(${libName})

View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
namespace Err {} // namespace Err

View File

@ -0,0 +1,38 @@
#include "codes.h"
#include "common.h"
#include "logging.h"
#include "types.h"
LOG_DEFINE_MODULE(libSceErrorDialog);
namespace {
static SceCommonDialogStatus g_curStatus = SceCommonDialogStatus::NONE;
} // namespace
extern "C" {
EXPORT const char* MODULE_NAME = "libSceErrorDialog";
EXPORT SYSV_ABI int32_t sceErrorDialogInitialize() {
g_curStatus = SceCommonDialogStatus::INITIALIZED;
return Ok;
}
EXPORT SYSV_ABI int32_t sceErrorDialogTerminate(void) {
g_curStatus = SceCommonDialogStatus::NONE;
return Ok;
}
EXPORT SYSV_ABI SceCommonDialogStatus sceErrorDialogUpdateStatus(void) {
return g_curStatus;
}
EXPORT SYSV_ABI SceCommonDialogStatus sceErrorDialogGetStatus(void) {
return g_curStatus;
}
EXPORT SYSV_ABI int32_t sceSaveDataDialogOpen(const void* param) {
g_curStatus = SceCommonDialogStatus::FINISHED;
return Ok;
}
}

View File

@ -0,0 +1,3 @@
#pragma once
#include "..\libSceCommonDialog\types.h"
#include "codes.h"

View File

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.24)
include(../setupModule.cmake)
set(libName libSceHttp2)
project(${libName})
add_library(${libName} SHARED entry.cpp)
setupModule(${libName})

View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
namespace Err {} // namespace Err

View File

@ -0,0 +1,15 @@
#include "codes.h"
#include "common.h"
#include "logging.h"
#include "types.h"
LOG_DEFINE_MODULE(libSceHttp2);
extern "C" {
EXPORT const char* MODULE_NAME = "libSceHttp2";
EXPORT SYSV_ABI int32_t sceHttp2Init() {
return Ok;
}
}

View File

@ -0,0 +1,2 @@
#pragma once
#include "codes.h"

View File

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.24)
include(../setupModule.cmake)
set(libName libSceJson2)
project(${libName})
add_library(${libName} SHARED entry.cpp)
setupModule(${libName})

View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
namespace Err {} // namespace Err

View File

@ -0,0 +1,107 @@
#include "codes.h"
#include "common.h"
#include "logging.h"
#include "types.h"
LOG_DEFINE_MODULE(libSceJson2);
extern "C" {
EXPORT const char* MODULE_NAME = "libSceJson";
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json12MemAllocatorC2Ev)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5ValueC1Ev)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setEb)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setEl)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setEm)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setEd)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json6StringC1EPKc)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setERKNS0_6StringE)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json6StringD1Ev)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json5Value3setENS0_9ValueTypeE)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID_HEX(5923AE81EE48B028)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID_HEX(12EF798E6AA7E51C)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID_HEX(236402F0F6212566)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json11InitializerC1Ev)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID(_ZN3sce4Json11Initializer10initializeEPKNS0_14InitParameter2E)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
EXPORT SYSV_ABI int32_t __NID_HEX(F9DAC3172012EAEE)() {
LOG_USE_MODULE(libSceJson2);
LOG_ERR(L"todo %S", __FUNCTION__);
return Ok;
}
}

View File

@ -0,0 +1,2 @@
#pragma once
#include "codes.h"

View File

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.24)
include(../setupModule.cmake)
set(libName libSceNpWebApi2)
project(${libName})
add_library(${libName} SHARED entry.cpp)
setupModule(${libName})

View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
namespace Err {} // namespace Err

View File

@ -0,0 +1,15 @@
#include "codes.h"
#include "common.h"
#include "logging.h"
#include "types.h"
LOG_DEFINE_MODULE(libSceNpWebApi2);
extern "C" {
EXPORT const char* MODULE_NAME = "libSceNpWebApi2";
EXPORT SYSV_ABI int32_t sceNpWebApi2Initialize() {
return Ok;
}
}

View File

@ -0,0 +1,2 @@
#pragma once
#include "codes.h"

View File

@ -8,5 +8,8 @@ add_library(${libName} SHARED entry.cpp)
add_dependencies(${libName} core)
target_link_libraries(${libName} PRIVATE SDL2 ${Vulkan_LIBRARIES} core.lib)
target_include_directories(${libName} PRIVATE
${PRJ_SRC_DIR}/third_party/SDL2/include
)
setupModule(${libName})

View File

@ -33,16 +33,17 @@ static auto* getData() {
}
static SDL_GameController* setupPad(int n, int userId) {
auto pData = getData();
auto padPtr = SDL_GameControllerOpen(n);
if (padPtr == nullptr) return nullptr;
auto pData = getData();
auto pController = SDL_GameControllerOpen(n);
if (pController == nullptr) return nullptr;
SDL_GameControllerSetPlayerIndex(padPtr, userId - 1);
SDL_GameControllerSetPlayerIndex(pController, userId - 1);
if (userId > 0) pData->controller[n].userId = userId;
pData->controller[n].prePadData = ScePadData();
pData->controller[n].padPtr = padPtr;
pData->controller[n].padPtr = pController;
++pData->controller[n].countConnect;
return padPtr;
return pController;
}
uint32_t getButtons(SDL_GameController* pad) {
@ -89,7 +90,6 @@ ScePadData getPadData(int handle) {
if (pController == nullptr) return ScePadData();
auto lockSDL2 = accessVideoOut().getSDLLock();
// SDL_GameControllerTouch
data = ScePadData {
.buttons = getButtons(pController),
@ -151,14 +151,34 @@ ScePadData getPadData(int handle) {
data.touchData.touchNum = 0;
for (int f = 0; f < SDL_GameControllerGetNumTouchpadFingers(pController, 0); f++) {
uint8_t s = 0;
float x = 0.0f, y = 0.0f;
auto& touch = data.touchData.touch[f];
float x = 0.0f, y = 0.0f, p = 0.0f;
SDL_GameControllerGetTouchpadFinger(pController, 0, f, &s, &x, &y, NULL);
if (s > 0) {
touch.x = x * 0xFFFF, touch.y = y * 0xFFFF;
++data.touchData.touchNum;
SDL_GameControllerGetTouchpadFinger(pController, 0, f, NULL, &x, &y, &p);
if (p > 0) {
auto& touch = data.touchData.touch[data.touchData.touchNum++];
touch.x = x * 0xFFFF, touch.y = y * 0xFFFF, touch.id = f;
}
}
if (SDL_GameControllerIsSensorEnabled(pController, SDL_SENSOR_GYRO)) {
float gdata[3];
if (SDL_GameControllerGetSensorData(pController, SDL_SENSOR_GYRO, gdata, 3) == 0) {
auto cr = std::cosf(gdata[2] * 0.5f);
auto sr = std::sinf(gdata[2] * 0.5f);
auto cp = std::cosf(gdata[0] * 0.5f);
auto sp = std::sinf(gdata[0] * 0.5f);
auto cy = std::cosf(gdata[1] * 0.5f);
auto sy = std::sinf(gdata[1] * 0.5f);
data.orientation = {
.x = sr * cp * cy - cr * sp * sy, .y = cr * sp * cy + sr * cp * sy, .z = cr * cp * sy - sr * sp * cy, .w = cr * cp * cy + sr * sp * sy};
}
}
if (SDL_GameControllerIsSensorEnabled(pController, SDL_SENSOR_ACCEL)) {
float adata[3];
if (SDL_GameControllerGetSensorData(pController, SDL_SENSOR_ACCEL, adata, 3) == 0) {
data.acceleration = {.x = adata[0], .y = adata[1], .z = adata[2]};
}
}
@ -173,11 +193,15 @@ EXPORT const char* MODULE_NAME = "libScePad";
EXPORT SYSV_ABI int scePadInit(void) {
LOG_USE_MODULE(libScePad);
int32_t ret = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) == 0 ? Ok : Err::FATAL;
int32_t ret = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC | SDL_INIT_SENSOR) == 0;
if (SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt") < 0) {
LOG_WARN(L"Failed to load game controller mappings");
}
return ret;
SDL_JoystickEventState(SDL_ENABLE);
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
return ret ? Ok : Err::FATAL;
}
EXPORT SYSV_ABI int scePadOpen(int32_t userId, PadPortType type, int32_t index, const void* pParam) {
@ -238,12 +262,11 @@ EXPORT SYSV_ABI int scePadRead(int32_t handle, ScePadData* pPadData, int32_t num
LOG_USE_MODULE(libScePad);
if (handle < 0) return Err::INVALID_HANDLE;
auto pData = getData();
auto pController = &pData->controller[handle];
auto pSDLController = pController->padPtr;
if (SDL_GameControllerGetAttached(pSDLController) == false) {
SDL_GameControllerClose(pSDLController);
if ((pSDLController = setupPad(handle, 0)) == nullptr) return Err::DEVICE_NOT_CONNECTED;
auto pData = getData();
auto pController = pData->controller[handle].padPtr;
if (SDL_GameControllerGetAttached(pController) == false) {
SDL_GameControllerClose(pController);
if ((pController = setupPad(handle, 0)) == nullptr) return Err::DEVICE_NOT_CONNECTED;
}
std::unique_lock const lock(pData->m_mutexInt);
@ -264,7 +287,21 @@ EXPORT SYSV_ABI int scePadReadState(int32_t handle, ScePadData* pData) {
}
EXPORT SYSV_ABI int scePadSetMotionSensorState(int32_t handle, bool bEnable) {
LOG_USE_MODULE(libScePad);
if (handle < 0) return Err::INVALID_HANDLE;
auto pData = getData();
auto pController = pData->controller[handle].padPtr;
if (SDL_GameControllerHasSensor(pController, SDL_SENSOR_GYRO)) {
if (SDL_GameControllerSetSensorEnabled(pController, SDL_SENSOR_GYRO, (SDL_bool)bEnable) == 0 &&
SDL_GameControllerSetSensorEnabled(pController, SDL_SENSOR_ACCEL, (SDL_bool)bEnable) == 0) {
LOG_INFO(L"Gyroscope sensor %S successfully!", bEnable ? "enabled" : "disabled");
} else {
LOG_WARN(L"Failed to enable the gyroscope sensor: %S", SDL_GetError());
}
}
return Ok;
}

View File

@ -24,7 +24,7 @@ enum class ScePadButtonDataOffset : uint32_t {
CIRCLE,
CROSS,
SQUARE,
TOUCH_PAD,
TOUCH_PAD = 20,
INTERCEPTED,
};
@ -291,4 +291,4 @@ struct ScePadDeviceClassExtendedInformation {
uint8_t data[12];
} classData;
};
};

View File

@ -105,6 +105,25 @@ EXPORT SYSV_ABI int sceKernelInternalMemoryGetModuleSegmentInfo(ModulInfo* info)
return Ok;
}
EXPORT SYSV_ABI int sceKernelConvertUtcToLocaltime(time_t time, time_t* local_time, struct SceTimesec* st, unsigned long* dstsec) {
const auto* tz = std::chrono::current_zone();
auto sys_inf = tz->get_info(std::chrono::system_clock::now());
*local_time = sys_inf.offset.count() + sys_inf.save.count() * 60 + time;
if (st != nullptr) {
st->t = time;
st->westsec = sys_inf.offset.count() * 60;
st->dstsec = sys_inf.save.count() * 60;
}
if (dstsec != nullptr) {
*dstsec = sys_inf.save.count() * 60;
}
return 0;
}
EXPORT SYSV_ABI unsigned int sceKernelSleep(unsigned int seconds) {
boost::this_thread::sleep_for(boost::chrono::seconds(seconds));
return Ok;
@ -365,4 +384,4 @@ EXPORT SYSV_ABI int __NID(getrusage)(rusageWho who, rusage_t* usage) {
*usage = rusage_t();
return Ok;
}
}
}

View File

@ -74,6 +74,10 @@ EXPORT SYSV_ABI size_t __NID(_readv)(int handle, const filesystem::SceKernelIove
return POSIX_CALL(filesystem::readv(handle, iov, iovcnt));
}
EXPORT SYSV_ABI size_t __NID(_writev)(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) {
return POSIX_CALL(filesystem::writev(handle, iov, iovcnt));
}
EXPORT SYSV_ABI size_t sceKernelWritev(int handle, const filesystem::SceKernelIovec* iov, int iovcnt) {
return filesystem::writev(handle, iov, iovcnt);
}
@ -185,4 +189,4 @@ EXPORT SYSV_ABI int64_t sceKernelLwfsLseek(int fd, int64_t offset, int whence) {
EXPORT SYSV_ABI size_t sceKernelLwfsWrite(int fd, const void* buf, size_t nbytes) {
return filesystem::lwfsWrite(fd, buf, nbytes);
}
}
}

View File

@ -91,3 +91,9 @@ struct rusage_t {
uint32_t ru_nvcsw = 0;
uint32_t ru_nivcsw = 0;
};
struct SceTimesec {
time_t t;
unsigned long westsec;
unsigned long dstsec;
};

View File

@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.24)
add_library(config SHARED config.cpp)
target_link_directories(config PRIVATE
${CMAKE_BINARY_DIR}/third_party/install/lib
)
add_dependencies(config boost)
target_link_libraries(config libboost_thread)
@ -18,4 +22,4 @@ set_target_properties(config
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/third_party/intall/lib"
)
install(TARGETS config LIBRARY DESTINATION .)
install(TARGETS config LIBRARY DESTINATION .)

View File

@ -2,15 +2,25 @@
#include "config.h"
#undef __APICALL_EXTERN
#include <fstream>
class Config: public IConfig {
nlohmann::json m_logging;
boost::mutex m_mutex_logging;
json m_logging;
json m_graphics;
json m_audio;
json m_controls;
boost::mutex m_mutex_logging;
boost::mutex m_mutex_graphics;
boost::mutex m_mutex_audio;
boost::mutex m_mutex_controls;
public:
Config() = default;
Config();
virtual ~Config() = default;
std::pair<boost::unique_lock<boost::mutex>, nlohmann::json*> accessLogging() final;
std::pair<boost::unique_lock<boost::mutex>, json*> accessModule(ConfigSaveFlags flag);
virtual bool save(uint32_t flags);
};
IConfig* accessConfig() {
@ -18,7 +28,97 @@ IConfig* accessConfig() {
return &obj;
}
std::pair<boost::unique_lock<boost::mutex>, nlohmann::json*> Config::accessLogging() {
boost::unique_lock lock(m_mutex_logging);
return std::make_pair(std::move(lock), &m_logging);
}
std::pair<boost::unique_lock<boost::mutex>, json*> Config::accessModule(ConfigSaveFlags flag) {
switch (flag) {
case ConfigSaveFlags::LOGGING: return std::make_pair(boost::unique_lock(m_mutex_logging), &m_logging);
case ConfigSaveFlags::GRAPHICS: return std::make_pair(boost::unique_lock(m_mutex_graphics), &m_graphics);
case ConfigSaveFlags::AUDIO: return std::make_pair(boost::unique_lock(m_mutex_audio), &m_audio);
case ConfigSaveFlags::CONTROLS: return std::make_pair(boost::unique_lock(m_mutex_controls), &m_controls);
default: throw std::exception("Invalid bit flag");
}
}
bool Config::save(uint32_t flags) {
auto save = [this](std::string_view fname, json& j) {
auto path = std::string("./config/") + fname.data();
try {
std::ofstream json_file(path);
json_file << std::setw(2) << j;
return true;
} catch (const json::exception& e) {
printf("Failed to save %s: %s\n", fname.data(), e.what());
return false;
}
};
bool result = true;
if (!std::filesystem::is_directory("./config/")) std::filesystem::create_directory("./config/");
if (flags & (uint32_t)ConfigSaveFlags::LOGGING) result &= save("logging.json", m_logging);
if (flags & (uint32_t)ConfigSaveFlags::GRAPHICS) result &= save("graphics.json", m_graphics);
if (flags & (uint32_t)ConfigSaveFlags::AUDIO) result &= save("audio.json", m_audio);
if (flags & (uint32_t)ConfigSaveFlags::CONTROLS) result &= save("controls.json", m_controls);
return true;
}
Config::Config() {
auto load = [this](std::string_view fname, json& j, json defaults = {}, ConfigSaveFlags dflag = ConfigSaveFlags::NONE) {
auto path = std::string("./config/") + fname.data();
bool should_resave = false;
try {
std::ifstream json_file(path);
j = json::parse(json_file, nullptr, true, true);
printf("Config %s loaded successfully\n", fname.data());
} catch (const json::exception& e) {
printf("Failed to parse %s: %s\n", fname.data(), e.what());
try {
std::filesystem::path newp(path);
newp.replace_extension(".back");
std::filesystem::rename(path, newp);
} catch (const std::filesystem::filesystem_error& e) {
}
j = defaults;
should_resave = true;
}
for (auto& [dkey, dval]: defaults.items()) {
if (j[dkey].is_null() && !dval.is_null()) {
j[dkey] = dval;
should_resave = true;
printf("%s: missing parameter \"%s\" has been added!\n", fname.data(), dkey.c_str());
}
}
for (auto& [ckey, cval]: j.items()) {
if (defaults[ckey].is_null()) {
j.erase(cval);
should_resave = true;
printf("%s: unused parameter \"%s\" has been removed!\n", fname.data(), ckey.c_str());
}
}
if (should_resave && dflag != ConfigSaveFlags::NONE) this->save((uint32_t)dflag);
};
load("logging.json", m_logging, {{"sink", "baical"}}, ConfigSaveFlags::LOGGING);
load("graphics.json", m_graphics, {}, ConfigSaveFlags::GRAPHICS);
load("audio.json", m_audio, {{"volume", 0.5f}, {"device", "[default]"}}, ConfigSaveFlags::AUDIO);
load("controls.json", m_controls,
{{"type", "gamepad"},
{"deadzones", json::array({{{"left_stick", {{"x", 0.0f}, {"y", 0.0f}}}, {"right_stick", {{"x", 0.0f}, {"y", 0.0f}}}},
{{"left_stick", {{"x", 0.0f}, {"y", 0.0f}}}, {"right_stick", {{"x", 0.0f}, {"y", 0.0f}}}},
{{"left_stick", {{"x", 0.0f}, {"y", 0.0f}}}, {"right_stick", {{"x", 0.0f}, {"y", 0.0f}}}},
{{"left_stick", {{"x", 0.0f}, {"y", 0.0f}}}, {"right_stick", {{"x", 0.0f}, {"y", 0.0f}}}}})},
{"keybinds",
{
{"triangle", ""}, {"square", ""}, {"circle", ""}, {"cross", ""}, {"dpad_up", ""}, {"dpad_down", ""}, {"dpad_left", ""}, {"dpad_right", ""},
{"options", ""}, {"touchpad", ""}, {"l1", ""}, {"l2", ""}, {"l3", ""}, {"r1", ""}, {"r2", ""}, {"r3", ""},
{"lx-", ""}, {"lx+", ""}, {"ly-", ""}, {"ly+", ""}, {"rx-", ""}, {"rx+", ""}, {"ry-", ""}, {"ry+", ""},
}}},
ConfigSaveFlags::CONTROLS);
}

View File

@ -4,6 +4,10 @@
#include <boost/thread/mutex.hpp>
using json = nlohmann::json;
enum class ConfigSaveFlags : uint32_t { NONE = 0, LOGGING = 2 << 0, GRAPHICS = 2 << 1, AUDIO = 2 << 2, CONTROLS = 2 << 3 };
class IConfig {
CLASS_NO_COPY(IConfig);
CLASS_NO_MOVE(IConfig);
@ -14,7 +18,9 @@ class IConfig {
public:
virtual ~IConfig() = default;
virtual std::pair<boost::unique_lock<boost::mutex>, nlohmann::json*> accessLogging() = 0;
virtual std::pair<boost::unique_lock<boost::mutex>, json*> accessModule(ConfigSaveFlags) = 0;
virtual bool save(uint32_t flags) = 0;
};
#ifdef __APICALL_EXTERN
@ -24,4 +30,4 @@ class IConfig {
#endif
__APICALL IConfig* accessConfig();
#undef __APICALL
#undef __APICALL