diff --git a/src/Common/precompiled.h b/src/Common/precompiled.h index 9e5c60f5..1185a34b 100644 --- a/src/Common/precompiled.h +++ b/src/Common/precompiled.h @@ -385,8 +385,6 @@ template constexpr bool HAS_FLAG(T1 flags, T2 test_flag) { return (flags & (T1)test_flag) == (T1)test_flag; } template constexpr bool HAS_BIT(T1 value, T2 index) { return (value & ((T1)1 << index)) != 0; } -template -constexpr void SAFE_RELEASE(T& p) { if (p) { p->Release(); p = nullptr; } } template constexpr uint32_t ppcsizeof() { return (uint32_t) sizeof(T); } diff --git a/src/audio/DirectSoundAPI.cpp b/src/audio/DirectSoundAPI.cpp index eabd3a7e..64042515 100644 --- a/src/audio/DirectSoundAPI.cpp +++ b/src/audio/DirectSoundAPI.cpp @@ -1,9 +1,8 @@ #include "DirectSoundAPI.h" -#include "gui/wxgui.h" - #include "util/helpers/helpers.h" #include "gui/guiWrapper.h" +#include #pragma comment(lib, "Dsound.lib") @@ -15,12 +14,9 @@ std::wstring DirectSoundAPI::DirectSoundDeviceDescription::GetIdentifier() const DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, sint32 samples_per_block, sint32 bits_per_sample) : IAudioAPI(samplerate, channels, samples_per_block, bits_per_sample) { - LPDIRECTSOUND8 direct_sound; - if (DirectSoundCreate8(guid, &direct_sound, nullptr) != DS_OK) + if (DirectSoundCreate8(guid, &m_direct_sound, nullptr) != DS_OK) throw std::runtime_error("can't create directsound device"); - m_direct_sound = decltype(m_direct_sound)(direct_sound); - if (FAILED(m_direct_sound->SetCooperativeLevel(gui_getWindowInfo().window_main.hwnd, DSSCL_PRIORITY))) throw std::runtime_error("can't set directsound priority"); @@ -30,7 +26,7 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s bd.dwBufferBytes = kBufferCount * m_bytesPerBlock; // kBlockCount * (samples_per_block * channels * (bits_per_sample / 8)); bd.lpwfxFormat = (LPWAVEFORMATEX)&m_wfx; - LPDIRECTSOUNDBUFFER sound_buffer; + Microsoft::WRL::ComPtr sound_buffer; if (FAILED(m_direct_sound->CreateSoundBuffer(&bd, &sound_buffer, nullptr))) throw std::runtime_error("can't create directsound soundbuffer"); @@ -41,27 +37,17 @@ DirectSoundAPI::DirectSoundAPI(GUID* guid, sint32 samplerate, sint32 channels, s m_sound_buffer_size = caps.dwBufferBytes; - LPDIRECTSOUNDBUFFER8 sound_buffer8; - LPDIRECTSOUNDNOTIFY8 notify8; - sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, (void**)&sound_buffer8); + Microsoft::WRL::ComPtr notify8; - if (!sound_buffer8) + if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundBuffer8, &m_sound_buffer))) { - sound_buffer->Release(); throw std::runtime_error("can't get directsound buffer interface"); } - m_sound_buffer = decltype(m_sound_buffer)(sound_buffer8); - - sound_buffer->QueryInterface(IID_IDirectSoundNotify8, (void**)¬ify8); - if (!notify8) + if (FAILED(sound_buffer->QueryInterface(IID_IDirectSoundNotify8, &m_notify))) { - sound_buffer->Release(); throw std::runtime_error("can't get directsound notify interface"); } - m_notify = decltype(m_notify)(notify8); - - sound_buffer->Release(); { // initialize sound buffer void *ptr1, *ptr2; @@ -155,10 +141,6 @@ DirectSoundAPI::~DirectSoundAPI() if(m_thread.joinable()) m_thread.join(); - m_notify.reset(); - m_sound_buffer.reset(); - m_direct_sound.reset(); - for(auto entry : m_notify_event) { if (entry) @@ -186,7 +168,7 @@ bool DirectSoundAPI::Stop() bool DirectSoundAPI::FeedBlock(sint16* data) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_buffer.size() > kBlockCount) { cemuLog_logDebug(LogType::Force, "dropped direct sound block since too many buffers are queued"); diff --git a/src/audio/DirectSoundAPI.h b/src/audio/DirectSoundAPI.h index c5ad0d6f..52817fbe 100644 --- a/src/audio/DirectSoundAPI.h +++ b/src/audio/DirectSoundAPI.h @@ -2,8 +2,8 @@ #define DIRECTSOUND_VERSION 0x0800 #include -//#include #include +#include #include "IAudioAPI.h" @@ -41,15 +41,10 @@ public: static std::vector GetInputDevices(); private: - struct DirectSoundDeleter - { - void operator()(IUnknown* ptr) const { if (ptr) ptr->Release(); } - }; - - std::unique_ptr m_direct_sound; - //std::unique_ptr m_direct_sound_capture; - std::unique_ptr m_sound_buffer; - std::unique_ptr m_notify; + Microsoft::WRL::ComPtr m_direct_sound; + //Microsoft::WRL::ComPtr m_direct_sound_capture; + Microsoft::WRL::ComPtr m_sound_buffer; + Microsoft::WRL::ComPtr m_notify; DWORD m_sound_buffer_size = 0; uint32_t m_offset = 0; diff --git a/src/audio/XAudio2API.cpp b/src/audio/XAudio2API.cpp index c92fd451..6b25baed 100644 --- a/src/audio/XAudio2API.cpp +++ b/src/audio/XAudio2API.cpp @@ -2,6 +2,7 @@ //#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/) +#include #include #ifndef XAUDIO2_DLL @@ -33,17 +34,15 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel throw std::runtime_error("can't find XAudio2Create import"); HRESULT hres; - IXAudio2* xaudio; - if (FAILED((hres = _XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR)))) + if (FAILED((hres = _XAudio2Create(&m_xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR)))) throw std::runtime_error(fmt::format("can't create xaudio device (hres: {:#x})", hres)); - m_xaudio = decltype(m_xaudio)(xaudio); IXAudio2MasteringVoice* mastering_voice; if (FAILED((hres = m_xaudio->CreateMasteringVoice(&mastering_voice, channels, samplerate, 0, m_device_id.empty() ? nullptr : m_device_id.c_str())))) throw std::runtime_error(fmt::format("can't create xaudio mastering voice (hres: {:#x})", hres)); - m_mastering_voice = decltype(m_mastering_voice)(mastering_voice); + m_mastering_voice.reset(mastering_voice); m_wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; m_wfx.Format.nChannels = channels; @@ -88,12 +87,6 @@ XAudio2API::XAudio2API(std::wstring device_id, uint32 samplerate, uint32 channel m_xaudio->StartEngine(); } -void XAudio2API::XAudioDeleter::operator()(IXAudio2* ptr) const -{ - if (ptr) - ptr->Release(); -} - void XAudio2API::VoiceDeleter::operator()(IXAudio2Voice* ptr) const { if (ptr) @@ -106,10 +99,6 @@ XAudio2API::~XAudio2API() m_xaudio->StopEngine(); XAudio2API::Stop(); - - m_source_voice.reset(); - m_mastering_voice.reset(); - m_xaudio.reset(); } void XAudio2API::SetVolume(sint32 volume) @@ -179,10 +168,10 @@ const std::vector& XAudio2API::RefreshDevices( try { - struct IWbemLocator *wbem_locator = nullptr; + Microsoft::WRL::ComPtr wbem_locator; - HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWbemLocator), (LPVOID*)&wbem_locator); - if (FAILED(hres) || !wbem_locator) + HRESULT hres = CoCreateInstance(__uuidof(WbemLocator), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wbem_locator)); + if (FAILED(hres)) throw std::system_error(hres, std::system_category()); std::shared_ptr path(SysAllocString(LR"(\\.\root\cimv2)"), SysFreeString); @@ -191,20 +180,19 @@ const std::vector& XAudio2API::RefreshDevices( std::shared_ptr name_row(SysAllocString(L"Name"), SysFreeString); std::shared_ptr device_id_row(SysAllocString(L"DeviceID"), SysFreeString); - IWbemServices *wbem_services = nullptr; + Microsoft::WRL::ComPtr wbem_services; hres = wbem_locator->ConnectServer(path.get(), nullptr, nullptr, nullptr, 0, nullptr, nullptr, &wbem_services); - wbem_locator->Release(); // Free memory resources. - if (FAILED(hres) || !wbem_services) - throw std::system_error(hres, std::system_category()); - - hres = CoSetProxyBlanket(wbem_services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE); if (FAILED(hres)) throw std::system_error(hres, std::system_category()); - IEnumWbemClassObject* wbem_enum = nullptr; + hres = CoSetProxyBlanket(wbem_services.Get(), RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE); + if (FAILED(hres)) + throw std::system_error(hres, std::system_category()); + + Microsoft::WRL::ComPtr wbem_enum; hres = wbem_services->ExecQuery(language.get(), query.get(), WBEM_FLAG_RETURN_WBEM_COMPLETE | WBEM_FLAG_FORWARD_ONLY, nullptr, &wbem_enum); - if (FAILED(hres) || !wbem_enum) + if (FAILED(hres)) throw std::system_error(hres, std::system_category()); ULONG returned; @@ -250,11 +238,6 @@ const std::vector& XAudio2API::RefreshDevices( auto default_device = std::make_shared(L"Primary Sound Driver", L""); s_devices.insert(s_devices.begin(), default_device); } - - wbem_enum->Release(); - - // Clean up - wbem_services->Release(); } catch (const std::system_error& ex) { diff --git a/src/audio/XAudio2API.h b/src/audio/XAudio2API.h index b5bb0296..5bb01b10 100644 --- a/src/audio/XAudio2API.h +++ b/src/audio/XAudio2API.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "IAudioAPI.h" @@ -50,11 +51,6 @@ private: static const std::vector& RefreshDevices(); - struct XAudioDeleter - { - void operator()(IXAudio2* ptr) const; - }; - struct VoiceDeleter { void operator()(IXAudio2Voice* ptr) const; @@ -63,7 +59,7 @@ private: static HMODULE s_xaudio_dll; static std::vector s_devices; - std::unique_ptr m_xaudio; + Microsoft::WRL::ComPtr m_xaudio; std::wstring m_device_id; std::unique_ptr m_mastering_voice; std::unique_ptr m_source_voice; diff --git a/src/gui/components/wxGameList.cpp b/src/gui/components/wxGameList.cpp index d70cb376..587374f6 100644 --- a/src/gui/components/wxGameList.cpp +++ b/src/gui/components/wxGameList.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #endif // public events @@ -1630,8 +1631,8 @@ void wxGameList::CreateShortcut(GameInfo2& gameInfo) } } - IShellLinkW* shellLink; - HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast(&shellLink)); + Microsoft::WRL::ComPtr shellLink; + HRESULT hres = CoCreateInstance(__uuidof(ShellLink), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink)); if (SUCCEEDED(hres)) { const auto description = wxString::Format("Play %s on Cemu", titleName); @@ -1647,15 +1648,13 @@ void wxGameList::CreateShortcut(GameInfo2& gameInfo) else shellLink->SetIconLocation(exePath.wstring().c_str(), 0); - IPersistFile* shellLinkFile; + Microsoft::WRL::ComPtr shellLinkFile; // save the shortcut - hres = shellLink->QueryInterface(IID_IPersistFile, reinterpret_cast(&shellLinkFile)); + hres = shellLink.As(&shellLinkFile); if (SUCCEEDED(hres)) { hres = shellLinkFile->Save(outputPath.wc_str(), TRUE); - shellLinkFile->Release(); } - shellLink->Release(); } if (!SUCCEEDED(hres)) { auto errorMsg = formatWxString(_("Failed to save shortcut to {}"), outputPath); diff --git a/src/input/api/DirectInput/DirectInputController.cpp b/src/input/api/DirectInput/DirectInputController.cpp index dbb7c80c..234696b6 100644 --- a/src/input/api/DirectInput/DirectInputController.cpp +++ b/src/input/api/DirectInput/DirectInputController.cpp @@ -15,9 +15,6 @@ DirectInputController::DirectInputController(const GUID& guid, std::string_view DirectInputController::~DirectInputController() { - if (m_effect) - m_effect->Release(); - if (m_device) { m_device->Unacquire(); @@ -39,8 +36,8 @@ DirectInputController::~DirectInputController() if (kGameCubeController == m_product_guid) should_release_device = false; - if (should_release_device) - m_device->Release(); + if (!should_release_device) + m_device.Detach(); } } @@ -104,7 +101,6 @@ bool DirectInputController::connect() // set data format if (FAILED(m_device->SetDataFormat(m_provider->get_data_format()))) { - SAFE_RELEASE(m_device); return false; } @@ -115,7 +111,6 @@ bool DirectInputController::connect() { if (FAILED(m_device->SetCooperativeLevel(hwndMainWindow, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) { - SAFE_RELEASE(m_device); return false; } // rumble can only be used with exclusive access diff --git a/src/input/api/DirectInput/DirectInputController.h b/src/input/api/DirectInput/DirectInputController.h index 2ec371a2..d2c3dba2 100644 --- a/src/input/api/DirectInput/DirectInputController.h +++ b/src/input/api/DirectInput/DirectInputController.h @@ -2,6 +2,7 @@ #include "input/api/DirectInput/DirectInputControllerProvider.h" #include "input/api/Controller.h" +#include class DirectInputController : public Controller { @@ -41,9 +42,9 @@ private: GUID m_product_guid{}; std::shared_mutex m_mutex; - LPDIRECTINPUTDEVICE8 m_device = nullptr; - LPDIRECTINPUTEFFECT m_effect = nullptr; + Microsoft::WRL::ComPtr m_device; + Microsoft::WRL::ComPtr m_effect; std::array m_min_axis{}; std::array m_max_axis{}; -}; \ No newline at end of file +}; diff --git a/src/input/api/DirectInput/DirectInputControllerProvider.cpp b/src/input/api/DirectInput/DirectInputControllerProvider.cpp index 063cb779..79f31354 100644 --- a/src/input/api/DirectInput/DirectInputControllerProvider.cpp +++ b/src/input/api/DirectInput/DirectInputControllerProvider.cpp @@ -19,7 +19,7 @@ DirectInputControllerProvider::DirectInputControllerProvider() const auto r = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_dinput8, nullptr); - if (FAILED(r) || !m_dinput8) + if (FAILED(r)) { const auto error = GetLastError(); //FreeLibrary(m_module); @@ -29,9 +29,6 @@ DirectInputControllerProvider::DirectInputControllerProvider() DirectInputControllerProvider::~DirectInputControllerProvider() { - if (m_dinput8) - m_dinput8->Release(); - /*if (m_module) FreeLibrary(m_module); */ diff --git a/src/input/api/DirectInput/DirectInputControllerProvider.h b/src/input/api/DirectInput/DirectInputControllerProvider.h index 5db883c0..de8c3216 100644 --- a/src/input/api/DirectInput/DirectInputControllerProvider.h +++ b/src/input/api/DirectInput/DirectInputControllerProvider.h @@ -4,6 +4,7 @@ #define DIRECTINPUT_VERSION 0x0800 #include +#include #include "input/api/ControllerProvider.h" @@ -22,7 +23,7 @@ public: std::vector> get_controllers() override; - IDirectInput8* get_dinput() const { return m_dinput8; } + IDirectInput8* get_dinput() const { return m_dinput8.Get(); } LPCDIDATAFORMAT get_data_format() const; private: @@ -31,7 +32,7 @@ private: decltype(&DirectInput8Create) m_DirectInput8Create; decltype(&GetdfDIJoystick) m_GetdfDIJoystick = nullptr; - IDirectInput8* m_dinput8 = nullptr; + Microsoft::WRL::ComPtr m_dinput8; }; -#endif \ No newline at end of file +#endif diff --git a/src/util/DXGIWrapper/DXGIWrapper.h b/src/util/DXGIWrapper/DXGIWrapper.h index 54f2454d..363733e7 100644 --- a/src/util/DXGIWrapper/DXGIWrapper.h +++ b/src/util/DXGIWrapper/DXGIWrapper.h @@ -1,7 +1,7 @@ #pragma once #include -//#include +#include class DXGIWrapper { @@ -23,34 +23,28 @@ public: throw std::runtime_error("can't find CreateDXGIFactory1 in dxgi module"); } - IDXGIFactory1* dxgiFactory = nullptr; + Microsoft::WRL::ComPtr dxgiFactory; pCreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)); - IDXGIAdapter1* tmpDxgiAdapter = nullptr; + Microsoft::WRL::ComPtr dxgiAdapter; UINT adapterIndex = 0; - while (dxgiFactory->EnumAdapters1(adapterIndex, &tmpDxgiAdapter) != DXGI_ERROR_NOT_FOUND) + while (dxgiFactory->EnumAdapters1(adapterIndex, &dxgiAdapter) != DXGI_ERROR_NOT_FOUND) { DXGI_ADAPTER_DESC1 desc; - tmpDxgiAdapter->GetDesc1(&desc); + dxgiAdapter->GetDesc1(&desc); if (deviceLUID == nullptr || memcmp(&desc.AdapterLuid, deviceLUID, sizeof(LUID)) == 0) { - tmpDxgiAdapter->QueryInterface(IID_PPV_ARGS(&m_dxgiAdapter)); - tmpDxgiAdapter->Release(); + if (FAILED(dxgiAdapter.As(&m_dxgiAdapter))) + { + Cleanup(); + throw std::runtime_error("can't create dxgi adapter"); + } break; } - tmpDxgiAdapter->Release(); ++adapterIndex; } - - dxgiFactory->Release(); - - if (!m_dxgiAdapter) - { - Cleanup(); - throw std::runtime_error("can't create dxgi adapter"); - } } ~DXGIWrapper() @@ -65,15 +59,11 @@ public: private: HMODULE m_moduleHandle = nullptr; - IDXGIAdapter3* m_dxgiAdapter = nullptr; + Microsoft::WRL::ComPtr m_dxgiAdapter; void Cleanup() { - if (m_dxgiAdapter) - { - m_dxgiAdapter->Release(); - m_dxgiAdapter = nullptr; - } + m_dxgiAdapter.Reset(); if (m_moduleHandle) { @@ -81,4 +71,4 @@ private: m_moduleHandle = nullptr; } } -}; \ No newline at end of file +};