From 035946cfe9e7b62d26712b2a3d7f909ad300fc09 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Mar 2025 11:19:48 +1100 Subject: [PATCH] Non working PS4 --- .gitignore | 2 + Makefile | 2 + misc/ps4/Makefile | 75 ++++++ src/Audio.h | 3 + src/Core.h | 9 +- src/Platform_PS4.c | 621 +++++++++++++++++++++++++++++++++++++++++++++ src/Window_PS4.c | 155 +++++++++++ 7 files changed, 865 insertions(+), 2 deletions(-) create mode 100644 misc/ps4/Makefile create mode 100644 src/Platform_PS4.c create mode 100644 src/Window_PS4.c diff --git a/.gitignore b/.gitignore index 1fd71b55c..69acc1cf6 100644 --- a/.gitignore +++ b/.gitignore @@ -56,12 +56,14 @@ misc/xbox/vs_textured.inl # Sony console build results build-ps2/ build-ps3/ +build-ps4/ build-psp/ build-vita/ EBOOT.PBP PARAM.SFO param.sfo eboot.bin +pkg.gp4 # Desktop build results build-amiga-68k/ diff --git a/Makefile b/Makefile index e6597518f..55b8b7bc0 100644 --- a/Makefile +++ b/Makefile @@ -239,6 +239,8 @@ ps2: $(MAKE) -f misc/ps2/Makefile ps3: $(MAKE) -f misc/ps3/Makefile +ps4: + $(MAKE) -f misc/ps4/Makefile xbox: $(MAKE) -f misc/xbox/Makefile xbox360: diff --git a/misc/ps4/Makefile b/misc/ps4/Makefile new file mode 100644 index 000000000..bf4983917 --- /dev/null +++ b/misc/ps4/Makefile @@ -0,0 +1,75 @@ +# Package metadata. +TITLE := OpenOrbis Hello World Sample +VERSION := 1.00 +TITLE_ID := BREW00083 +CONTENT_ID := IV0000-BREW00083_00-HELLOWORLD000000 + +# Libraries linked into the ELF. +LIBS := -lc -lkernel -lc++ -lSceVideoOut -lSceNet + +# Additional compile flags. +#EXTRAFLAGS := + +# Asset and module directories. +LIBMODULES := $(wildcard sce_module/*) + +# You likely won't need to touch anything below this point. + +# Root vars +TOOLCHAIN := $(OO_PS4_TOOLCHAIN) +PROJDIR := src +INTDIR := build-ps4 + +# Define objects to build +CFILES := $(wildcard $(PROJDIR)/*.c) +CPPFILES := $(wildcard $(PROJDIR)/*.cpp) +OBJS := $(patsubst $(PROJDIR)/%.c, $(INTDIR)/%.o, $(CFILES)) $(patsubst $(PROJDIR)/%.cpp, $(INTDIR)/%.o, $(CPPFILES)) + +# Define final C/C++ flags +CFLAGS := --target=x86_64-pc-freebsd12-elf -fPIC -funwind-tables -c $(EXTRAFLAGS) -isysroot $(TOOLCHAIN) -isystem $(TOOLCHAIN)/include -isystem $(TOOLCHAIN)/include/orbis -DPLAT_PS4 +CXXFLAGS := $(CFLAGS) -isystem $(TOOLCHAIN)/include/c++/v1 +LDFLAGS := -m elf_x86_64 -pie --script $(TOOLCHAIN)/link.x --eh-frame-hdr -L$(TOOLCHAIN)/lib $(LIBS) $(TOOLCHAIN)/lib/crt1.o + +# Create the intermediate directory incase it doesn't already exist. +_unused := $(shell mkdir -p $(INTDIR)) + +CC := clang +CCX := clang++ +LD := ld.lld +CDIR := linux + +all: $(CONTENT_ID).pkg + +$(CONTENT_ID).pkg: pkg.gp4 + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core pkg_build $< . + +pkg.gp4: eboot.bin + cd misc/ps4 + $(TOOLCHAIN)/bin/$(CDIR)/create-gp4 -out $@ --content-id=$(CONTENT_ID) --files "sce_sys/about/right.sprx sce_sys/param.sfo sce_sys/icon0.png" + +misc/ps4/sce_sys/param.sfo: Makefile + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_new $@ + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ APP_TYPE --type Integer --maxsize 4 --value 1 + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ APP_VER --type Utf8 --maxsize 8 --value '$(VERSION)' + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ ATTRIBUTE --type Integer --maxsize 4 --value 0 + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ CATEGORY --type Utf8 --maxsize 4 --value 'gd' + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ CONTENT_ID --type Utf8 --maxsize 48 --value '$(CONTENT_ID)' + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ DOWNLOAD_DATA_SIZE --type Integer --maxsize 4 --value 0 + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ SYSTEM_VER --type Integer --maxsize 4 --value 0 + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ TITLE --type Utf8 --maxsize 128 --value '$(TITLE)' + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ TITLE_ID --type Utf8 --maxsize 12 --value '$(TITLE_ID)' + $(TOOLCHAIN)/bin/$(CDIR)/PkgTool.Core sfo_setentry $@ VERSION --type Utf8 --maxsize 8 --value '$(VERSION)' + +eboot.bin: $(INTDIR) $(OBJS) + $(LD) $(INTDIR)/*.o -o $(INTDIR)/$(PROJDIR).elf $(LDFLAGS) + $(TOOLCHAIN)/bin/$(CDIR)/create-fself -in=$(INTDIR)/$(PROJDIR).elf -out=$(INTDIR)/$(PROJDIR).oelf --eboot "eboot.bin" --paid 0x3800000000000011 + +$(INTDIR)/%.o: $(PROJDIR)/%.c + $(CC) $(CFLAGS) -o $@ $< + +$(INTDIR)/%.o: $(PROJDIR)/%.cpp + $(CCX) $(CXXFLAGS) -o $@ $< + +clean: + rm -f $(CONTENT_ID).pkg pkg.gp4 pkg/sce_sys/param.sfo eboot.bin \ + $(INTDIR)/$(PROJDIR).elf $(INTDIR)/$(PROJDIR).oelf $(OBJS) diff --git a/src/Audio.h b/src/Audio.h index 2b97f2689..5dcb801ab 100644 --- a/src/Audio.h +++ b/src/Audio.h @@ -56,6 +56,9 @@ cc_bool AudioBackend_Init(void); void AudioBackend_Tick(void); void AudioBackend_Free(void); +#define AUDIO_USAGE_BUFFER 0x01 +#define AUDIO_USAGE_STREAM 0x02 + /* Initialises an audio context. */ cc_result Audio_Init(struct AudioContext* ctx, int buffers); /* Stops any playing audio and then frees the audio context. */ diff --git a/src/Core.h b/src/Core.h index a5e12c87b..83f53a3fd 100644 --- a/src/Core.h +++ b/src/Core.h @@ -186,14 +186,19 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_NOSOUNDS #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN #define DEFAULT_SSL_BACKEND CC_SSL_BACKEND_BEARSSL +#elif defined PLAT_PS4 + #define CC_BUILD_PS4 + #define CC_BUILD_CONSOLE + #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN + #define DEFAULT_AUD_BACKEND CC_AUD_BACKEND_OPENAL + #define DEFAULT_GFX_BACKEND CC_GFX_BACKEND_SOFTGPU #elif defined __WRL_NO_DEFAULT_LIB__ #undef CC_BUILD_FREETYPE #define CC_BUILD_WIN #define CC_BUILD_UWP - #define CC_BUILD_NOMUSIC - #define CC_BUILD_NOSOUNDS #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN #define DEFAULT_GFX_BACKEND CC_GFX_BACKEND_D3D11 + #define DEFAULT_AUD_BACKEND CC_AUD_BACKEND_OPENAL #elif defined _WIN32 #define CC_BUILD_WIN #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN diff --git a/src/Platform_PS4.c b/src/Platform_PS4.c new file mode 100644 index 000000000..1a208d712 --- /dev/null +++ b/src/Platform_PS4.c @@ -0,0 +1,621 @@ +#include "Core.h" +#if defined CC_BUILD_PS4 + +#include "_PlatformBase.h" +#include "Stream.h" +#include "ExtMath.h" +#include "SystemFonts.h" +#include "Funcs.h" +#include "Window.h" +#include "Utils.h" +#include "Errors.h" +#include "PackedCol.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "_PlatformConsole.h" + +const cc_result ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ +const cc_result ReturnCode_FileNotFound = ENOENT; +const cc_result ReturnCode_DirectoryExists = EEXIST; +const cc_result ReturnCode_SocketInProgess = EINPROGRESS; +const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK; +const cc_result ReturnCode_SocketDropped = EPIPE; +#define SUPPORTS_GETADDRINFO 1 + +const char* Platform_AppNameSuffix = " PS4"; +cc_bool Platform_ReadonlyFilesystem; + + +/*########################################################################################################################* +*------------------------------------------------------Logging/Time-------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_Log(const char* msg, int len) { + sceKernelWrite(STDOUT_FILENO, msg, len); + sceKernelWrite(STDOUT_FILENO, "\n", 1); +} + +TimeMS DateTime_CurrentUTC(void) { + struct timeval cur; + gettimeofday(&cur, NULL); + return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS; +} + +void DateTime_CurrentLocal(struct cc_datetime* t) { + struct timeval cur; + struct tm loc_time; + gettimeofday(&cur, NULL); + localtime_r(&cur.tv_sec, &loc_time); + + t->year = loc_time.tm_year + 1900; + t->month = loc_time.tm_mon + 1; + t->day = loc_time.tm_mday; + t->hour = loc_time.tm_hour; + t->minute = loc_time.tm_min; + t->second = loc_time.tm_sec; +} + + +/*########################################################################################################################* +*--------------------------------------------------------Stopwatch--------------------------------------------------------* +*#########################################################################################################################*/ +#define NS_PER_SEC 1000000000ULL + +/* clock_gettime is optional, see http://pubs.opengroup.org/onlinepubs/009696899/functions/clock_getres.html */ +/* "... These functions are part of the Timers option and need not be available on all implementations..." */ +cc_uint64 Stopwatch_Measure(void) { + struct timespec t; + /* TODO: CLOCK_MONOTONIC_RAW ?? */ + clock_gettime(CLOCK_MONOTONIC, &t); + return (cc_uint64)t.tv_sec * NS_PER_SEC + t.tv_nsec; +} + +cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) { + if (end < beg) return 0; + return (end - beg) / 1000; +} + + +/*########################################################################################################################* +*-------------------------------------------------------Crash handling----------------------------------------------------* +*#########################################################################################################################*/ +void CrashHandler_Install(void) { +} + +void Process_Abort2(cc_result result, const char* raw_msg) { + Logger_DoAbort(result, raw_msg, NULL); +} + + +/*########################################################################################################################* +*-----------------------------------------------------Directory/File------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_EncodePath(cc_filepath* dst, const cc_string* path) { + char* str = dst->buffer; + String_EncodeUtf8(str, path); +} + +cc_result Directory_Create(const cc_filepath* path) { + /* read/write/search permissions for owner and group, and with read/search permissions for others. */ + /* TODO: Is the default mode in all cases */ + return mkdir(path->buffer, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1 ? errno : 0; +} + +int File_Exists(const cc_filepath* path) { + struct stat sb; + return stat(path->buffer, &sb) == 0 && S_ISREG(sb.st_mode); +} + +cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) { + cc_string path; char pathBuffer[FILENAME_SIZE]; + cc_filepath str; + DIR* dirPtr; + struct dirent* entry; + char* src; + int len, res, is_dir; + + Platform_EncodePath(&str, dirPath); + dirPtr = opendir(str.buffer); + if (!dirPtr) return errno; + + /* POSIX docs: "When the end of the directory is encountered, a null pointer is returned and errno is not changed." */ + /* errno is sometimes leftover from previous calls, so always reset it before readdir gets called */ + errno = 0; + String_InitArray(path, pathBuffer); + + while ((entry = readdir(dirPtr))) { + path.length = 0; + String_Format1(&path, "%s/", dirPath); + + /* ignore . and .. entry */ + src = entry->d_name; + if (src[0] == '.' && src[1] == '\0') continue; + if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue; + + len = String_Length(src); + String_AppendUtf8(&path, src, len); + +#if defined CC_BUILD_HAIKU || defined CC_BUILD_SOLARIS || defined CC_BUILD_HPUX || defined CC_BUILD_IRIX || defined CC_BUILD_BEOS + { + char full_path[NATIVE_STR_LEN]; + struct stat sb; + String_EncodeUtf8(full_path, &path); + is_dir = stat(full_path, &sb) == 0 && S_ISDIR(sb.st_mode); + } +#else + is_dir = entry->d_type == DT_DIR; + /* TODO: fallback to stat when this fails */ +#endif + + callback(&path, obj, is_dir); + errno = 0; + } + + res = errno; /* return code from readdir */ + closedir(dirPtr); + return res; +} + +static cc_result File_Do(cc_file* file, const char* path, int mode) { + *file = open(path, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + return *file == -1 ? errno : 0; +} + +cc_result File_Open(cc_file* file, const cc_filepath* path) { +#if !defined CC_BUILD_OS2 + return File_Do(file, path->buffer, O_RDONLY); +#else + return File_Do(file, path->buffer, O_RDONLY | O_BINARY); +#endif +} +cc_result File_Create(cc_file* file, const cc_filepath* path) { +#if !defined CC_BUILD_OS2 + return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_TRUNC); +#else + return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_TRUNC | O_BINARY); +#endif +} +cc_result File_OpenOrCreate(cc_file* file, const cc_filepath* path) { +#if !defined CC_BUILD_OS2 + return File_Do(file, path->buffer, O_RDWR | O_CREAT); +#else + return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_BINARY); +#endif +} + +cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) { + *bytesRead = read(file, data, count); + return *bytesRead == -1 ? errno : 0; +} + +cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) { + *bytesWrote = write(file, data, count); + return *bytesWrote == -1 ? errno : 0; +} + +cc_result File_Close(cc_file file) { + return close(file) == -1 ? errno : 0; +} + +cc_result File_Seek(cc_file file, int offset, int seekType) { + static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END }; + return lseek(file, offset, modes[seekType]) == -1 ? errno : 0; +} + +cc_result File_Position(cc_file file, cc_uint32* pos) { + *pos = lseek(file, 0, SEEK_CUR); + return *pos == -1 ? errno : 0; +} + +cc_result File_Length(cc_file file, cc_uint32* len) { + struct stat st; + if (fstat(file, &st) == -1) { *len = -1; return errno; } + *len = st.st_size; return 0; +} + + +/*########################################################################################################################* +*--------------------------------------------------------Threading--------------------------------------------------------* +*#########################################################################################################################*/ +void Thread_Sleep(cc_uint32 milliseconds) { usleep(milliseconds * 1000); } + +static void* ExecThread(void* param) { + ((Thread_StartFunc)param)(); + return NULL; +} + +void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) { + pthread_t* ptr = (pthread_t*)Mem_Alloc(1, sizeof(pthread_t), "thread"); + pthread_attr_t attrs; + int res; + + *handle = ptr; + pthread_attr_init(&attrs); + pthread_attr_setstacksize(&attrs, stackSize); + + res = pthread_create(ptr, &attrs, ExecThread, (void*)func); + if (res) Process_Abort2(res, "Creating thread"); + pthread_attr_destroy(&attrs); +} + +void Thread_Detach(void* handle) { + pthread_t* ptr = (pthread_t*)handle; + int res = pthread_detach(*ptr); + if (res) Process_Abort2(res, "Detaching thread"); + Mem_Free(ptr); +} + +void Thread_Join(void* handle) { + pthread_t* ptr = (pthread_t*)handle; + int res = pthread_join(*ptr, NULL); + if (res) Process_Abort2(res, "Joining thread"); + Mem_Free(ptr); +} + +void* Mutex_Create(const char* name) { + pthread_mutex_t* ptr = (pthread_mutex_t*)Mem_Alloc(1, sizeof(pthread_mutex_t), "mutex"); + int res = pthread_mutex_init(ptr, NULL); + if (res) Process_Abort2(res, "Creating mutex"); + return ptr; +} + +void Mutex_Free(void* handle) { + int res = pthread_mutex_destroy((pthread_mutex_t*)handle); + if (res) Process_Abort2(res, "Destroying mutex"); + Mem_Free(handle); +} + +void Mutex_Lock(void* handle) { + int res = pthread_mutex_lock((pthread_mutex_t*)handle); + if (res) Process_Abort2(res, "Locking mutex"); +} + +void Mutex_Unlock(void* handle) { + int res = pthread_mutex_unlock((pthread_mutex_t*)handle); + if (res) Process_Abort2(res, "Unlocking mutex"); +} + +struct WaitData { + pthread_cond_t cond; + pthread_mutex_t mutex; + int signalled; /* For when Waitable_Signal is called before Waitable_Wait */ +}; + +void* Waitable_Create(const char* name) { + struct WaitData* ptr = (struct WaitData*)Mem_Alloc(1, sizeof(struct WaitData), "waitable"); + int res; + + res = pthread_cond_init(&ptr->cond, NULL); + if (res) Process_Abort2(res, "Creating waitable"); + res = pthread_mutex_init(&ptr->mutex, NULL); + if (res) Process_Abort2(res, "Creating waitable mutex"); + + ptr->signalled = false; + return ptr; +} + +void Waitable_Free(void* handle) { + struct WaitData* ptr = (struct WaitData*)handle; + int res; + + res = pthread_cond_destroy(&ptr->cond); + if (res) Process_Abort2(res, "Destroying waitable"); + res = pthread_mutex_destroy(&ptr->mutex); + if (res) Process_Abort2(res, "Destroying waitable mutex"); + Mem_Free(handle); +} + +void Waitable_Signal(void* handle) { + struct WaitData* ptr = (struct WaitData*)handle; + int res; + + Mutex_Lock(&ptr->mutex); + ptr->signalled = true; + Mutex_Unlock(&ptr->mutex); + + res = pthread_cond_signal(&ptr->cond); + if (res) Process_Abort2(res, "Signalling event"); +} + +void Waitable_Wait(void* handle) { + struct WaitData* ptr = (struct WaitData*)handle; + int res; + + Mutex_Lock(&ptr->mutex); + if (!ptr->signalled) { + res = pthread_cond_wait(&ptr->cond, &ptr->mutex); + if (res) Process_Abort2(res, "Waitable wait"); + } + ptr->signalled = false; + Mutex_Unlock(&ptr->mutex); +} + +void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) { + struct WaitData* ptr = (struct WaitData*)handle; + struct timeval tv; + struct timespec ts; + int res; + gettimeofday(&tv, NULL); + + /* absolute time for some silly reason */ + ts.tv_sec = tv.tv_sec + milliseconds / 1000; + ts.tv_nsec = 1000 * (tv.tv_usec + 1000 * (milliseconds % 1000)); + + /* statement above might exceed max nsec, so adjust for that */ + while (ts.tv_nsec >= NS_PER_SEC) { + ts.tv_sec++; + ts.tv_nsec -= NS_PER_SEC; + } + + Mutex_Lock(&ptr->mutex); + if (!ptr->signalled) { + res = pthread_cond_timedwait(&ptr->cond, &ptr->mutex, &ts); + if (res && res != ETIMEDOUT) Process_Abort2(res, "Waitable wait for"); + } + ptr->signalled = false; + Mutex_Unlock(&ptr->mutex); +} + + +/*########################################################################################################################* +*--------------------------------------------------------Font/Text--------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_LoadSysFonts(void) { } + + +/*########################################################################################################################* +*---------------------------------------------------------Socket----------------------------------------------------------* +*#########################################################################################################################*/ +union SocketAddress { + struct sockaddr raw; + struct sockaddr_in v4; + #ifdef AF_INET6 + struct sockaddr_in6 v6; + struct sockaddr_storage total; + #endif +}; +/* Sanity check to ensure cc_sockaddr struct is large enough to contain all socket addresses supported by this platform */ +static char sockaddr_size_check[sizeof(union SocketAddress) < CC_SOCKETADDR_MAXSIZE ? 1 : -1]; + +#if SUPPORTS_GETADDRINFO +static cc_result ParseHost(const char* host, int port, cc_sockaddr* addrs, int* numValidAddrs) { + char portRaw[32]; cc_string portStr; + struct addrinfo hints = { 0 }; + struct addrinfo* result; + struct addrinfo* cur; + int res, i = 0; + + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + String_InitArray(portStr, portRaw); + String_AppendInt(&portStr, port); + portRaw[portStr.length] = '\0'; + + res = getaddrinfo(host, portRaw, &hints, &result); + if (res == EAI_AGAIN) return SOCK_ERR_UNKNOWN_HOST; + if (res) return res; + + /* Prefer IPv4 addresses first */ + for (cur = result; cur && i < SOCKET_MAX_ADDRS; cur = cur->ai_next) + { + if (cur->ai_family != AF_INET) continue; + SocketAddr_Set(&addrs[i], cur->ai_addr, cur->ai_addrlen); i++; + } + + for (cur = result; cur && i < SOCKET_MAX_ADDRS; cur = cur->ai_next) + { + if (cur->ai_family == AF_INET) continue; + SocketAddr_Set(&addrs[i], cur->ai_addr, cur->ai_addrlen); i++; + } + + freeaddrinfo(result); + *numValidAddrs = i; + return i == 0 ? ERR_INVALID_ARGUMENT : 0; +} +#else +static cc_result ParseHost(const char* host, int port, cc_sockaddr* addrs, int* numValidAddrs) { + struct hostent* res = gethostbyname(host); + struct sockaddr_in* addr4; + char* src_addr; + int i; + + // Must have at least one IPv4 address + if (res->h_addrtype != AF_INET) return ERR_INVALID_ARGUMENT; + if (!res->h_addr_list) return ERR_INVALID_ARGUMENT; + + for (i = 0; i < SOCKET_MAX_ADDRS; i++) + { + src_addr = res->h_addr_list[i]; + if (!src_addr) break; + addrs[i].size = sizeof(struct sockaddr_in); + + addr4 = (struct sockaddr_in*)addrs[i].data; + addr4->sin_family = AF_INET; + addr4->sin_port = htons(port); + addr4->sin_addr = *(struct in_addr*)src_addr; + } + + *numValidAddrs = i; + return i == 0 ? ERR_INVALID_ARGUMENT : 0; +} +#endif + +cc_result Socket_ParseAddress(const cc_string* address, int port, cc_sockaddr* addrs, int* numValidAddrs) { + union SocketAddress* addr = (union SocketAddress*)addrs[0].data; + char str[NATIVE_STR_LEN]; + + String_EncodeUtf8(str, address); + *numValidAddrs = 0; + + if (inet_pton(AF_INET, str, &addr->v4.sin_addr) > 0) { + addr->v4.sin_family = AF_INET; + addr->v4.sin_port = htons(port); + + addrs[0].size = sizeof(addr->v4); + *numValidAddrs = 1; + return 0; + } + + #ifdef AF_INET6 + if (inet_pton(AF_INET6, str, &addr->v6.sin6_addr) > 0) { + addr->v6.sin6_family = AF_INET6; + addr->v6.sin6_port = htons(port); + + addrs[0].size = sizeof(addr->v6); + *numValidAddrs = 1; + return 0; + } + #endif + + return ParseHost(str, port, addrs, numValidAddrs); +} + +cc_result Socket_Create(cc_socket* s, cc_sockaddr* addr, cc_bool nonblocking) { + struct sockaddr* raw = (struct sockaddr*)addr->data; + + *s = socket(raw->sa_family, SOCK_STREAM, IPPROTO_TCP); + if (*s == -1) return errno; + + if (nonblocking) { + int blocking_raw = -1; /* non-blocking mode */ + //ioctl(*s, FIONBIO, &blocking_raw); TODO + } + return 0; +} + +cc_result Socket_Connect(cc_socket s, cc_sockaddr* addr) { + struct sockaddr* raw = (struct sockaddr*)addr->data; + + int res = connect(s, raw, addr->size); + return res == -1 ? errno : 0; +} + +cc_result Socket_Read(cc_socket s, cc_uint8* data, cc_uint32 count, cc_uint32* modified) { + int recvCount = recv(s, data, count, 0); + if (recvCount != -1) { *modified = recvCount; return 0; } + *modified = 0; return errno; +} + +cc_result Socket_Write(cc_socket s, const cc_uint8* data, cc_uint32 count, cc_uint32* modified) { + int sentCount = send(s, data, count, 0); + if (sentCount != -1) { *modified = sentCount; return 0; } + *modified = 0; return errno; +} + +void Socket_Close(cc_socket s) { + shutdown(s, SHUT_RDWR); + close(s); +} + +#if defined CC_BUILD_DARWIN || defined CC_BUILD_BEOS +/* poll is broken on old OSX apparently https://daniel.haxx.se/docs/poll-vs-select.html */ +/* BeOS lacks support for poll */ +static cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) { + fd_set set; + struct timeval time = { 0 }; + int selectCount; + + FD_ZERO(&set); + FD_SET(s, &set); + + if (mode == SOCKET_POLL_READ) { + selectCount = select(s + 1, &set, NULL, NULL, &time); + } else { + selectCount = select(s + 1, NULL, &set, NULL, &time); + } + + if (selectCount == -1) { *success = false; return errno; } + *success = FD_ISSET(s, &set) != 0; return 0; +} +#else +#include +static cc_result Socket_Poll(cc_socket s, int mode, cc_bool* success) { + struct pollfd pfd; + int flags; + + pfd.fd = s; + pfd.events = mode == SOCKET_POLL_READ ? POLLIN : POLLOUT; + if (poll(&pfd, 1, 0) == -1) { *success = false; return errno; } + + /* to match select, closed socket still counts as readable */ + flags = mode == SOCKET_POLL_READ ? (POLLIN | POLLHUP) : POLLOUT; + *success = (pfd.revents & flags) != 0; + return 0; +} +#endif + +cc_result Socket_CheckReadable(cc_socket s, cc_bool* readable) { + return Socket_Poll(s, SOCKET_POLL_READ, readable); +} + +cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) { + socklen_t resultSize = sizeof(socklen_t); + cc_result res = Socket_Poll(s, SOCKET_POLL_WRITE, writable); + if (res || *writable) return res; + + /* https://stackoverflow.com/questions/29479953/so-error-value-after-successful-socket-operation */ + getsockopt(s, SOL_SOCKET, SO_ERROR, &res, &resultSize); + return res; +} + + +/*########################################################################################################################* +*--------------------------------------------------------Platform---------------------------------------------------------* +*#########################################################################################################################*/ +void Platform_Free(void) { } + +cc_bool Platform_DescribeError(cc_result res, cc_string* dst) { + char chars[NATIVE_STR_LEN]; + int len; + + /* For unrecognised error codes, strerror_r might return messages */ + /* such as 'No error information', which is not very useful */ + /* (could check errno here but quicker just to skip entirely) */ + if (res >= 1000) return false; + + len = strerror_r(res, chars, NATIVE_STR_LEN); + if (len == -1) return false; + + len = String_CalcLen(chars, NATIVE_STR_LEN); + String_AppendUtf8(dst, chars, len); + return true; +} + +cc_bool Process_OpenSupported = false; +cc_result Process_StartOpen(const cc_string* args) { + return ERR_NOT_SUPPORTED; +} + +void Platform_Init(void) { + Platform_LogConst("initing 2.."); +} + + +/*########################################################################################################################* +*-------------------------------------------------------Encryption--------------------------------------------------------* +*#########################################################################################################################*/ +#define MACHINE_KEY "PS4_PS4_PS4_PS4_" + +static cc_result GetMachineID(cc_uint32* key) { + Mem_Copy(key, MACHINE_KEY, sizeof(MACHINE_KEY) - 1); + return 0; +} +#endif diff --git a/src/Window_PS4.c b/src/Window_PS4.c new file mode 100644 index 000000000..e11de6415 --- /dev/null +++ b/src/Window_PS4.c @@ -0,0 +1,155 @@ +#include "Core.h" +#if defined CC_BUILD_PS4 +#include "Window.h" +#include "Platform.h" +#include "Input.h" +#include "Event.h" +#include "Graphics.h" +#include "String.h" +#include "Funcs.h" +#include "Bitmap.h" +#include "Errors.h" +#include "ExtMath.h" +#include "Logger.h" +#include "VirtualKeyboard.h" +#include + +static cc_bool launcherMode; + +struct _DisplayData DisplayInfo; +struct cc_window WindowInfo; + +void Window_PreInit(void) { + Platform_LogConst("initing 1.."); +} + +void Window_Init(void) { + OrbisVideoOutResolutionStatus res; + int handle = sceVideoOutOpen(ORBIS_VIDEO_USER_MAIN, ORBIS_VIDEO_OUT_BUS_MAIN, 0, 0); + sceVideoOutGetResolutionStatus(handle, &res); + + DisplayInfo.Width = res.width; + DisplayInfo.Height = res.height; + DisplayInfo.ScaleX = 1; + DisplayInfo.ScaleY = 1; + + Window_Main.Width = res.width; + Window_Main.Height = res.height; + Window_Main.Focused = true; + + Window_Main.Exists = true; + Window_Main.UIScaleX = DEFAULT_UI_SCALE_X; + Window_Main.UIScaleY = DEFAULT_UI_SCALE_Y; + + DisplayInfo.ContentOffsetX = 20; + DisplayInfo.ContentOffsetY = 20; + Window_Main.SoftKeyboard = SOFT_KEYBOARD_VIRTUAL; +} + +void Window_Free(void) { } + +void Window_Create2D(int width, int height) { + launcherMode = true; + Gfx_Create(); // launcher also uses RSX to draw +} + +void Window_Create3D(int width, int height) { + launcherMode = false; +} + +void Window_Destroy(void) { } + +void Window_SetTitle(const cc_string* title) { } +void Clipboard_GetText(cc_string* value) { } // TODO sceClipboardGetText +void Clipboard_SetText(const cc_string* value) { } // TODO sceClipboardSetText + +int Window_GetWindowState(void) { return WINDOW_STATE_FULLSCREEN; } +cc_result Window_EnterFullscreen(void) { return 0; } +cc_result Window_ExitFullscreen(void) { return 0; } +int Window_IsObscured(void) { return 0; } + +void Window_Show(void) { } +void Window_SetSize(int width, int height) { } + +void Window_RequestClose(void) { + Event_RaiseVoid(&WindowEvents.Closing); +} + + +/*########################################################################################################################* +*----------------------------------------------------Input processing-----------------------------------------------------* +*#########################################################################################################################*/ +void Window_ProcessEvents(float delta) { +} + +void Cursor_SetPosition(int x, int y) { } // Makes no sense for PS Vita + +void Window_EnableRawMouse(void) { Input.RawMode = true; } +void Window_UpdateRawMouse(void) { } +void Window_DisableRawMouse(void) { Input.RawMode = false; } + + +/*########################################################################################################################* +*-------------------------------------------------------Gamepads----------------------------------------------------------* +*#########################################################################################################################*/ +void Gamepads_Init(void) { + Input.Sources |= INPUT_SOURCE_GAMEPAD; +} + +void Gamepads_Process(float delta) { +} + + +/*########################################################################################################################* +*------------------------------------------------------Framebuffer--------------------------------------------------------* +*#########################################################################################################################*/ +void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) { + bmp->scan0 = Mem_Alloc(width * height, 4, "bitmap"); + bmp->width = width; + bmp->height = height; +} + +void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) { + // TODO test +} + +void Window_FreeFramebuffer(struct Bitmap* bmp) { + Mem_Free(bmp->scan0); +} + + +/*########################################################################################################################* +*------------------------------------------------------Soft keyboard------------------------------------------------------* +*#########################################################################################################################*/ +void OnscreenKeyboard_Open(struct OpenKeyboardArgs* args) { + if (Input.Sources & INPUT_SOURCE_NORMAL) return; + + VirtualKeyboard_Open(args, launcherMode); +} + +void OnscreenKeyboard_SetText(const cc_string* text) { + VirtualKeyboard_SetText(text); +} + +void OnscreenKeyboard_Close(void) { + VirtualKeyboard_Close(); +} + + +/*########################################################################################################################* +*-------------------------------------------------------Misc/Other--------------------------------------------------------* +*#########################################################################################################################*/ +void Window_ShowDialog(const char* title, const char* msg) { + /* TODO implement */ + Platform_LogConst(title); + Platform_LogConst(msg); +} + +cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) { + return ERR_NOT_SUPPORTED; +} + +cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { + return ERR_NOT_SUPPORTED; +} +#endif