xref: /AOO41X/main/accessibility/bridge/source/java/WindowsAccessBridgeAdapter.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_accessibility.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //------------------------------------------------------------------------
32*cdf0e10cSrcweir // includes
33*cdf0e10cSrcweir //------------------------------------------------------------------------
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <WindowsAccessBridgeAdapter.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <tools/prewin.h>
38*cdf0e10cSrcweir #include <wtypes.h>
39*cdf0e10cSrcweir #include <tools/postwin.h>
40*cdf0e10cSrcweir #include <rtl/process.h>
41*cdf0e10cSrcweir #include <tools/link.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #ifndef _SVAPP_HXX
44*cdf0e10cSrcweir #include <vcl/svapp.hxx>
45*cdf0e10cSrcweir #endif
46*cdf0e10cSrcweir #include <vcl/window.hxx>
47*cdf0e10cSrcweir #include <vcl/sysdata.hxx>
48*cdf0e10cSrcweir #include <uno/current_context.hxx>
49*cdf0e10cSrcweir #include <uno/environment.h>
50*cdf0e10cSrcweir #include <uno/mapping.hxx>
51*cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleRole.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/accessibility/XAccessible.hpp>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #ifndef _JVMACCESS_UNOVIRTUALMACHINE_HXX_
55*cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx"
56*cdf0e10cSrcweir #endif
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #ifndef _JVMACCESS_VIRTUALMACHINE_HXX_
59*cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx"
60*cdf0e10cSrcweir #endif
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir #include <osl/diagnose.h>
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir using ::rtl::OUString;
65*cdf0e10cSrcweir using ::com::sun::star::uno::Mapping;
66*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
67*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException;
68*cdf0e10cSrcweir using namespace ::com::sun::star::accessibility;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir long VCLEventListenerLinkFunc(void * pInst, void * pData);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir //------------------------------------------------------------------------
73*cdf0e10cSrcweir // global vatiables
74*cdf0e10cSrcweir //------------------------------------------------------------------------
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir Link g_aEventListenerLink(NULL, VCLEventListenerLinkFunc);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir rtl::Reference< jvmaccess::UnoVirtualMachine > g_xUnoVirtualMachine;
79*cdf0e10cSrcweir typelib_InterfaceTypeDescription * g_pTypeDescription = NULL;
80*cdf0e10cSrcweir Mapping g_unoMapping;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir jclass g_jcWindowsAccessBridgeAdapter = NULL;
83*cdf0e10cSrcweir jmethodID g_jmRegisterTopWindow = 0;
84*cdf0e10cSrcweir jmethodID g_jmRevokeTopWindow = 0;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir //------------------------------------------------------------------------
87*cdf0e10cSrcweir // functions
88*cdf0e10cSrcweir //------------------------------------------------------------------------
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *, void *)
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     return JNI_VERSION_1_2;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir JNIEXPORT jbyteArray JNICALL
96*cdf0e10cSrcweir Java_org_openoffice_accessibility_WindowsAccessBridgeAdapter_getProcessID(JNIEnv *pJNIEnv, jclass clazz)
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir     // Initialize global class and method references
99*cdf0e10cSrcweir     g_jcWindowsAccessBridgeAdapter =
100*cdf0e10cSrcweir         static_cast< jclass > (pJNIEnv->NewGlobalRef(clazz));
101*cdf0e10cSrcweir     if (NULL == g_jcWindowsAccessBridgeAdapter) {
102*cdf0e10cSrcweir         return 0; /* jni error occured */
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir     g_jmRegisterTopWindow =
105*cdf0e10cSrcweir         pJNIEnv->GetStaticMethodID(clazz, "registerTopWindow", "(ILcom/sun/star/accessibility/XAccessible;)V");
106*cdf0e10cSrcweir     if (0 == g_jmRegisterTopWindow) {
107*cdf0e10cSrcweir         return 0; /* jni error occured */
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir     g_jmRevokeTopWindow =
110*cdf0e10cSrcweir         pJNIEnv->GetStaticMethodID(clazz, "revokeTopWindow", "(ILcom/sun/star/accessibility/XAccessible;)V");
111*cdf0e10cSrcweir     if (0 == g_jmRevokeTopWindow) {
112*cdf0e10cSrcweir         return 0; /* jni error occured */
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     // Use the special protocol of XJavaVM.getJavaVM:  If the passed in
116*cdf0e10cSrcweir     // process ID has an extra 17th byte of value one, the returned any
117*cdf0e10cSrcweir     // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of
118*cdf0e10cSrcweir     // the underlying JavaVM pointer:
119*cdf0e10cSrcweir     jbyte processID[17];
120*cdf0e10cSrcweir     rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *> (processID));
121*cdf0e10cSrcweir     // #i51265# we need a jvmaccess::UnoVirtualMachine pointer for the
122*cdf0e10cSrcweir     // uno_getEnvironment() call later.
123*cdf0e10cSrcweir     processID[16] = 1;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     // Copy the result into a java byte[] and return.
126*cdf0e10cSrcweir     jbyteArray jbaProcessID = pJNIEnv->NewByteArray(17);
127*cdf0e10cSrcweir     pJNIEnv->SetByteArrayRegion(jbaProcessID, 0, 17, processID);
128*cdf0e10cSrcweir     return jbaProcessID;
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir JNIEXPORT jboolean JNICALL
132*cdf0e10cSrcweir Java_org_openoffice_accessibility_WindowsAccessBridgeAdapter_createMapping(JNIEnv *, jclass, jlong pointer)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	uno_Environment * pJava_environment = NULL;
135*cdf0e10cSrcweir 	uno_Environment * pUno_environment = NULL;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     try {
138*cdf0e10cSrcweir         // We get a non-refcounted pointer to a jvmaccess::VirtualMachine
139*cdf0e10cSrcweir         // from the XJavaVM service (the pointer is guaranteed to be valid
140*cdf0e10cSrcweir         // as long as our reference to the XJavaVM service lasts), and
141*cdf0e10cSrcweir         // convert the non-refcounted pointer into a refcounted one
142*cdf0e10cSrcweir         // immediately:
143*cdf0e10cSrcweir         g_xUnoVirtualMachine = reinterpret_cast< jvmaccess::UnoVirtualMachine * >(pointer);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         if ( g_xUnoVirtualMachine.is() )
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java"));
148*cdf0e10cSrcweir             uno_getEnvironment(&pJava_environment, sJava.pData, g_xUnoVirtualMachine.get());
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir             OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
151*cdf0e10cSrcweir             uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir             if ( pJava_environment && pUno_environment )
154*cdf0e10cSrcweir             {
155*cdf0e10cSrcweir                 g_unoMapping = Mapping(pUno_environment, pJava_environment);
156*cdf0e10cSrcweir                 getCppuType((::com::sun::star::uno::Reference< XAccessible > *) 0).getDescription((typelib_TypeDescription **) & g_pTypeDescription);
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir             if ( pJava_environment )
160*cdf0e10cSrcweir             {
161*cdf0e10cSrcweir                 // release java environment
162*cdf0e10cSrcweir                 pJava_environment->release(pJava_environment);
163*cdf0e10cSrcweir                 pJava_environment = NULL;
164*cdf0e10cSrcweir             }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir             if ( pUno_environment )
167*cdf0e10cSrcweir             {
168*cdf0e10cSrcweir                 // release uno environment
169*cdf0e10cSrcweir                 pUno_environment->release(pUno_environment);
170*cdf0e10cSrcweir                 pUno_environment = NULL;
171*cdf0e10cSrcweir             }
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     catch ( RuntimeException e)
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         OSL_TRACE("RuntimeException caught while initializing the mapping");
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     if ( (0 != g_jmRegisterTopWindow) && (0 != g_jmRevokeTopWindow) )
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         ::Application::AddEventListener(g_aEventListenerLink);
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir     return JNI_TRUE;
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *)
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir     ::Application::RemoveEventListener(g_aEventListenerLink);
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     if ( NULL != g_jcWindowsAccessBridgeAdapter )
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         JNIEnv * pJNIEnv;
194*cdf0e10cSrcweir         if ( ! jvm->GetEnv((void **) &pJNIEnv, JNI_VERSION_1_2) )
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             pJNIEnv->DeleteGlobalRef(g_jcWindowsAccessBridgeAdapter);
197*cdf0e10cSrcweir             g_jcWindowsAccessBridgeAdapter = NULL;
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     if ( NULL != g_pTypeDescription )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         typelib_typedescription_release( reinterpret_cast< typelib_TypeDescription * > (g_pTypeDescription) );
204*cdf0e10cSrcweir         g_pTypeDescription = NULL;
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     g_unoMapping.clear();
208*cdf0e10cSrcweir     g_xUnoVirtualMachine.clear();
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir HWND GetHWND(Window * pWindow)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     const SystemEnvData * pEnvData = pWindow->GetSystemData();
214*cdf0e10cSrcweir     if (pEnvData != NULL)
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir         return pEnvData->hWnd;
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir     return (HWND) -1;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir void handleWindowEvent(Window * pWindow, bool bShow)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     if ( pWindow && pWindow->IsTopWindow() )
224*cdf0e10cSrcweir     {
225*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< XAccessible > xAccessible;
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         // Test for combo box - drop down floating windows first
228*cdf0e10cSrcweir         Window * pParentWindow = pWindow->GetParent();
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         if ( pParentWindow )
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             try
233*cdf0e10cSrcweir             {
234*cdf0e10cSrcweir                 // The parent window of a combo box floating window should have the role COMBO_BOX
235*cdf0e10cSrcweir                 ::com::sun::star::uno::Reference< XAccessible > xParentAccessible(pParentWindow->GetAccessible());
236*cdf0e10cSrcweir                 if ( xParentAccessible.is() )
237*cdf0e10cSrcweir                 {
238*cdf0e10cSrcweir                     ::com::sun::star::uno::Reference< XAccessibleContext > xParentAC(xParentAccessible->getAccessibleContext());
239*cdf0e10cSrcweir                     if ( xParentAC.is() && (AccessibleRole::COMBO_BOX == xParentAC->getAccessibleRole()) )
240*cdf0e10cSrcweir                     {
241*cdf0e10cSrcweir                         // O.k. - this is a combo box floating window corresponding to the child of role LIST of the parent.
242*cdf0e10cSrcweir                         // Let's not rely on a specific child order, just search for the child with the role LIST
243*cdf0e10cSrcweir                         sal_Int32 nCount = xParentAC->getAccessibleChildCount();
244*cdf0e10cSrcweir                         for ( sal_Int32 n = 0; (n < nCount) && !xAccessible.is(); n++)
245*cdf0e10cSrcweir                         {
246*cdf0e10cSrcweir                             ::com::sun::star::uno::Reference< XAccessible > xChild = xParentAC->getAccessibleChild(n);
247*cdf0e10cSrcweir                             if ( xChild.is() )
248*cdf0e10cSrcweir                             {
249*cdf0e10cSrcweir                                 ::com::sun::star::uno::Reference< XAccessibleContext > xChildAC = xChild->getAccessibleContext();
250*cdf0e10cSrcweir                                 if ( xChildAC.is() && (AccessibleRole::LIST == xChildAC->getAccessibleRole()) )
251*cdf0e10cSrcweir                                 {
252*cdf0e10cSrcweir                                     xAccessible = xChild;
253*cdf0e10cSrcweir                                 }
254*cdf0e10cSrcweir                             }
255*cdf0e10cSrcweir                         }
256*cdf0e10cSrcweir                     }
257*cdf0e10cSrcweir                 }
258*cdf0e10cSrcweir             }
259*cdf0e10cSrcweir             catch (::com::sun::star::uno::RuntimeException e)
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 // Ignore show events that throw DisposedExceptions in getAccessibleContext(),
262*cdf0e10cSrcweir                 // but keep revoking these windows in hide(s).
263*cdf0e10cSrcweir                 if (bShow)
264*cdf0e10cSrcweir                     return;
265*cdf0e10cSrcweir             }
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir         // We have to rely on the fact that Window::GetAccessible()->getAccessibleContext() returns a valid XAccessibleContext
269*cdf0e10cSrcweir         // also for other menus than menubar or toplevel popup window. Otherwise we had to traverse the hierarchy to find the
270*cdf0e10cSrcweir         // context object to this menu floater. This makes the call to Window->IsMenuFloatingWindow() obsolete.
271*cdf0e10cSrcweir         if ( ! xAccessible.is() )
272*cdf0e10cSrcweir             xAccessible = pWindow->GetAccessible();
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         if ( xAccessible.is() && g_unoMapping.is() )
275*cdf0e10cSrcweir         {
276*cdf0e10cSrcweir             jobject * joXAccessible = reinterpret_cast < jobject * > (g_unoMapping.mapInterface(
277*cdf0e10cSrcweir                 xAccessible.get(), g_pTypeDescription));
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir             if ( NULL != joXAccessible )
280*cdf0e10cSrcweir             {
281*cdf0e10cSrcweir                 jvmaccess::VirtualMachine::AttachGuard aGuard(g_xUnoVirtualMachine->getVirtualMachine());
282*cdf0e10cSrcweir                 JNIEnv * pJNIEnv = aGuard.getEnvironment();
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir                 if ( NULL != pJNIEnv )
285*cdf0e10cSrcweir                 {
286*cdf0e10cSrcweir                     // g_jmRegisterTopWindow and g_jmRevokeTopWindow are ensured to be != 0 - otherwise
287*cdf0e10cSrcweir                     // the event listener would not have been attached.
288*cdf0e10cSrcweir                     pJNIEnv->CallStaticVoidMethod(g_jcWindowsAccessBridgeAdapter,
289*cdf0e10cSrcweir                         (bShow) ? g_jmRegisterTopWindow : g_jmRevokeTopWindow,
290*cdf0e10cSrcweir                         (jint) GetHWND(pWindow), joXAccessible );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir                     // Clear any exception that might have been occured.
293*cdf0e10cSrcweir                     if (pJNIEnv->ExceptionCheck()) {
294*cdf0e10cSrcweir                         pJNIEnv->ExceptionClear();
295*cdf0e10cSrcweir                     }
296*cdf0e10cSrcweir                 }
297*cdf0e10cSrcweir             }
298*cdf0e10cSrcweir         }
299*cdf0e10cSrcweir     }
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir long VCLEventListenerLinkFunc(void *, void * pData)
303*cdf0e10cSrcweir {
304*cdf0e10cSrcweir     ::VclSimpleEvent const * pEvent = (::VclSimpleEvent const *) pData;
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     switch (pEvent->GetId())
307*cdf0e10cSrcweir     {
308*cdf0e10cSrcweir     case VCLEVENT_WINDOW_SHOW:
309*cdf0e10cSrcweir         handleWindowEvent(((::VclWindowEvent const *) pEvent)->GetWindow(), true);
310*cdf0e10cSrcweir 	    break;
311*cdf0e10cSrcweir     case VCLEVENT_WINDOW_HIDE:
312*cdf0e10cSrcweir         handleWindowEvent(((::VclWindowEvent const *) pEvent)->GetWindow(), false);
313*cdf0e10cSrcweir         break;
314*cdf0e10cSrcweir     }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir     return 0;
317*cdf0e10cSrcweir }
318