1*b0724fc6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b0724fc6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b0724fc6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b0724fc6SAndrew Rist  * distributed with this work for additional information
6*b0724fc6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b0724fc6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b0724fc6SAndrew Rist  * "License"); you may not use this file except in compliance
9*b0724fc6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b0724fc6SAndrew Rist  *
11*b0724fc6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b0724fc6SAndrew Rist  *
13*b0724fc6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b0724fc6SAndrew Rist  * software distributed under the License is distributed on an
15*b0724fc6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b0724fc6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b0724fc6SAndrew Rist  * specific language governing permissions and limitations
18*b0724fc6SAndrew Rist  * under the License.
19*b0724fc6SAndrew Rist  *
20*b0724fc6SAndrew Rist  *************************************************************/
21*b0724fc6SAndrew Rist 
22*b0724fc6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_toolkit.hxx"
26cdf0e10cSrcweir #include <toolkit/helper/accessibilityclient.hxx>
27cdf0e10cSrcweir #include <toolkit/helper/accessiblefactory.hxx>
28cdf0e10cSrcweir #include <osl/module.h>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include <tools/solar.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // #define UNLOAD_ON_LAST_CLIENT_DYING
33cdf0e10cSrcweir     // this is not recommended currently. If enabled, the implementation will log
34cdf0e10cSrcweir     // the number of active clients, and unload the acc library when the last client
35cdf0e10cSrcweir     // goes away.
36cdf0e10cSrcweir     // Sounds like a good idea, unfortunately, there's no guarantee that all objects
37cdf0e10cSrcweir     // implemented in this library are already dead.
38cdf0e10cSrcweir     // Iow, just because an object implementing an XAccessible (implemented in this lib
39cdf0e10cSrcweir     // here) died, it's not said that everybody released all references to the
40cdf0e10cSrcweir     // XAccessibleContext used by this component, and implemented in the acc lib.
41cdf0e10cSrcweir     // So we cannot really unload the lib.
42cdf0e10cSrcweir     //
43cdf0e10cSrcweir     // Alternatively, if the lib would us own "usage counting", i.e. every component
44cdf0e10cSrcweir     // implemented therein would affect a static ref count, the acc lib could care
45cdf0e10cSrcweir     // for unloading itself.
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //........................................................................
48cdf0e10cSrcweir namespace toolkit
49cdf0e10cSrcweir {
50cdf0e10cSrcweir //........................................................................
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
53cdf0e10cSrcweir     using namespace ::com::sun::star::accessibility;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     namespace
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
58cdf0e10cSrcweir         static oslInterlockedCount                      s_nAccessibilityClients = 0;
59cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
60cdf0e10cSrcweir         static oslModule                                s_hAccessibleImplementationModule = NULL;
61cdf0e10cSrcweir         static GetStandardAccComponentFactory           s_pAccessibleFactoryFunc = NULL;
62cdf0e10cSrcweir         static ::rtl::Reference< IAccessibleFactory >   s_pFactory;
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     //====================================================================
66cdf0e10cSrcweir 	//= AccessibleDummyFactory
67cdf0e10cSrcweir 	//====================================================================
68cdf0e10cSrcweir     class AccessibleDummyFactory : public IAccessibleFactory
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir     public:
71cdf0e10cSrcweir         AccessibleDummyFactory();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     protected:
74cdf0e10cSrcweir         virtual ~AccessibleDummyFactory();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     private:
77cdf0e10cSrcweir         AccessibleDummyFactory( const AccessibleDummyFactory& );            // never implemented
78cdf0e10cSrcweir         AccessibleDummyFactory& operator=( const AccessibleDummyFactory& ); // never implemented
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         oslInterlockedCount m_refCount;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     public:
83cdf0e10cSrcweir         // IReference
84cdf0e10cSrcweir 	    virtual oslInterlockedCount SAL_CALL acquire();
85cdf0e10cSrcweir 	    virtual oslInterlockedCount SAL_CALL release();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         // IAccessibleFactory
88cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXButton *)89cdf0e10cSrcweir                 createAccessibleContext( VCLXButton* /*_pXWindow*/ )
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             return NULL;
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXCheckBox *)94cdf0e10cSrcweir                 createAccessibleContext( VCLXCheckBox* /*_pXWindow*/ )
95cdf0e10cSrcweir         {
96cdf0e10cSrcweir             return NULL;
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXRadioButton *)99cdf0e10cSrcweir                 createAccessibleContext( VCLXRadioButton* /*_pXWindow*/ )
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             return NULL;
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXListBox *)104cdf0e10cSrcweir                 createAccessibleContext( VCLXListBox* /*_pXWindow*/ )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             return NULL;
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXFixedHyperlink *)109cdf0e10cSrcweir                 createAccessibleContext( VCLXFixedHyperlink* /*_pXWindow*/ )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             return NULL;
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXFixedText *)114cdf0e10cSrcweir                 createAccessibleContext( VCLXFixedText* /*_pXWindow*/ )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             return NULL;
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXScrollBar *)119cdf0e10cSrcweir                 createAccessibleContext( VCLXScrollBar* /*_pXWindow*/ )
120cdf0e10cSrcweir         {
121cdf0e10cSrcweir             return NULL;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXEdit *)124cdf0e10cSrcweir                 createAccessibleContext( VCLXEdit* /*_pXWindow*/ )
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             return NULL;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXComboBox *)129cdf0e10cSrcweir                 createAccessibleContext( VCLXComboBox* /*_pXWindow*/ )
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             return NULL;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXToolBox *)134cdf0e10cSrcweir                 createAccessibleContext( VCLXToolBox* /*_pXWindow*/ )
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             return NULL;
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
createAccessibleContext(VCLXWindow *)139cdf0e10cSrcweir                 createAccessibleContext( VCLXWindow* /*_pXWindow*/ )
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             return NULL;
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible >
createAccessible(Menu *,sal_Bool)144cdf0e10cSrcweir                 createAccessible( Menu* /*_pMenu*/, sal_Bool /*_bIsMenuBar*/ )
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             return NULL;
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir     };
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	//--------------------------------------------------------------------
AccessibleDummyFactory()151cdf0e10cSrcweir     AccessibleDummyFactory::AccessibleDummyFactory()
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	//--------------------------------------------------------------------
~AccessibleDummyFactory()156cdf0e10cSrcweir     AccessibleDummyFactory::~AccessibleDummyFactory()
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	//--------------------------------------------------------------------
acquire()161cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AccessibleDummyFactory::acquire()
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_refCount );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	//--------------------------------------------------------------------
release()167cdf0e10cSrcweir 	oslInterlockedCount SAL_CALL AccessibleDummyFactory::release()
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
170cdf0e10cSrcweir         {
171cdf0e10cSrcweir             delete this;
172cdf0e10cSrcweir             return 0;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir         return m_refCount;
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     //====================================================================
178cdf0e10cSrcweir 	//= AccessibilityClient
179cdf0e10cSrcweir 	//====================================================================
180cdf0e10cSrcweir 	//--------------------------------------------------------------------
AccessibilityClient()181cdf0e10cSrcweir     AccessibilityClient::AccessibilityClient()
182cdf0e10cSrcweir         :m_bInitialized( false )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	//--------------------------------------------------------------------
thisModule()187cdf0e10cSrcweir     extern "C" { static void SAL_CALL thisModule() {} }
188cdf0e10cSrcweir 
ensureInitialized()189cdf0e10cSrcweir     void AccessibilityClient::ensureInitialized()
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         if ( m_bInitialized )
192cdf0e10cSrcweir             return;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
197cdf0e10cSrcweir         if ( 1 == osl_incrementInterlockedCount( &s_nAccessibilityClients ) )
198cdf0e10cSrcweir         {   // the first client
199cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
200cdf0e10cSrcweir             // load the library implementing the factory
201cdf0e10cSrcweir             if ( !s_pFactory.get() )
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir 			    const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
204cdf0e10cSrcweir 				    SVLIBRARY( "acc" )
205cdf0e10cSrcweir 			    );
206cdf0e10cSrcweir                 s_hAccessibleImplementationModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, 0 );
207cdf0e10cSrcweir                 if ( s_hAccessibleImplementationModule != NULL )
208cdf0e10cSrcweir                 {
209cdf0e10cSrcweir 				    const ::rtl::OUString sFactoryCreationFunc =
210cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii( "getStandardAccessibleFactory" );
211cdf0e10cSrcweir 				    s_pAccessibleFactoryFunc = (GetStandardAccComponentFactory)
212cdf0e10cSrcweir                         osl_getFunctionSymbol( s_hAccessibleImplementationModule, sFactoryCreationFunc.pData );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir                 }
215cdf0e10cSrcweir                 OSL_ENSURE( s_pAccessibleFactoryFunc, "AccessibilityClient::ensureInitialized: could not load the library, or not retrieve the needed symbol!" );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                 // get a factory instance
218cdf0e10cSrcweir                 if ( s_pAccessibleFactoryFunc )
219cdf0e10cSrcweir                 {
220cdf0e10cSrcweir                     IAccessibleFactory* pFactory = static_cast< IAccessibleFactory* >( (*s_pAccessibleFactoryFunc)() );
221cdf0e10cSrcweir                     OSL_ENSURE( pFactory, "AccessibilityClient::ensureInitialized: no factory provided by the A11Y lib!" );
222cdf0e10cSrcweir                     if ( pFactory )
223cdf0e10cSrcweir                     {
224cdf0e10cSrcweir                         s_pFactory = pFactory;
225cdf0e10cSrcweir                         pFactory->release();
226cdf0e10cSrcweir                     }
227cdf0e10cSrcweir                 }
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir             if ( !s_pFactory.get() )
231cdf0e10cSrcweir                 // the attempt to load the lib, or to create the factory, failed
232cdf0e10cSrcweir                 // -> fall back to a dummy factory
233cdf0e10cSrcweir                 s_pFactory = new AccessibleDummyFactory;
234cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir #endif
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         m_bInitialized = true;
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	//--------------------------------------------------------------------
~AccessibilityClient()242cdf0e10cSrcweir     AccessibilityClient::~AccessibilityClient()
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         if ( m_bInitialized )
245cdf0e10cSrcweir         {
246cdf0e10cSrcweir             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
247cdf0e10cSrcweir 
248cdf0e10cSrcweir #ifdef UNLOAD_ON_LAST_CLIENT_DYING
249cdf0e10cSrcweir             if( 0 == osl_decrementInterlockedCount( &s_nAccessibilityClients ) )
250cdf0e10cSrcweir             {
251cdf0e10cSrcweir                 s_pFactory = NULL;
252cdf0e10cSrcweir                 s_pAccessibleFactoryFunc = NULL;
253cdf0e10cSrcweir                 if ( s_hAccessibleImplementationModule )
254cdf0e10cSrcweir                 {
255cdf0e10cSrcweir                     osl_unloadModule( s_hAccessibleImplementationModule );
256cdf0e10cSrcweir                     s_hAccessibleImplementationModule = NULL;
257cdf0e10cSrcweir                 }
258cdf0e10cSrcweir             }
259cdf0e10cSrcweir #endif // UNLOAD_ON_LAST_CLIENT_DYING
260cdf0e10cSrcweir         }
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     //--------------------------------------------------------------------
getFactory()264cdf0e10cSrcweir     IAccessibleFactory& AccessibilityClient::getFactory()
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         ensureInitialized();
267cdf0e10cSrcweir         OSL_ENSURE( s_pFactory.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
268cdf0e10cSrcweir         return *s_pFactory;
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir //........................................................................
272cdf0e10cSrcweir }   // namespace toolkit
273cdf0e10cSrcweir //........................................................................
274