bump product version to 4.1.6.2
[LibreOffice.git] / toolkit / source / helper / accessibilityclient.cxx
blob43912aacef0a7f8dfd22d634bdd7c27018097244
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
29 // goes away.
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 //........................................................................
42 namespace toolkit
44 //........................................................................
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::accessibility;
49 namespace
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;
56 #endif
57 static GetStandardAccComponentFactory s_pAccessibleFactoryFunc = NULL;
58 static ::rtl::Reference< IAccessibleFactory > s_pFactory;
61 //====================================================================
62 //= AccessibleDummyFactory
63 //====================================================================
64 class AccessibleDummyFactory : public IAccessibleFactory
66 public:
67 AccessibleDummyFactory();
69 protected:
70 virtual ~AccessibleDummyFactory();
72 private:
73 AccessibleDummyFactory( const AccessibleDummyFactory& ); // never implemented
74 AccessibleDummyFactory& operator=( const AccessibleDummyFactory& ); // never implemented
76 oslInterlockedCount m_refCount;
78 public:
79 // IReference
80 virtual oslInterlockedCount SAL_CALL acquire();
81 virtual oslInterlockedCount SAL_CALL release();
83 // IAccessibleFactory
84 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
85 createAccessibleContext( VCLXButton* /*_pXWindow*/ )
87 return NULL;
89 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
90 createAccessibleContext( VCLXCheckBox* /*_pXWindow*/ )
92 return NULL;
94 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
95 createAccessibleContext( VCLXRadioButton* /*_pXWindow*/ )
97 return NULL;
99 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
100 createAccessibleContext( VCLXListBox* /*_pXWindow*/ )
102 return NULL;
104 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
105 createAccessibleContext( VCLXFixedHyperlink* /*_pXWindow*/ )
107 return NULL;
109 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
110 createAccessibleContext( VCLXFixedText* /*_pXWindow*/ )
112 return NULL;
114 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
115 createAccessibleContext( VCLXScrollBar* /*_pXWindow*/ )
117 return NULL;
119 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
120 createAccessibleContext( VCLXEdit* /*_pXWindow*/ )
122 return NULL;
124 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
125 createAccessibleContext( VCLXComboBox* /*_pXWindow*/ )
127 return NULL;
129 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
130 createAccessibleContext( VCLXToolBox* /*_pXWindow*/ )
132 return NULL;
134 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessibleContext >
135 createAccessibleContext( VCLXWindow* /*_pXWindow*/ )
137 return NULL;
139 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible >
140 createAccessible( Menu* /*_pMenu*/, sal_Bool /*_bIsMenuBar*/ )
142 return NULL;
146 //--------------------------------------------------------------------
147 AccessibleDummyFactory::AccessibleDummyFactory()
148 : m_refCount(0)
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 ) )
168 delete this;
169 return 0;
171 return 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() {} }
186 #else
187 extern "C" void *getStandardAccessibleFactory();
188 #endif
190 void AccessibilityClient::ensureInitialized()
192 if ( m_bInitialized )
193 return;
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!" );
216 #else
217 s_pAccessibleFactoryFunc = getStandardAccessibleFactory;
218 #endif
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!" );
225 if ( pFactory )
227 s_pFactory = pFactory;
228 pFactory->release();
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
239 #endif
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 ) )
254 s_pFactory = NULL;
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()
269 ensureInitialized();
270 OSL_ENSURE( s_pFactory.is(), "AccessibilityClient::getFactory: at least a dummy factory should have been created!" );
271 return *s_pFactory;
274 //........................................................................
275 } // namespace toolkit
276 //........................................................................
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */