The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

332 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #define JUCE_NATIVE_ACCESSIBILITY_INCLUDED 1
  21. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  22. static bool isStartingUpOrShuttingDown()
  23. {
  24. if (auto* app = JUCEApplicationBase::getInstance())
  25. if (app->isInitialising())
  26. return true;
  27. if (auto* mm = MessageManager::getInstanceWithoutCreating())
  28. if (mm->hasStopMessageBeenSent())
  29. return true;
  30. return false;
  31. }
  32. static bool isHandlerValid (const AccessibilityHandler& handler)
  33. {
  34. if (auto* provider = handler.getNativeImplementation())
  35. return provider->isElementValid();
  36. return false;
  37. }
  38. //==============================================================================
  39. class AccessibilityHandler::AccessibilityNativeImpl
  40. {
  41. public:
  42. explicit AccessibilityNativeImpl (AccessibilityHandler& owner)
  43. : accessibilityElement (new AccessibilityNativeHandle (owner))
  44. {
  45. ++providerCount;
  46. }
  47. ~AccessibilityNativeImpl()
  48. {
  49. ComSmartPtr<IRawElementProviderSimple> provider;
  50. accessibilityElement->QueryInterface (IID_PPV_ARGS (provider.resetAndGetPointerAddress()));
  51. accessibilityElement->invalidateElement();
  52. --providerCount;
  53. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  54. {
  55. uiaWrapper->disconnectProvider (provider);
  56. if (providerCount == 0 && JUCEApplicationBase::isStandaloneApp())
  57. uiaWrapper->disconnectAllProviders();
  58. }
  59. }
  60. //==============================================================================
  61. ComSmartPtr<AccessibilityNativeHandle> accessibilityElement;
  62. static int providerCount;
  63. //==============================================================================
  64. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AccessibilityNativeImpl)
  65. };
  66. int AccessibilityHandler::AccessibilityNativeImpl::providerCount = 0;
  67. //==============================================================================
  68. AccessibilityNativeHandle* AccessibilityHandler::getNativeImplementation() const
  69. {
  70. return nativeImpl->accessibilityElement;
  71. }
  72. static bool areAnyAccessibilityClientsActive()
  73. {
  74. const auto areClientsListening = []
  75. {
  76. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  77. return uiaWrapper->clientsAreListening() != 0;
  78. return false;
  79. };
  80. const auto isScreenReaderRunning = []
  81. {
  82. BOOL isRunning = FALSE;
  83. SystemParametersInfo (SPI_GETSCREENREADER, 0, (PVOID) &isRunning, 0);
  84. return isRunning != 0;
  85. };
  86. return areClientsListening() || isScreenReaderRunning();
  87. }
  88. template <typename Callback>
  89. void getProviderWithCheckedWrapper (const AccessibilityHandler& handler, Callback&& callback)
  90. {
  91. if (! areAnyAccessibilityClientsActive() || isStartingUpOrShuttingDown() || ! isHandlerValid (handler))
  92. return;
  93. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  94. {
  95. ComSmartPtr<IRawElementProviderSimple> provider;
  96. handler.getNativeImplementation()->QueryInterface (IID_PPV_ARGS (provider.resetAndGetPointerAddress()));
  97. callback (uiaWrapper, provider);
  98. }
  99. }
  100. void sendAccessibilityAutomationEvent (const AccessibilityHandler& handler, EVENTID event)
  101. {
  102. jassert (event != EVENTID{});
  103. getProviderWithCheckedWrapper (handler, [event] (WindowsUIAWrapper* uiaWrapper, ComSmartPtr<IRawElementProviderSimple>& provider)
  104. {
  105. uiaWrapper->raiseAutomationEvent (provider, event);
  106. });
  107. }
  108. void sendAccessibilityPropertyChangedEvent (const AccessibilityHandler& handler, PROPERTYID property, VARIANT newValue)
  109. {
  110. jassert (property != PROPERTYID{});
  111. getProviderWithCheckedWrapper (handler, [property, newValue] (WindowsUIAWrapper* uiaWrapper, ComSmartPtr<IRawElementProviderSimple>& provider)
  112. {
  113. VARIANT oldValue;
  114. VariantHelpers::clear (&oldValue);
  115. uiaWrapper->raiseAutomationPropertyChangedEvent (provider, property, oldValue, newValue);
  116. });
  117. }
  118. void notifyAccessibilityEventInternal (const AccessibilityHandler& handler, InternalAccessibilityEvent eventType)
  119. {
  120. using namespace ComTypes::Constants;
  121. if (eventType == InternalAccessibilityEvent::elementCreated
  122. || eventType == InternalAccessibilityEvent::elementDestroyed)
  123. {
  124. if (auto* parent = handler.getParent())
  125. sendAccessibilityAutomationEvent (*parent, UIA_LayoutInvalidatedEventId);
  126. return;
  127. }
  128. if (eventType == InternalAccessibilityEvent::windowOpened
  129. || eventType == InternalAccessibilityEvent::windowClosed)
  130. {
  131. if (auto* peer = handler.getComponent().getPeer())
  132. if ((peer->getStyleFlags() & ComponentPeer::windowHasTitleBar) == 0)
  133. return;
  134. }
  135. auto event = [eventType]() -> EVENTID
  136. {
  137. switch (eventType)
  138. {
  139. case InternalAccessibilityEvent::focusChanged: return UIA_AutomationFocusChangedEventId;
  140. case InternalAccessibilityEvent::windowOpened: return UIA_Window_WindowOpenedEventId;
  141. case InternalAccessibilityEvent::windowClosed: return UIA_Window_WindowClosedEventId;
  142. case InternalAccessibilityEvent::elementCreated:
  143. case InternalAccessibilityEvent::elementDestroyed:
  144. case InternalAccessibilityEvent::elementMovedOrResized: break;
  145. }
  146. return {};
  147. }();
  148. if (event != EVENTID{})
  149. sendAccessibilityAutomationEvent (handler, event);
  150. }
  151. void AccessibilityHandler::notifyAccessibilityEvent (AccessibilityEvent eventType) const
  152. {
  153. if (eventType == AccessibilityEvent::titleChanged)
  154. {
  155. VARIANT newValue;
  156. VariantHelpers::setString (getTitle(), &newValue);
  157. sendAccessibilityPropertyChangedEvent (*this, UIA_NamePropertyId, newValue);
  158. return;
  159. }
  160. if (eventType == AccessibilityEvent::valueChanged)
  161. {
  162. if (auto* valueInterface = getValueInterface())
  163. {
  164. const auto propertyType = getRole() == AccessibilityRole::slider ? UIA_RangeValueValuePropertyId
  165. : UIA_ValueValuePropertyId;
  166. const auto value = getRole() == AccessibilityRole::slider
  167. ? VariantHelpers::getWithValue (valueInterface->getCurrentValue())
  168. : VariantHelpers::getWithValue (valueInterface->getCurrentValueAsString());
  169. sendAccessibilityPropertyChangedEvent (*this, propertyType, value);
  170. }
  171. return;
  172. }
  173. auto event = [eventType]() -> EVENTID
  174. {
  175. using namespace ComTypes::Constants;
  176. switch (eventType)
  177. {
  178. case AccessibilityEvent::textSelectionChanged: return UIA_Text_TextSelectionChangedEventId;
  179. case AccessibilityEvent::textChanged: return UIA_Text_TextChangedEventId;
  180. case AccessibilityEvent::structureChanged: return UIA_StructureChangedEventId;
  181. case AccessibilityEvent::rowSelectionChanged: return UIA_SelectionItem_ElementSelectedEventId;
  182. case AccessibilityEvent::titleChanged:
  183. case AccessibilityEvent::valueChanged: break;
  184. }
  185. return {};
  186. }();
  187. if (event != EVENTID{})
  188. sendAccessibilityAutomationEvent (*this, event);
  189. }
  190. struct SpVoiceWrapper : public DeletedAtShutdown
  191. {
  192. SpVoiceWrapper()
  193. {
  194. auto hr = voice.CoCreateInstance (ComTypes::CLSID_SpVoice);
  195. jassertquiet (SUCCEEDED (hr));
  196. }
  197. ~SpVoiceWrapper() override
  198. {
  199. clearSingletonInstance();
  200. }
  201. ComSmartPtr<ISpVoice> voice;
  202. JUCE_DECLARE_SINGLETON (SpVoiceWrapper, false)
  203. };
  204. JUCE_IMPLEMENT_SINGLETON (SpVoiceWrapper)
  205. void AccessibilityHandler::postAnnouncement (const String& announcementString, AnnouncementPriority priority)
  206. {
  207. if (! areAnyAccessibilityClientsActive())
  208. return;
  209. if (auto* sharedVoice = SpVoiceWrapper::getInstance())
  210. {
  211. auto voicePriority = [priority]
  212. {
  213. switch (priority)
  214. {
  215. case AnnouncementPriority::low: return SPVPRI_OVER;
  216. case AnnouncementPriority::medium: return SPVPRI_NORMAL;
  217. case AnnouncementPriority::high: return SPVPRI_ALERT;
  218. }
  219. jassertfalse;
  220. return SPVPRI_OVER;
  221. }();
  222. sharedVoice->voice->SetPriority (voicePriority);
  223. sharedVoice->voice->Speak (announcementString.toWideCharPointer(), SPF_ASYNC, nullptr);
  224. }
  225. }
  226. //==============================================================================
  227. namespace WindowsAccessibility
  228. {
  229. static long getUiaRootObjectId()
  230. {
  231. return static_cast<long> (UiaRootObjectId);
  232. }
  233. static bool handleWmGetObject (AccessibilityHandler* handler, WPARAM wParam, LPARAM lParam, LRESULT* res)
  234. {
  235. if (isStartingUpOrShuttingDown() || (handler == nullptr || ! isHandlerValid (*handler)))
  236. return false;
  237. if (auto* uiaWrapper = WindowsUIAWrapper::getInstance())
  238. {
  239. ComSmartPtr<IRawElementProviderSimple> provider;
  240. handler->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (provider.resetAndGetPointerAddress()));
  241. if (! uiaWrapper->isProviderDisconnecting (provider))
  242. *res = uiaWrapper->returnRawElementProvider ((HWND) handler->getComponent().getWindowHandle(), wParam, lParam, provider);
  243. return true;
  244. }
  245. return false;
  246. }
  247. static void revokeUIAMapEntriesForWindow (HWND hwnd)
  248. {
  249. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  250. uiaWrapper->returnRawElementProvider (hwnd, 0, 0, nullptr);
  251. }
  252. }
  253. JUCE_IMPLEMENT_SINGLETON (WindowsUIAWrapper)
  254. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  255. } // namespace juce