Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. class WinRTWrapper : public DeletedAtShutdown
  20. {
  21. public:
  22. class ScopedHString
  23. {
  24. public:
  25. ScopedHString (String);
  26. ~ScopedHString();
  27. HSTRING get() const noexcept { return hstr; }
  28. private:
  29. HSTRING hstr = nullptr;
  30. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
  31. };
  32. template <class ComClass>
  33. class ComPtr
  34. {
  35. public:
  36. ComPtr() noexcept {}
  37. ComPtr (ComClass* obj) : p (obj) { if (p) p->AddRef(); }
  38. ComPtr (const ComPtr& other) : p (other.p) { if (p) p->AddRef(); }
  39. ~ComPtr() { release(); }
  40. operator ComClass*() const noexcept { return p; }
  41. ComClass* get() const noexcept { return p; }
  42. ComClass& operator*() const noexcept { return *p; }
  43. ComClass* operator->() const noexcept { return p; }
  44. ComPtr& operator= (ComClass* const newP)
  45. {
  46. if (newP != nullptr)
  47. newP->AddRef();
  48. release();
  49. p = newP;
  50. return *this;
  51. }
  52. ComPtr& operator= (const ComPtr& newP) { return operator= (newP.p); }
  53. ComClass** resetAndGetPointerAddress()
  54. {
  55. release();
  56. p = nullptr;
  57. return &p;
  58. }
  59. private:
  60. ComClass* p = nullptr;
  61. void release() { if (p != nullptr) p->Release(); }
  62. ComClass** operator&() noexcept; // private to avoid it being used accidentally
  63. };
  64. JUCE_DECLARE_SINGLETON (WinRTWrapper, true)
  65. ~WinRTWrapper();
  66. String hStringToString (HSTRING);
  67. bool isInitialised() const noexcept { return initialised; }
  68. template <class ComClass>
  69. ComPtr<ComClass> activateInstance (const wchar_t* runtimeClassID, REFCLSID classUUID)
  70. {
  71. ComPtr<ComClass> result;
  72. if (isInitialised())
  73. {
  74. ComPtr<IInspectable> inspectable;
  75. ScopedHString runtimeClass (runtimeClassID);
  76. auto hr = roActivateInstance (runtimeClass.get(), inspectable.resetAndGetPointerAddress());
  77. if (SUCCEEDED (hr))
  78. inspectable->QueryInterface (classUUID, (void**) result.resetAndGetPointerAddress());
  79. }
  80. return result;
  81. }
  82. template <class ComClass>
  83. ComPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
  84. {
  85. ComPtr<ComClass> comPtr;
  86. if (isInitialised())
  87. {
  88. ScopedHString classID (runtimeClassID);
  89. if (classID.get() != nullptr)
  90. roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
  91. }
  92. return comPtr;
  93. }
  94. private:
  95. WinRTWrapper();
  96. HMODULE winRTHandle = nullptr;
  97. bool initialised = false;
  98. typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
  99. typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
  100. typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
  101. typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
  102. typedef HRESULT (WINAPI* RoActivateInstanceFuncPtr) (HSTRING, IInspectable**);
  103. typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
  104. RoInitializeFuncPtr roInitialize = nullptr;
  105. WindowsCreateStringFuncPtr createHString = nullptr;
  106. WindowsDeleteStringFuncPtr deleteHString = nullptr;
  107. WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
  108. RoActivateInstanceFuncPtr roActivateInstance = nullptr;
  109. RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
  110. };
  111. } // namespace juce