1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: accessibilityclient.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
33 #include <toolkit/helper/accessibilityclient.hxx>
34 #include <toolkit/helper/accessiblefactory.hxx>
35 #include <osl/module.h>
36 #include <osl/diagnose.h>
37 #include <tools/solar.h>
39 // #define UNLOAD_ON_LAST_CLIENT_DYING
40 // this is not recommended currently. If enabled, the implementation will log
41 // the number of active clients, and unload the acc library when the last client
43 // Sounds like a good idea, unfortunately, there's no guarantee that all objects
44 // implemented in this library are already dead.
45 // Iow, just because an object implementing an XAccessible (implemented in this lib
46 // here) died, it's not said that everybody released all references to the
47 // XAccessibleContext used by this component, and implemented in the acc lib.
48 // So we cannot really unload the lib.
50 // Alternatively, if the lib would us own "usage counting", i.e. every component
51 // implemented therein would affect a static ref count, the acc lib could care
52 // for unloading itself.
54 //........................................................................
57 //........................................................................
59 using namespace ::com::sun::star::uno
;
60 using namespace ::com::sun::star::accessibility
;
64 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
65 static oslInterlockedCount s_nAccessibilityClients
= 0;
66 #endif // UNLOAD_ON_LAST_CLIENT_DYING
67 static oslModule s_hAccessibleImplementationModule
= NULL
;
68 static GetStandardAccComponentFactory s_pAccessibleFactoryFunc
= NULL
;
69 static ::rtl::Reference
< IAccessibleFactory
> s_pFactory
;
72 //====================================================================
73 //= AccessibleDummyFactory
74 //====================================================================
75 class AccessibleDummyFactory
: public IAccessibleFactory
78 AccessibleDummyFactory();
81 virtual ~AccessibleDummyFactory();
84 AccessibleDummyFactory( const AccessibleDummyFactory
& ); // never implemented
85 AccessibleDummyFactory
& operator=( const AccessibleDummyFactory
& ); // never implemented
87 oslInterlockedCount m_refCount
;
91 virtual oslInterlockedCount SAL_CALL
acquire();
92 virtual oslInterlockedCount SAL_CALL
release();
95 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
96 createAccessibleContext( VCLXButton
* /*_pXWindow*/ )
100 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
101 createAccessibleContext( VCLXCheckBox
* /*_pXWindow*/ )
105 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
106 createAccessibleContext( VCLXRadioButton
* /*_pXWindow*/ )
110 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
111 createAccessibleContext( VCLXListBox
* /*_pXWindow*/ )
115 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
116 createAccessibleContext( VCLXFixedHyperlink
* /*_pXWindow*/ )
120 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
121 createAccessibleContext( VCLXFixedText
* /*_pXWindow*/ )
125 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
126 createAccessibleContext( VCLXScrollBar
* /*_pXWindow*/ )
130 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
131 createAccessibleContext( VCLXEdit
* /*_pXWindow*/ )
135 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
136 createAccessibleContext( VCLXComboBox
* /*_pXWindow*/ )
140 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
141 createAccessibleContext( VCLXToolBox
* /*_pXWindow*/ )
145 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
146 createAccessibleContext( VCLXWindow
* /*_pXWindow*/ )
150 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
>
151 createAccessible( Menu
* /*_pMenu*/, sal_Bool
/*_bIsMenuBar*/ )
157 //--------------------------------------------------------------------
158 AccessibleDummyFactory::AccessibleDummyFactory()
162 //--------------------------------------------------------------------
163 AccessibleDummyFactory::~AccessibleDummyFactory()
167 //--------------------------------------------------------------------
168 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::acquire()
170 return osl_incrementInterlockedCount( &m_refCount
);
173 //--------------------------------------------------------------------
174 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::release()
176 if ( 0 == osl_decrementInterlockedCount( &m_refCount
) )
184 //====================================================================
185 //= AccessibilityClient
186 //====================================================================
187 //--------------------------------------------------------------------
188 AccessibilityClient::AccessibilityClient()
189 :m_bInitialized( false )
193 //--------------------------------------------------------------------
194 extern "C" { static void SAL_CALL
thisModule() {} }
196 void AccessibilityClient::ensureInitialized()
198 if ( m_bInitialized
)
201 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
203 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
204 if ( 1 == osl_incrementInterlockedCount( &s_nAccessibilityClients
) )
205 { // the first client
206 #endif // UNLOAD_ON_LAST_CLIENT_DYING
207 // load the library implementing the factory
208 if ( !s_pFactory
.get() )
210 const ::rtl::OUString sModuleName
= ::rtl::OUString::createFromAscii(
213 s_hAccessibleImplementationModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
214 if ( s_hAccessibleImplementationModule
!= NULL
)
216 const ::rtl::OUString sFactoryCreationFunc
=
217 ::rtl::OUString::createFromAscii( "getStandardAccessibleFactory" );
218 s_pAccessibleFactoryFunc
= (GetStandardAccComponentFactory
)
219 osl_getFunctionSymbol( s_hAccessibleImplementationModule
, sFactoryCreationFunc
.pData
);
222 OSL_ENSURE( s_pAccessibleFactoryFunc
, "AccessibilityClient::ensureInitialized: could not load the library, or not retrieve the needed symbol!" );
224 // get a factory instance
225 if ( s_pAccessibleFactoryFunc
)
227 IAccessibleFactory
* pFactory
= static_cast< IAccessibleFactory
* >( (*s_pAccessibleFactoryFunc
)() );
228 OSL_ENSURE( pFactory
, "AccessibilityClient::ensureInitialized: no factory provided by the A11Y lib!" );
231 s_pFactory
= pFactory
;
237 if ( !s_pFactory
.get() )
238 // the attempt to load the lib, or to create the factory, failed
239 // -> fall back to a dummy factory
240 s_pFactory
= new AccessibleDummyFactory
;
241 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
245 m_bInitialized
= true;
248 //--------------------------------------------------------------------
249 AccessibilityClient::~AccessibilityClient()
251 if ( m_bInitialized
)
253 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
255 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
256 if( 0 == osl_decrementInterlockedCount( &s_nAccessibilityClients
) )
259 s_pAccessibleFactoryFunc
= NULL
;
260 if ( s_hAccessibleImplementationModule
)
262 osl_unloadModule( s_hAccessibleImplementationModule
);
263 s_hAccessibleImplementationModule
= NULL
;
266 #endif // UNLOAD_ON_LAST_CLIENT_DYING
270 //--------------------------------------------------------------------
271 IAccessibleFactory
& AccessibilityClient::getFactory()
274 OSL_ENSURE( s_pFactory
.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
278 //........................................................................
279 } // namespace toolkit
280 //........................................................................