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.

313 lines
10KB

  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. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. accessibilityElement->invalidateElement();
  50. --providerCount;
  51. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  52. {
  53. ComSmartPtr<IRawElementProviderSimple> provider;
  54. accessibilityElement->QueryInterface (IID_PPV_ARGS (provider.resetAndGetPointerAddress()));
  55. uiaWrapper->disconnectProvider (provider);
  56. if (providerCount == 0)
  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. 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. if (eventType == InternalAccessibilityEvent::elementCreated
  121. || eventType == InternalAccessibilityEvent::elementDestroyed)
  122. {
  123. if (auto* parent = handler.getParent())
  124. sendAccessibilityAutomationEvent (*parent, UIA_LayoutInvalidatedEventId);
  125. return;
  126. }
  127. auto event = [eventType]() -> EVENTID
  128. {
  129. switch (eventType)
  130. {
  131. case InternalAccessibilityEvent::focusChanged: return UIA_AutomationFocusChangedEventId;
  132. case InternalAccessibilityEvent::windowOpened: return UIA_Window_WindowOpenedEventId;
  133. case InternalAccessibilityEvent::windowClosed: return UIA_Window_WindowClosedEventId;
  134. case InternalAccessibilityEvent::elementCreated:
  135. case InternalAccessibilityEvent::elementDestroyed:
  136. case InternalAccessibilityEvent::elementMovedOrResized: break;
  137. }
  138. return {};
  139. }();
  140. if (event != EVENTID{})
  141. sendAccessibilityAutomationEvent (handler, event);
  142. }
  143. void AccessibilityHandler::notifyAccessibilityEvent (AccessibilityEvent eventType) const
  144. {
  145. if (eventType == AccessibilityEvent::titleChanged)
  146. {
  147. VARIANT newValue;
  148. VariantHelpers::setString (getTitle(), &newValue);
  149. sendAccessibilityPropertyChangedEvent (*this, UIA_NamePropertyId, newValue);
  150. }
  151. auto event = [eventType] () -> EVENTID
  152. {
  153. switch (eventType)
  154. {
  155. case AccessibilityEvent::textSelectionChanged: return UIA_Text_TextSelectionChangedEventId;
  156. case AccessibilityEvent::textChanged: return UIA_Text_TextChangedEventId;
  157. case AccessibilityEvent::structureChanged: return UIA_StructureChangedEventId;
  158. case AccessibilityEvent::rowSelectionChanged: return UIA_SelectionItem_ElementSelectedEventId;
  159. case AccessibilityEvent::titleChanged:
  160. case AccessibilityEvent::valueChanged: break;
  161. }
  162. return {};
  163. }();
  164. if (event != EVENTID{})
  165. sendAccessibilityAutomationEvent (*this, event);
  166. }
  167. struct SpVoiceWrapper : public DeletedAtShutdown
  168. {
  169. SpVoiceWrapper()
  170. {
  171. auto hr = voice.CoCreateInstance (CLSID_SpVoice);
  172. jassert (SUCCEEDED (hr));
  173. ignoreUnused (hr);
  174. }
  175. ~SpVoiceWrapper() override
  176. {
  177. clearSingletonInstance();
  178. }
  179. ComSmartPtr<ISpVoice> voice;
  180. JUCE_DECLARE_SINGLETON (SpVoiceWrapper, false)
  181. };
  182. JUCE_IMPLEMENT_SINGLETON (SpVoiceWrapper)
  183. void AccessibilityHandler::postAnnouncement (const String& announcementString, AnnouncementPriority priority)
  184. {
  185. if (! areAnyAccessibilityClientsActive())
  186. return;
  187. if (auto* sharedVoice = SpVoiceWrapper::getInstance())
  188. {
  189. auto voicePriority = [priority]
  190. {
  191. switch (priority)
  192. {
  193. case AnnouncementPriority::low: return SPVPRI_OVER;
  194. case AnnouncementPriority::medium: return SPVPRI_NORMAL;
  195. case AnnouncementPriority::high: return SPVPRI_ALERT;
  196. }
  197. jassertfalse;
  198. return SPVPRI_OVER;
  199. }();
  200. sharedVoice->voice->SetPriority (voicePriority);
  201. sharedVoice->voice->Speak (announcementString.toWideCharPointer(), SPF_ASYNC, nullptr);
  202. }
  203. }
  204. AccessibilityHandler::AccessibilityNativeImpl* AccessibilityHandler::createNativeImpl (AccessibilityHandler& handler)
  205. {
  206. return new AccessibilityHandler::AccessibilityNativeImpl (handler);
  207. }
  208. void AccessibilityHandler::DestroyNativeImpl::operator() (AccessibilityHandler::AccessibilityNativeImpl* impl) const noexcept
  209. {
  210. delete impl;
  211. }
  212. //==============================================================================
  213. namespace WindowsAccessibility
  214. {
  215. long getUiaRootObjectId()
  216. {
  217. return static_cast<long> (UiaRootObjectId);
  218. }
  219. bool handleWmGetObject (AccessibilityHandler* handler, WPARAM wParam, LPARAM lParam, LRESULT* res)
  220. {
  221. if (isStartingUpOrShuttingDown() || (handler == nullptr || ! isHandlerValid (*handler)))
  222. return false;
  223. if (auto* uiaWrapper = WindowsUIAWrapper::getInstance())
  224. {
  225. ComSmartPtr<IRawElementProviderSimple> provider;
  226. handler->getNativeImplementation()->QueryInterface (IID_PPV_ARGS (provider.resetAndGetPointerAddress()));
  227. if (! uiaWrapper->isProviderDisconnecting (provider))
  228. *res = uiaWrapper->returnRawElementProvider ((HWND) handler->getComponent().getWindowHandle(), wParam, lParam, provider);
  229. return true;
  230. }
  231. return false;
  232. }
  233. void revokeUIAMapEntriesForWindow (HWND hwnd)
  234. {
  235. if (auto* uiaWrapper = WindowsUIAWrapper::getInstanceWithoutCreating())
  236. uiaWrapper->returnRawElementProvider (hwnd, 0, 0, nullptr);
  237. }
  238. }
  239. JUCE_IMPLEMENT_SINGLETON (WindowsUIAWrapper)
  240. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  241. } // namespace juce