xref: /aoo41x/main/vcl/unx/gtk/app/gtkinst.cxx (revision 24c56ab9)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <osl/module.h>
28 #include <unx/gtk/gtkdata.hxx>
29 #include <unx/gtk/gtkinst.hxx>
30 #include <unx/salobj.h>
31 #include <unx/gtk/gtkframe.hxx>
32 #include <unx/gtk/gtkobject.hxx>
33 #include <unx/gtk/atkbridge.hxx>
34 
35 #include <rtl/strbuf.hxx>
36 
37 #include <rtl/uri.hxx>
38 
39 #if OSL_DEBUG_LEVEL > 1
40 #include <stdio.h>
41 #endif
42 
43 #include <dlfcn.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 
GtkHookedYieldMutex()47 GtkHookedYieldMutex::GtkHookedYieldMutex()
48 {
49 }
50 
51 /*
52  * These methods always occur in pairs
53  * A ThreadsEnter is followed by a ThreadsLeave
54  * We need to queue up the recursive lock count
55  * for each pair, so we can accurately restore
56  * it later.
57  */
ThreadsEnter()58 void GtkHookedYieldMutex::ThreadsEnter()
59 {
60 	acquire();
61 	if( !aYieldStack.empty() )
62 	{ /* Previously called ThreadsLeave() */
63 		sal_uLong nCount = aYieldStack.front();
64 		aYieldStack.pop_front();
65 		while( nCount-- > 1 )
66 			acquire();
67 	}
68 }
69 
ThreadsLeave()70 void GtkHookedYieldMutex::ThreadsLeave()
71 {
72 	aYieldStack.push_front( mnCount );
73 
74 #if OSL_DEBUG_LEVEL > 1
75 	if( mnThreadId &&
76 		mnThreadId != vos::OThread::getCurrentIdentifier())
77 		fprintf( stderr, "\n\n--- A different thread owns the mutex ...---\n\n\n");
78 #endif
79 
80 	while( mnCount > 1 )
81 		release();
82 	release();
83 }
84 
acquire()85 void GtkHookedYieldMutex::acquire()
86 {
87 	SalYieldMutex::acquire();
88 }
89 
release()90 void GtkHookedYieldMutex::release()
91 {
92 	SalYieldMutex::release();
93 }
94 
95 extern "C"
96 {
97 	#define GET_YIELD_MUTEX() static_cast<GtkHookedYieldMutex*>(GetSalData()->m_pInstance->GetYieldMutex())
GdkThreadsEnter(void)98 	static void GdkThreadsEnter( void )
99 	{
100 		GtkHookedYieldMutex *pYieldMutex = GET_YIELD_MUTEX();
101 		pYieldMutex->ThreadsEnter();
102 	}
GdkThreadsLeave(void)103 	static void GdkThreadsLeave( void )
104 	{
105 		GtkHookedYieldMutex *pYieldMutex = GET_YIELD_MUTEX();
106 		pYieldMutex->ThreadsLeave();
107 	}
hookLocks(oslModule pModule)108 	static bool hookLocks( oslModule pModule )
109 	{
110 		typedef void (*GdkLockFn) (GCallback enter_fn, GCallback leave_fn);
111 
112 		GdkLockFn gdk_threads_set_lock_functions =
113 				(GdkLockFn) osl_getAsciiFunctionSymbol( pModule, "gdk_threads_set_lock_functions" );
114 		if ( !gdk_threads_set_lock_functions )
115 		{
116 #if OSL_DEBUG_LEVEL > 1
117 		    fprintf( stderr, "Failed to hook gdk threads locks\n" );
118 #endif
119 			return false;
120 		}
121 
122 		gdk_threads_set_lock_functions (GdkThreadsEnter, GdkThreadsLeave);
123 #if OSL_DEBUG_LEVEL > 1
124 		fprintf( stderr, "Hooked gdk threads locks\n" );
125 #endif
126 		return true;
127 	}
128 
create_SalInstance(oslModule pModule)129     VCLPLUG_GTK_PUBLIC SalInstance* create_SalInstance( oslModule pModule )
130     {
131         /* #i92121# workaround deadlocks in the X11 implementation
132         */
133         static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" );
134         /* #i90094#
135            from now on we know that an X connection will be
136            established, so protect X against itself
137         */
138         if( ! ( pNoXInitThreads && *pNoXInitThreads ) )
139             XInitThreads();
140 
141         const gchar* pVersion = gtk_check_version( 2, 2, 0 );
142         if( pVersion )
143         {
144 #if OSL_DEBUG_LEVEL > 1
145             fprintf( stderr, "gtk version conflict: %s\n", pVersion );
146 #endif
147             return NULL;
148         }
149 
150 		GtkYieldMutex *pYieldMutex;
151 
152         // init gdk thread protection
153 		if ( !g_thread_supported() )
154 			g_thread_init( NULL );
155 
156 		if ( hookLocks( pModule ) )
157 			pYieldMutex = new GtkHookedYieldMutex();
158 		else
159 			pYieldMutex = new GtkYieldMutex();
160 
161 		gdk_threads_init();
162 
163         GtkInstance* pInstance = new GtkInstance( pYieldMutex );
164 #if OSL_DEBUG_LEVEL > 1
165         fprintf( stderr, "creating GtkSalInstance 0x%p\n", pInstance );
166 #endif
167 
168         // initialize SalData
169         GtkData *pSalData = new GtkData();
170         SetSalData( pSalData );
171         pSalData->m_pInstance = pInstance;
172         pSalData->Init();
173         pSalData->initNWF();
174 
175         InitAtkBridge();
176 
177         return pInstance;
178     }
179 }
180 
~GtkInstance()181 GtkInstance::~GtkInstance()
182 {
183     DeInitAtkBridge();
184 }
185 
CreateFrame(SalFrame * pParent,sal_uLong nStyle)186 SalFrame* GtkInstance::CreateFrame( SalFrame* pParent, sal_uLong nStyle )
187 {
188     return new GtkSalFrame( pParent, nStyle );
189 }
190 
CreateChildFrame(SystemParentData * pParentData,sal_uLong)191 SalFrame* GtkInstance::CreateChildFrame( SystemParentData* pParentData, sal_uLong )
192 {
193 	SalFrame* pFrame = new GtkSalFrame( pParentData );
194 
195 	return pFrame;
196 }
197 
CreateObject(SalFrame * pParent,SystemWindowData * pWindowData,sal_Bool bShow)198 SalObject* GtkInstance::CreateObject( SalFrame* pParent, SystemWindowData* pWindowData, sal_Bool bShow )
199 {
200     // there is no method to set a visual for a GtkWidget
201     // so we need the X11SalObject in that case
202     if( pWindowData )
203         return X11SalObject::CreateObject( pParent, pWindowData, bShow );
204 
205     return new GtkSalObject( static_cast<GtkSalFrame*>(pParent), bShow );
206 }
207 
208 extern "C"
209 {
210     typedef void*(* getDefaultFnc)();
211     typedef void(* addItemFnc)(void *, const char *);
212 }
213 
AddToRecentDocumentList(const rtl::OUString & rFileUrl,const rtl::OUString & rMimeType)214 void GtkInstance::AddToRecentDocumentList(const rtl::OUString& rFileUrl, const rtl::OUString& rMimeType)
215 {
216     rtl::OString sGtkURL;
217     rtl_TextEncoding aSystemEnc = osl_getThreadTextEncoding();
218     if ((aSystemEnc == RTL_TEXTENCODING_UTF8) || (rFileUrl.compareToAscii( "file://", 7 ) !=  0))
219         sGtkURL = rtl::OUStringToOString(rFileUrl, RTL_TEXTENCODING_UTF8);
220     else
221     {
222         //Non-utf8 locales are a bad idea if trying to work with non-ascii filenames
223         //Decode %XX components
224         rtl::OUString sDecodedUri = Uri::decode(rFileUrl.copy(7), rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
225         //Convert back to system locale encoding
226         rtl::OString sSystemUrl = rtl::OUStringToOString(sDecodedUri, aSystemEnc);
227         //Encode to an escaped ASCII-encoded URI
228         gchar *g_uri = g_filename_to_uri(sSystemUrl.getStr(), NULL, NULL);
229         sGtkURL = rtl::OString(g_uri);
230         g_free(g_uri);
231     }
232 #if GTK_CHECK_VERSION(2,10,0)
233     GtkRecentManager *manager = gtk_recent_manager_get_default ();
234     gtk_recent_manager_add_item( manager, sGtkURL.getStr());
235     (void)rMimeType;
236 #else
237     static getDefaultFnc sym_gtk_recent_manager_get_default =
238         (getDefaultFnc)osl_getAsciiFunctionSymbol( GetSalData()->m_pPlugin, "gtk_recent_manager_get_default" );
239 
240     static addItemFnc sym_gtk_recent_manager_add_item =
241         (addItemFnc)osl_getAsciiFunctionSymbol( GetSalData()->m_pPlugin, "gtk_recent_manager_add_item");
242     if (sym_gtk_recent_manager_get_default && sym_gtk_recent_manager_add_item)
243         sym_gtk_recent_manager_add_item(sym_gtk_recent_manager_get_default(), sGtkURL);
244     else
245         X11SalInstance::AddToRecentDocumentList(rFileUrl, rMimeType);
246 #endif
247 }
248 
GtkYieldMutex()249 GtkYieldMutex::GtkYieldMutex()
250 {
251 }
252 
acquire()253 void GtkYieldMutex::acquire()
254 {
255     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
256     // protect member manipulation
257     OMutex::acquire();
258     if( mnCount > 0 && mnThreadId == aCurrentThread )
259     {
260         mnCount++;
261         OMutex::release();
262         return;
263     }
264     OMutex::release();
265 
266     // obtain gdk mutex
267     gdk_threads_enter();
268 
269     // obtained gdk mutex, now lock count is one by definition
270     OMutex::acquire();
271     mnCount = 1;
272     mnThreadId = aCurrentThread;
273     OMutex::release();
274 }
275 
release()276 void GtkYieldMutex::release()
277 {
278     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
279     // protect member manipulation
280     OMutex::acquire();
281     // strange things happen, do nothing if we don't own the mutex
282     if( mnThreadId == aCurrentThread )
283     {
284         mnCount--;
285         if( mnCount == 0 )
286         {
287             gdk_threads_leave();
288             mnThreadId = 0;
289         }
290     }
291     OMutex::release();
292 }
293 
tryToAcquire()294 sal_Bool GtkYieldMutex::tryToAcquire()
295 {
296     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
297     // protect member manipulation
298     OMutex::acquire();
299     if( mnCount > 0 )
300     {
301         if( mnThreadId == aCurrentThread )
302         {
303             mnCount++;
304             OMutex::release();
305             return sal_True;
306         }
307         else
308         {
309             OMutex::release();
310             return sal_False;
311         }
312     }
313     OMutex::release();
314 
315     // HACK: gdk_threads_mutex is private, we shouldn't use it.
316     // how to we do a try_lock without having a gdk_threads_try_enter ?
317     if( ! g_mutex_trylock( gdk_threads_mutex ) )
318         return sal_False;
319 
320     // obtained gdk mutex, now lock count is one by definition
321     OMutex::acquire();
322     mnCount = 1;
323     mnThreadId = aCurrentThread;
324     OMutex::release();
325 
326     return sal_True;
327 }
328 
Grab()329 int GtkYieldMutex::Grab()
330 {
331     // this MUST only be called by gdk/gtk callbacks:
332     // they are entered with gdk mutex locked; the mutex
333     // was unlocked by GtkYieldMutex befor yielding which
334     // is now locked again by gtk implicitly
335 
336     // obtained gdk mutex, now lock count is one by definition
337     OMutex::acquire();
338     int nRet = mnCount;
339     if( mnCount == 0 ) // recursive else
340         mnThreadId = vos::OThread::getCurrentIdentifier();
341 #if OSL_DEBUG_LEVEL > 1
342     else if( mnThreadId != vos::OThread::getCurrentIdentifier() )
343     {
344         fprintf( stderr, "Yield mutex grabbed in different thread !\n" );
345         abort();
346     }
347 #endif
348     mnCount = 1;
349     OMutex::release();
350     return nRet;
351 }
352 
Ungrab(int nGrabs)353 void GtkYieldMutex::Ungrab( int nGrabs )
354 {
355     // this MUST only be called when leaving the callback
356     // that locked the mutex with Grab()
357     OMutex::acquire();
358     mnCount = nGrabs;
359     if( mnCount == 0 )
360         mnThreadId = 0;
361     OMutex::release();
362 }
363