1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <toolkit/helper/accessibilityclient.hxx>
21 #include <toolkit/helper/accessiblefactory.hxx>
22 #include <osl/module.h>
23 #include <osl/diagnose.h>
24 #include <tools/solar.h>
26 // #define UNLOAD_ON_LAST_CLIENT_DYING
27 // this is not recommended currently. If enabled, the implementation will log
28 // the number of active clients, and unload the acc library when the last client
30 // Sounds like a good idea, unfortunately, there's no guarantee that all objects
31 // implemented in this library are already dead.
32 // Iow, just because an object implementing an XAccessible (implemented in this lib
33 // here) died, it's not said that everybody released all references to the
34 // XAccessibleContext used by this component, and implemented in the acc lib.
35 // So we cannot really unload the lib.
37 // Alternatively, if the lib would us own "usage counting", i.e. every component
38 // implemented therein would affect a static ref count, the acc lib could care
39 // for unloading itself.
41 //........................................................................
44 //........................................................................
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::accessibility
;
51 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
52 static oslInterlockedCount s_nAccessibilityClients
= 0;
53 #endif // UNLOAD_ON_LAST_CLIENT_DYING
54 #ifndef DISABLE_DYNLOADING
55 static oslModule s_hAccessibleImplementationModule
= NULL
;
57 static GetStandardAccComponentFactory s_pAccessibleFactoryFunc
= NULL
;
58 static ::rtl::Reference
< IAccessibleFactory
> s_pFactory
;
61 //====================================================================
62 //= AccessibleDummyFactory
63 //====================================================================
64 class AccessibleDummyFactory
: public IAccessibleFactory
67 AccessibleDummyFactory();
70 virtual ~AccessibleDummyFactory();
73 AccessibleDummyFactory( const AccessibleDummyFactory
& ); // never implemented
74 AccessibleDummyFactory
& operator=( const AccessibleDummyFactory
& ); // never implemented
76 oslInterlockedCount m_refCount
;
80 virtual oslInterlockedCount SAL_CALL
acquire();
81 virtual oslInterlockedCount SAL_CALL
release();
84 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
85 createAccessibleContext( VCLXButton
* /*_pXWindow*/ )
89 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
90 createAccessibleContext( VCLXCheckBox
* /*_pXWindow*/ )
94 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
95 createAccessibleContext( VCLXRadioButton
* /*_pXWindow*/ )
99 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
100 createAccessibleContext( VCLXListBox
* /*_pXWindow*/ )
104 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
105 createAccessibleContext( VCLXFixedHyperlink
* /*_pXWindow*/ )
109 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
110 createAccessibleContext( VCLXFixedText
* /*_pXWindow*/ )
114 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
115 createAccessibleContext( VCLXScrollBar
* /*_pXWindow*/ )
119 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
120 createAccessibleContext( VCLXEdit
* /*_pXWindow*/ )
124 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
125 createAccessibleContext( VCLXComboBox
* /*_pXWindow*/ )
129 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
130 createAccessibleContext( VCLXToolBox
* /*_pXWindow*/ )
134 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessibleContext
>
135 createAccessibleContext( VCLXWindow
* /*_pXWindow*/ )
139 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
>
140 createAccessible( Menu
* /*_pMenu*/, sal_Bool
/*_bIsMenuBar*/ )
146 //--------------------------------------------------------------------
147 AccessibleDummyFactory::AccessibleDummyFactory()
152 //--------------------------------------------------------------------
153 AccessibleDummyFactory::~AccessibleDummyFactory()
157 //--------------------------------------------------------------------
158 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::acquire()
160 return osl_atomic_increment( &m_refCount
);
163 //--------------------------------------------------------------------
164 oslInterlockedCount SAL_CALL
AccessibleDummyFactory::release()
166 if ( 0 == osl_atomic_decrement( &m_refCount
) )
174 //====================================================================
175 //= AccessibilityClient
176 //====================================================================
177 //--------------------------------------------------------------------
178 AccessibilityClient::AccessibilityClient()
179 :m_bInitialized( false )
183 //--------------------------------------------------------------------
184 #ifndef DISABLE_DYNLOADING
185 extern "C" { static void SAL_CALL
thisModule() {} }
187 extern "C" void *getStandardAccessibleFactory();
190 void AccessibilityClient::ensureInitialized()
192 if ( m_bInitialized
)
195 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
197 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
198 if ( 1 == osl_atomic_increment( &s_nAccessibilityClients
) )
199 { // the first client
200 #endif // UNLOAD_ON_LAST_CLIENT_DYING
201 // load the library implementing the factory
202 if ( !s_pFactory
.get() )
204 #ifndef DISABLE_DYNLOADING
205 const OUString
sModuleName( SVLIBRARY( "acc" ) );
206 s_hAccessibleImplementationModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
207 if ( s_hAccessibleImplementationModule
!= NULL
)
209 const OUString sFactoryCreationFunc
=
210 OUString("getStandardAccessibleFactory");
211 s_pAccessibleFactoryFunc
= (GetStandardAccComponentFactory
)
212 osl_getFunctionSymbol( s_hAccessibleImplementationModule
, sFactoryCreationFunc
.pData
);
215 OSL_ENSURE( s_pAccessibleFactoryFunc
, "AccessibilityClient::ensureInitialized: could not load the library, or not retrieve the needed symbol!" );
217 s_pAccessibleFactoryFunc
= getStandardAccessibleFactory
;
220 // get a factory instance
221 if ( s_pAccessibleFactoryFunc
)
223 IAccessibleFactory
* pFactory
= static_cast< IAccessibleFactory
* >( (*s_pAccessibleFactoryFunc
)() );
224 OSL_ENSURE( pFactory
, "AccessibilityClient::ensureInitialized: no factory provided by the A11Y lib!" );
227 s_pFactory
= pFactory
;
233 if ( !s_pFactory
.get() )
234 // the attempt to load the lib, or to create the factory, failed
235 // -> fall back to a dummy factory
236 s_pFactory
= new AccessibleDummyFactory
;
237 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
241 m_bInitialized
= true;
244 //--------------------------------------------------------------------
245 AccessibilityClient::~AccessibilityClient()
247 if ( m_bInitialized
)
249 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
251 #ifdef UNLOAD_ON_LAST_CLIENT_DYING
252 if( 0 == osl_atomic_decrement( &s_nAccessibilityClients
) )
255 s_pAccessibleFactoryFunc
= NULL
;
256 if ( s_hAccessibleImplementationModule
)
258 osl_unloadModule( s_hAccessibleImplementationModule
);
259 s_hAccessibleImplementationModule
= NULL
;
262 #endif // UNLOAD_ON_LAST_CLIENT_DYING
266 //--------------------------------------------------------------------
267 IAccessibleFactory
& AccessibilityClient::getFactory()
270 OSL_ENSURE( s_pFactory
.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
274 //........................................................................
275 } // namespace toolkit
276 //........................................................................
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */