xref: /trunk/main/vcl/unx/gtk/app/gtkinst.cxx (revision df8e50b18960fb1a64b172e95b64d545305bb068)
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 
AddToRecentDocumentList(const rtl::OUString & rFileUrl,const rtl::OUString & rMimeType)208 void GtkInstance::AddToRecentDocumentList(const rtl::OUString& rFileUrl, const rtl::OUString& rMimeType)
209 {
210     rtl::OString sGtkURL;
211     rtl_TextEncoding aSystemEnc = osl_getThreadTextEncoding();
212     if ((aSystemEnc == RTL_TEXTENCODING_UTF8) || (rFileUrl.compareToAscii( "file://", 7 ) !=  0))
213         sGtkURL = rtl::OUStringToOString(rFileUrl, RTL_TEXTENCODING_UTF8);
214     else
215     {
216         //Non-utf8 locales are a bad idea if trying to work with non-ascii filenames
217         //Decode %XX components
218         rtl::OUString sDecodedUri = Uri::decode(rFileUrl.copy(7), rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
219         //Convert back to system locale encoding
220         rtl::OString sSystemUrl = rtl::OUStringToOString(sDecodedUri, aSystemEnc);
221         //Encode to an escaped ASCII-encoded URI
222         gchar *g_uri = g_filename_to_uri(sSystemUrl.getStr(), NULL, NULL);
223         sGtkURL = rtl::OString(g_uri);
224         g_free(g_uri);
225     }
226     GtkRecentManager *manager = gtk_recent_manager_get_default ();
227     gtk_recent_manager_add_item( manager, sGtkURL.getStr());
228     (void)rMimeType;
229 }
230 
GtkYieldMutex()231 GtkYieldMutex::GtkYieldMutex()
232 {
233 }
234 
acquire()235 void GtkYieldMutex::acquire()
236 {
237     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
238     // protect member manipulation
239     OMutex::acquire();
240     if( mnCount > 0 && mnThreadId == aCurrentThread )
241     {
242         mnCount++;
243         OMutex::release();
244         return;
245     }
246     OMutex::release();
247 
248     // obtain gdk mutex
249     gdk_threads_enter();
250 
251     // obtained gdk mutex, now lock count is one by definition
252     OMutex::acquire();
253     mnCount = 1;
254     mnThreadId = aCurrentThread;
255     OMutex::release();
256 }
257 
release()258 void GtkYieldMutex::release()
259 {
260     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
261     // protect member manipulation
262     OMutex::acquire();
263     // strange things happen, do nothing if we don't own the mutex
264     if( mnThreadId == aCurrentThread )
265     {
266         mnCount--;
267         if( mnCount == 0 )
268         {
269             gdk_threads_leave();
270             mnThreadId = 0;
271         }
272     }
273     OMutex::release();
274 }
275 
tryToAcquire()276 sal_Bool GtkYieldMutex::tryToAcquire()
277 {
278     vos::OThread::TThreadIdentifier aCurrentThread = vos::OThread::getCurrentIdentifier();
279     // protect member manipulation
280     OMutex::acquire();
281     if( mnCount > 0 )
282     {
283         if( mnThreadId == aCurrentThread )
284         {
285             mnCount++;
286             OMutex::release();
287             return sal_True;
288         }
289         else
290         {
291             OMutex::release();
292             return sal_False;
293         }
294     }
295     OMutex::release();
296 
297     // HACK: gdk_threads_mutex is private, we shouldn't use it.
298     // how to we do a try_lock without having a gdk_threads_try_enter ?
299     if( ! g_mutex_trylock( gdk_threads_mutex ) )
300         return sal_False;
301 
302     // obtained gdk mutex, now lock count is one by definition
303     OMutex::acquire();
304     mnCount = 1;
305     mnThreadId = aCurrentThread;
306     OMutex::release();
307 
308     return sal_True;
309 }
310 
Grab()311 int GtkYieldMutex::Grab()
312 {
313     // this MUST only be called by gdk/gtk callbacks:
314     // they are entered with gdk mutex locked; the mutex
315     // was unlocked by GtkYieldMutex befor yielding which
316     // is now locked again by gtk implicitly
317 
318     // obtained gdk mutex, now lock count is one by definition
319     OMutex::acquire();
320     int nRet = mnCount;
321     if( mnCount == 0 ) // recursive else
322         mnThreadId = vos::OThread::getCurrentIdentifier();
323 #if OSL_DEBUG_LEVEL > 1
324     else if( mnThreadId != vos::OThread::getCurrentIdentifier() )
325     {
326         fprintf( stderr, "Yield mutex grabbed in different thread !\n" );
327         abort();
328     }
329 #endif
330     mnCount = 1;
331     OMutex::release();
332     return nRet;
333 }
334 
Ungrab(int nGrabs)335 void GtkYieldMutex::Ungrab( int nGrabs )
336 {
337     // this MUST only be called when leaving the callback
338     // that locked the mutex with Grab()
339     OMutex::acquire();
340     mnCount = nGrabs;
341     if( mnCount == 0 )
342         mnThreadId = 0;
343     OMutex::release();
344 }
345