1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: salinst.cxx,v $
10 * $Revision: 1.34.154.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
40 #include "saldata.hxx"
41 #include "saldisp.hxx"
48 #include "vcl/salwtype.hxx"
49 #include "vcl/salatype.hxx"
50 #include "vcl/helper.hxx"
52 #include "vos/mutex.hxx"
54 // -------------------------------------------------------------------------
58 // -------------------------------------------------------------------------
60 SalYieldMutex::SalYieldMutex()
66 void SalYieldMutex::acquire()
69 mnThreadId
= NAMESPACE_VOS(OThread
)::getCurrentIdentifier();
73 void SalYieldMutex::release()
75 if ( mnThreadId
== NAMESPACE_VOS(OThread
)::getCurrentIdentifier() )
84 sal_Bool
SalYieldMutex::tryToAcquire()
86 if ( OMutex::tryToAcquire() )
88 mnThreadId
= NAMESPACE_VOS(OThread
)::getCurrentIdentifier();
96 //----------------------------------------------------------------------------
98 // -=-= SalInstance =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
99 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
101 // plugin factory function
104 VCL_DLLPUBLIC SalInstance
* create_SalInstance()
106 X11SalInstance
* pInstance
= new X11SalInstance( new SalYieldMutex() );
108 // initialize SalData
109 X11SalData
*pSalData
= new X11SalData
;
110 SetSalData( pSalData
);
111 pSalData
->m_pInstance
= pInstance
;
118 X11SalInstance::~X11SalInstance()
120 // close session management
121 SessionManagerClient::close();
123 // dispose SalDisplay list from SalData
124 // would be done in a static destructor else which is
127 X11SalData
*pSalData
= GetX11SalData();
128 pSalData
->deInitNWF();
132 delete mpSalYieldMutex
;
136 // --------------------------------------------------------
137 // AnyInput from sv/mow/source/app/svapp.cxx
139 struct PredicateReturn
146 Bool
ImplPredicateEvent( Display
*, XEvent
*pEvent
, char *pData
)
148 PredicateReturn
*pPre
= (PredicateReturn
*)pData
;
155 switch( pEvent
->type
)
167 nType
= INPUT_KEYBOARD
;
178 if ( (nType
& pPre
->nType
) || ( ! nType
&& (pPre
->nType
& INPUT_OTHER
) ) )
185 bool X11SalInstance::AnyInput(USHORT nType
)
187 X11SalData
*pSalData
= GetX11SalData();
188 Display
*pDisplay
= pSalData
->GetDisplay()->GetDisplay();
191 if( (nType
& INPUT_TIMER
) &&
192 pSalData
->GetDisplay()->GetXLib()->CheckTimeout( false ) )
196 else if (XPending(pDisplay
) )
198 PredicateReturn aInput
;
202 aInput
.nType
= nType
;
204 XCheckIfEvent(pDisplay
, &aEvent
, ImplPredicateEvent
,
212 vos::IMutex
* X11SalInstance::GetYieldMutex()
214 return mpSalYieldMutex
;
217 // -----------------------------------------------------------------------
219 ULONG
X11SalInstance::ReleaseYieldMutex()
221 SalYieldMutex
* pYieldMutex
= mpSalYieldMutex
;
222 if ( pYieldMutex
->GetThreadId() ==
223 NAMESPACE_VOS(OThread
)::getCurrentIdentifier() )
225 ULONG nCount
= pYieldMutex
->GetAcquireCount();
229 pYieldMutex
->release();
239 // -----------------------------------------------------------------------
241 void X11SalInstance::AcquireYieldMutex( ULONG nCount
)
243 SalYieldMutex
* pYieldMutex
= mpSalYieldMutex
;
246 pYieldMutex
->acquire();
251 void X11SalInstance::Yield( bool bWait
, bool bHandleAllCurrentEvents
)
252 { GetX11SalData()->GetLib()->Yield( bWait
, bHandleAllCurrentEvents
); }
254 void* X11SalInstance::GetConnectionIdentifier( ConnectionIdentifierType
& rReturnedType
, int& rReturnedBytes
)
256 static const char* pDisplay
= getenv( "DISPLAY" );
257 rReturnedType
= AsciiCString
;
258 rReturnedBytes
= pDisplay
? strlen( pDisplay
)+1 : 1;
259 return pDisplay
? (void*)pDisplay
: (void*)"";
262 SalFrame
*X11SalInstance::CreateFrame( SalFrame
*pParent
, ULONG nSalFrameStyle
)
264 SalFrame
*pFrame
= new X11SalFrame( pParent
, nSalFrameStyle
);
269 SalFrame
* X11SalInstance::CreateChildFrame( SystemParentData
* pParentData
, ULONG nStyle
)
271 SalFrame
* pFrame
= new X11SalFrame( NULL
, nStyle
, pParentData
);
276 void X11SalInstance::DestroyFrame( SalFrame
* pFrame
)
281 static void getServerDirectories( std::list
< rtl::OString
>& o_rFontPaths
)
285 * chkfontpath exists on some (RH derived) Linux distributions
287 static const char* pCommands
[] = {
288 "/usr/sbin/chkfontpath 2>/dev/null", "chkfontpath 2>/dev/null"
290 ::std::list
< ByteString
> aLines
;
292 for( unsigned int i
= 0; i
< sizeof(pCommands
)/sizeof(pCommands
[0]); i
++ )
294 FILE* pPipe
= popen( pCommands
[i
], "r" );
300 while( fgets( line
, sizeof(line
), pPipe
) )
302 int nLen
= strlen( line
);
303 if( line
[nLen
-1] == '\n' )
305 pSearch
= strstr( line
, ": " );
307 aLines
.push_back( pSearch
+2 );
309 if( ! pclose( pPipe
) )
314 for( ::std::list
< ByteString
>::iterator it
= aLines
.begin(); it
!= aLines
.end(); ++it
)
316 if( ! access( it
->GetBuffer(), F_OK
) )
318 o_rFontPaths
.push_back( *it
);
319 #if OSL_DEBUG_LEVEL > 1
320 fprintf( stderr
, "adding fs dir %s\n", it
->GetBuffer() );
331 void X11SalInstance::FillFontPathList( std::list
< rtl::OString
>& o_rFontPaths
)
333 Display
*pDisplay
= GetX11SalData()->GetDisplay()->GetDisplay();
335 DBG_ASSERT( pDisplay
, "No Display !" );
338 // get font paths to look for fonts
340 char** pPaths
= XGetFontPath( pDisplay
, &nPaths
);
342 bool bServerDirs
= false;
343 for( i
= 0; i
< nPaths
; i
++ )
345 OString
aPath( pPaths
[i
] );
348 && ( nPos
= aPath
.indexOf( ':' ) ) > 0
349 && ( !aPath
.copy(nPos
).equals( ":unscaled" ) ) )
352 getServerDirectories( o_rFontPaths
);
356 psp::normPath( aPath
);
357 o_rFontPaths
.push_back( aPath
);
362 XFreeFontPath( pPaths
);
365 // insert some standard directories
366 o_rFontPaths
.push_back( "/usr/openwin/lib/X11/fonts/TrueType" );
367 o_rFontPaths
.push_back( "/usr/openwin/lib/X11/fonts/Type1" );
368 o_rFontPaths
.push_back( "/usr/openwin/lib/X11/fonts/Type1/sun" );
369 o_rFontPaths
.push_back( "/usr/X11R6/lib/X11/fonts/truetype" );
370 o_rFontPaths
.push_back( "/usr/X11R6/lib/X11/fonts/Type1" );
373 /* cde specials, from /usr/dt/bin/Xsession: here are the good fonts,
374 the OWfontpath file may contain as well multiple lines as a comma
375 separated list of fonts in each line. to make it even more weird
376 environment variables are allowed as well */
378 const char* lang
= getenv("LANG");
381 String
aOpenWinDir( String::CreateFromAscii( "/usr/openwin/lib/locale/" ) );
382 aOpenWinDir
.AppendAscii( lang
);
383 aOpenWinDir
.AppendAscii( "/OWfontpath" );
385 SvFileStream
aStream( aOpenWinDir
, STREAM_READ
);
387 // TODO: replace environment variables
388 while( aStream
.IsOpen() && ! aStream
.IsEof() )
391 aStream
.ReadLine( aLine
);
392 // need an OString for normpath
393 OString
aNLine( aLine
);
394 psp::normPath( aNLine
);
396 // try to avoid bad fonts in some cases
397 static bool bAvoid
= (strncasecmp( lang
, "ar", 2 ) == 0) || (strncasecmp( lang
, "he", 2 ) == 0) || strncasecmp( lang
, "iw", 2 ) == 0 || (strncasecmp( lang
, "hi", 2 ) == 0);
398 if( bAvoid
&& aLine
.Search( "iso_8859" ) != STRING_NOTFOUND
)
400 o_rFontPaths
.push_back( aLine
);