From eb4089657e8e9ad7755a9b145a02c2ee83f2ca4e Mon Sep 17 00:00:00 2001 From: Gauvain 'GovanifY' Roussel-Tarbouriech Date: Mon, 2 Nov 2020 18:03:43 +0100 Subject: [PATCH] USB: cleaning up a bunch of warnings, platcompat fixes --- pcsx2/CMakeLists.txt | 1 + pcsx2/USB/USB.cpp | 14 ++-- pcsx2/USB/configuration.cpp | 48 +------------ pcsx2/USB/configuration.h | 52 +------------- pcsx2/USB/platcompat.h | 131 +++++++++++++++++++++++++++++++++++ pcsx2/USB/qemu-usb/desc.h | 9 +-- pcsx2/USB/usb-mic/audiodev.h | 13 ++-- 7 files changed, 149 insertions(+), 119 deletions(-) create mode 100644 pcsx2/USB/platcompat.h diff --git a/pcsx2/CMakeLists.txt b/pcsx2/CMakeLists.txt index bc811f360b..17fc46a6d2 100644 --- a/pcsx2/CMakeLists.txt +++ b/pcsx2/CMakeLists.txt @@ -365,6 +365,7 @@ set(pcsx2USBHeaders USB/deviceproxy.h USB/configuration.h USB/osdebugout.h + USB/platcompat.h USB/helpers.h USB/readerwriterqueue/readerwriterqueue.h USB/readerwriterqueue/atomicops.h diff --git a/pcsx2/USB/USB.cpp b/pcsx2/USB/USB.cpp index de5b7eb40f..9444249dc0 100644 --- a/pcsx2/USB/USB.cpp +++ b/pcsx2/USB/USB.cpp @@ -339,7 +339,7 @@ s32 USBfreeze(int mode, freezeData *data) { //TODO FREEZE_SIZE mismatch causes loading to fail in PCSX2 beforehand if (mode == FREEZE_LOAD) { - if(data->size < sizeof(USBfreezeData)) + if((long unsigned int)data->size < sizeof(USBfreezeData)) { SysMessage(TEXT("ERROR: Unable to load freeze data! Got %d bytes, expected >= %d.\n"), data->size, sizeof(USBfreezeData)); return -1; @@ -358,7 +358,7 @@ s32 USBfreeze(int mode, freezeData *data) { //clocks = usbd.cycles; //remaining = usbd.remaining; - for(int i=0; i< qemu_ohci->num_ports; i++) + for(uint32_t i=0; i< qemu_ohci->num_ports; i++) { usbd.t.rhport[i].port.opaque = qemu_ohci; usbd.t.rhport[i].port.ops = qemu_ohci->rhport[i].port.ops; @@ -368,7 +368,7 @@ s32 USBfreeze(int mode, freezeData *data) { s8 *ptr = data->data + sizeof(USBfreezeData); // Load the state of the attached devices - if (data->size != sizeof(USBfreezeData) + usbd.device[0].size + usbd.device[1].size + 8192) + if ((long unsigned int)data->size != sizeof(USBfreezeData) + usbd.device[0].size + usbd.device[1].size + 8192) return -1; RegisterDevice& regInst = RegisterDevice::instance(); @@ -398,7 +398,7 @@ s32 USBfreeze(int mode, freezeData *data) { if (proxy && usb_device[i]) /* usb device creation may have failed for some reason */ { - if (proxy->Freeze(FREEZE_SIZE, usb_device[i], nullptr) != usbd.device[i].size) + if (proxy->Freeze(FREEZE_SIZE, usb_device[i], nullptr) != (s32)usbd.device[i].size) { SysMessage(TEXT("Port %d: device's freeze size doesn't match.\n"), 1+(1-i)); return -1; @@ -509,13 +509,13 @@ s32 USBfreeze(int mode, freezeData *data) { usbd.usb_packet.dev_index = i; } - strncpy(usbd.freezeID, USBfreezeID, strlen(USBfreezeID)); + strncpy(usbd.freezeID, USBfreezeID, strlen(usbd.freezeID)); usbd.t = *qemu_ohci; usbd.usb_packet.ep = qemu_ohci->usb_packet.ep ? *qemu_ohci->usb_packet.ep : USBEndpoint{0}; usbd.t.usb_packet.iov = { }; usbd.t.usb_packet.ep = nullptr; - for(int i=0; i< qemu_ohci->num_ports; i++) + for(uint32_t i=0; i< qemu_ohci->num_ports; i++) { usbd.t.rhport[i].port.opaque = nullptr; usbd.t.rhport[i].port.ops = nullptr; @@ -580,7 +580,7 @@ void USBasync(u32 cycles) clocks += remaining; if(qemu_ohci->eof_timer>0) { - while(remaining>=qemu_ohci->eof_timer) + while((uint64_t)remaining>=qemu_ohci->eof_timer) { remaining-=qemu_ohci->eof_timer; qemu_ohci->eof_timer=0; diff --git a/pcsx2/USB/configuration.cpp b/pcsx2/USB/configuration.cpp index 5e2a3645d7..e58102b770 100644 --- a/pcsx2/USB/configuration.cpp +++ b/pcsx2/USB/configuration.cpp @@ -2,6 +2,7 @@ #include "deviceproxy.h" #include "configuration.h" #include "shared/inifile.h" +#include "platcompat.h" #include #include @@ -14,33 +15,13 @@ CIniFile ciniFile; void USBsetSettingsDir( const char* dir ) { -#ifdef _UNICODE - OSDebugOut(L"USBsetSettingsDir: %S\n", dir); - wchar_t dst[4096] = {0}; - size_t num = 0; - mbstowcs_s(&num, dst, dir, countof(dst)); - IniPath = dst; - IniPath.append(iniFile); - OSDebugOut(L"USBsetSettingsDir: %s\n", IniPath.c_str()); - -#else IniPath = dir; IniPath.append(iniFile); -#endif } void USBsetLogDir( const char* dir ) { -#ifdef _UNICODE - OSDebugOut(L"USBsetLogDir: %S\n", dir); - wchar_t dst[4096] = {0}; - size_t num = 0; - mbstowcs_s(&num, dst, dir, countof(dst)); - LogDir = dst; - LogDir.append(_T("USBqemu-wheel.log")); -#else LogDir = dir; -#endif } std::string GetSelectedAPI(const std::pair& pair) @@ -90,33 +71,6 @@ bool SaveSettingValue(const TSTDSTRING& ini, const TSTDSTRING& section, const TC return true; } -#ifdef _UNICODE -bool LoadSettingValue(const TSTDSTRING& ini, const TSTDSTRING& section, const TCHAR* param, std::string& value) -{ - char tmpA[4096] = { 0 }; - size_t num = 0; - std::wstring str; - - CIniKey *key; - auto sect = ciniFile.GetSection(section); - if (sect && (key = sect->GetKey(param))) { - str = key->GetValue(); - wcstombs_s(&num, tmpA, str.c_str(), sizeof(tmpA)); //TODO error-check - value = tmpA; - return true; - } - return false; -} - -bool SaveSettingValue(const TSTDSTRING& ini, const TSTDSTRING& section, const TCHAR* param, const std::string& value) -{ - std::wstring wstr; - wstr.assign(value.begin(), value.end()); - ciniFile.SetKeyValue(section, param, wstr); - return true; -} -#endif - void SaveConfig() { SaveSetting(_T("MAIN"), _T("log"), conf.Log); diff --git a/pcsx2/USB/configuration.h b/pcsx2/USB/configuration.h index 88e1d0b97b..9d02ba1856 100644 --- a/pcsx2/USB/configuration.h +++ b/pcsx2/USB/configuration.h @@ -1,55 +1,11 @@ #pragma once - -#ifdef _WIN32 -#ifndef NOMINMAX -#define NOMINMAX -#endif -#include -#define wfopen _wfopen -#define fseeko64 _fseeki64 -#define ftello64 _ftelli64 -#define TSTDSTRING std::wstring -#define TSTDSTRINGSTREAM std::wstringstream -#define TSTDTOSTRING std::to_wstring - -#ifdef _MSC_VER -typedef SSIZE_T ssize_t; -#endif - -//FIXME narrow string fmt -#ifdef UNICODE -#define SFMTs "S" -#endif - -#define __builtin_constant_p(p) false - -#else //_WIN32 - -#define MAX_PATH PATH_MAX -#define __inline inline - -//#ifndef TEXT -//#define TEXT(x) L##x -//#endif -//FIXME narrow string fmt -#define SFMTs "s" -#define TEXT(val) val -#define TCHAR char -#define wfopen fopen -#define TSTDSTRING std::string -#define TSTDSTRINGSTREAM std::stringstream -#define TSTDTOSTRING std::to_string - -void SysMessage(const char *fmt, ...); - -#endif //_WIN32 - #include #include #include #include #include "osdebugout.h" +#include "platcompat.h" #define RESULT_CANCELED 0 #define RESULT_OK 1 @@ -207,9 +163,3 @@ bool SaveSetting(const TCHAR* section, const TCHAR* key, const Type var) OSDebugOutStream_noprfx(var); return ret; } - -template -constexpr std::size_t countof(const T (&)[N]) noexcept -{ - return N; -} diff --git a/pcsx2/USB/platcompat.h b/pcsx2/USB/platcompat.h new file mode 100644 index 0000000000..988d310dd4 --- /dev/null +++ b/pcsx2/USB/platcompat.h @@ -0,0 +1,131 @@ +#pragma once + +// Annoying defines +// --------------------------------------------------------------------- +// make sure __POSIX__ is defined for all systems where we assume POSIX +// compliance +#if defined(__linux__) || defined(__APPLE__) || defined(__unix__) || defined(__CYGWIN__) || defined(__LINUX__) +#if !defined(__POSIX__) +#define __POSIX__ 1 +#endif +#endif + +#ifdef _WIN32 +# define CALLBACK __stdcall +#else +# define CALLBACK __attribute__((stdcall)) +#endif + +#ifndef EXPORT_C_ +#ifdef _MSC_VER +#define EXPORT_C_(type) extern "C" type CALLBACK +#else +#define EXPORT_C_(type) extern "C" __attribute__((stdcall,externally_visible,visibility("default"))) type +//#define EXPORT_C_(type) extern "C" __attribute__((stdcall,visibility("default"))) type +#endif +#endif + +#ifdef _WIN32 +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#define wfopen _wfopen +#define fseeko64 _fseeki64 +#define ftello64 _ftelli64 +#define TSTDSTRING std::wstring +#define TSTDSTRINGSTREAM std::wstringstream +#define TSTDTOSTRING std::to_wstring + +#ifdef _MSC_VER +typedef SSIZE_T ssize_t; +#endif + +//FIXME narrow string fmt +#define SFMTs "S" + +#define __builtin_constant_p(p) false + +void SysMessageW(const wchar_t *fmt, ...); +#define SysMessage SysMessageW + +#ifndef _T +#define _T(x) L##x +#endif + +#else //_WIN32 + +#define MAX_PATH PATH_MAX +#define __inline inline + +//#ifndef TEXT +//#define TEXT(x) L##x +//#endif +//FIXME narrow string fmt +#define SFMTs "s" +#define TEXT(val) val +#define TCHAR char +#define wfopen fopen +#define TSTDSTRING std::string +#define TSTDSTRINGSTREAM std::stringstream +#define TSTDTOSTRING std::to_string + +void SysMessage(const char *fmt, ...); + +#endif //_WIN32 + +#if __MINGW32__ + +#define DBL_EPSILON 2.2204460492503131e-16 +#define FLT_EPSILON 1.1920928955078125e-7f + +template +errno_t mbstowcs_s( + size_t *pReturnValue, + wchar_t (&wcstr)[size], + const char *mbstr, + size_t count +) +{ + return mbstowcs_s(pReturnValue, wcstr, size, mbstr, count); +} + +template +errno_t wcstombs_s( + size_t *pReturnValue, + char (&mbstr)[size], + const wchar_t *wcstr, + size_t count +) +{ + return wcstombs_s(pReturnValue, mbstr, size, wcstr, count); +} + +#endif //__MINGW32__ + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0]))) +#endif + +#include + +template +constexpr std::size_t countof(const T (&)[N]) noexcept +{ + return N; +} + +template +constexpr std::size_t countof(const T N) +{ + return N.size(); +} + +//TODO Idk, used only in desc.h and struct USBDescriptor should be already packed anyway +#if defined(_WIN32) && !defined(__MINGW32__) +#define PACK(def,name) __pragma( pack(push, 1) ) def name __pragma( pack(pop) ) +#elif defined(__clang__) +#define PACK(def,name) def __attribute__((packed)) name +#else +#define PACK(def,name) def __attribute__((gcc_struct, packed)) name +#endif diff --git a/pcsx2/USB/qemu-usb/desc.h b/pcsx2/USB/qemu-usb/desc.h index 6cb2ed7862..90ebe9797c 100644 --- a/pcsx2/USB/qemu-usb/desc.h +++ b/pcsx2/USB/qemu-usb/desc.h @@ -2,14 +2,7 @@ #include #include - -#if defined(_WIN32) && !defined(__MINGW32__) -#define PACK(def,name) __pragma( pack(push, 1) ) def name __pragma( pack(pop) ) -#elif defined(__clang__) -#define PACK(def,name) def __attribute__((packed)) name -#else -#define PACK(def,name) def __attribute__((gcc_struct, packed)) name -#endif +#include "../platcompat.h" /* binary representation */ PACK(typedef struct USBDescriptor { diff --git a/pcsx2/USB/usb-mic/audiodev.h b/pcsx2/USB/usb-mic/audiodev.h index 4734b6ae9f..c5fcb14cd8 100644 --- a/pcsx2/USB/usb-mic/audiodev.h +++ b/pcsx2/USB/usb-mic/audiodev.h @@ -67,6 +67,12 @@ public: { } +protected: + int mPort; + const char *mDevType; + int mDevice; + AudioDir mAudioDir; +public: virtual ~AudioDevice() {} //get buffer, converted to 16bit int format virtual uint32_t GetBuffer(int16_t *buff, uint32_t len) = 0; @@ -87,12 +93,7 @@ public: //Remember to add to your class //static const wchar_t* GetName(); -protected: - int mPort; - int mDevice; - AudioDir mAudioDir; - const char *mDevType; }; typedef std::vector AudioDeviceInfoList; -#endif \ No newline at end of file +#endif