update dev300-m58
[ooovba.git] / vcl / unx / source / app / salinst.cxx
blobc2563abf557045e50aeb67892340d18f146f5549
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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"
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
38 #include "salunx.h"
40 #include "saldata.hxx"
41 #include "saldisp.hxx"
42 #include "salinst.h"
43 #include "salframe.h"
44 #include "dtint.hxx"
45 #include "salprn.h"
46 #include "sm.hxx"
48 #include "vcl/salwtype.hxx"
49 #include "vcl/salatype.hxx"
50 #include "vcl/helper.hxx"
52 #include "vos/mutex.hxx"
54 // -------------------------------------------------------------------------
56 // SalYieldMutex
58 // -------------------------------------------------------------------------
60 SalYieldMutex::SalYieldMutex()
62 mnCount = 0;
63 mnThreadId = 0;
66 void SalYieldMutex::acquire()
68 OMutex::acquire();
69 mnThreadId = NAMESPACE_VOS(OThread)::getCurrentIdentifier();
70 mnCount++;
73 void SalYieldMutex::release()
75 if ( mnThreadId == NAMESPACE_VOS(OThread)::getCurrentIdentifier() )
77 if ( mnCount == 1 )
78 mnThreadId = 0;
79 mnCount--;
81 OMutex::release();
84 sal_Bool SalYieldMutex::tryToAcquire()
86 if ( OMutex::tryToAcquire() )
88 mnThreadId = NAMESPACE_VOS(OThread)::getCurrentIdentifier();
89 mnCount++;
90 return True;
92 else
93 return False;
96 //----------------------------------------------------------------------------
98 // -=-= SalInstance =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
99 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
101 // plugin factory function
102 extern "C"
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;
112 pSalData->Init();
114 return 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
125 // a little late
127 X11SalData *pSalData = GetX11SalData();
128 pSalData->deInitNWF();
129 delete pSalData;
130 SetSalData( NULL );
132 delete mpSalYieldMutex;
136 // --------------------------------------------------------
137 // AnyInput from sv/mow/source/app/svapp.cxx
139 struct PredicateReturn
141 USHORT nType;
142 BOOL bRet;
145 extern "C" {
146 Bool ImplPredicateEvent( Display *, XEvent *pEvent, char *pData )
148 PredicateReturn *pPre = (PredicateReturn *)pData;
150 if ( pPre->bRet )
151 return False;
153 USHORT nType;
155 switch( pEvent->type )
157 case ButtonPress:
158 case ButtonRelease:
159 case MotionNotify:
160 case EnterNotify:
161 case LeaveNotify:
162 nType = INPUT_MOUSE;
163 break;
165 case XLIB_KeyPress:
166 //case KeyRelease:
167 nType = INPUT_KEYBOARD;
168 break;
169 case Expose:
170 case GraphicsExpose:
171 case NoExpose:
172 nType = INPUT_PAINT;
173 break;
174 default:
175 nType = 0;
178 if ( (nType & pPre->nType) || ( ! nType && (pPre->nType & INPUT_OTHER) ) )
179 pPre->bRet = TRUE;
181 return False;
185 bool X11SalInstance::AnyInput(USHORT nType)
187 X11SalData *pSalData = GetX11SalData();
188 Display *pDisplay = pSalData->GetDisplay()->GetDisplay();
189 BOOL bRet = FALSE;
191 if( (nType & INPUT_TIMER) &&
192 pSalData->GetDisplay()->GetXLib()->CheckTimeout( false ) )
194 bRet = TRUE;
196 else if (XPending(pDisplay) )
198 PredicateReturn aInput;
199 XEvent aEvent;
201 aInput.bRet = FALSE;
202 aInput.nType = nType;
204 XCheckIfEvent(pDisplay, &aEvent, ImplPredicateEvent,
205 (char *)&aInput );
207 bRet = aInput.bRet;
209 return bRet;
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();
226 ULONG n = nCount;
227 while ( n )
229 pYieldMutex->release();
230 n--;
233 return nCount;
235 else
236 return 0;
239 // -----------------------------------------------------------------------
241 void X11SalInstance::AcquireYieldMutex( ULONG nCount )
243 SalYieldMutex* pYieldMutex = mpSalYieldMutex;
244 while ( nCount )
246 pYieldMutex->acquire();
247 nCount--;
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 );
266 return pFrame;
269 SalFrame* X11SalInstance::CreateChildFrame( SystemParentData* pParentData, ULONG nStyle )
271 SalFrame* pFrame = new X11SalFrame( NULL, nStyle, pParentData );
273 return pFrame;
276 void X11SalInstance::DestroyFrame( SalFrame* pFrame )
278 delete pFrame;
281 static void getServerDirectories( std::list< rtl::OString >& o_rFontPaths )
283 #ifdef LINUX
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" );
295 aLines.clear();
296 if( pPipe )
298 char line[1024];
299 char* pSearch;
300 while( fgets( line, sizeof(line), pPipe ) )
302 int nLen = strlen( line );
303 if( line[nLen-1] == '\n' )
304 line[nLen-1] = 0;
305 pSearch = strstr( line, ": " );
306 if( pSearch )
307 aLines.push_back( pSearch+2 );
309 if( ! pclose( pPipe ) )
310 break;
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() );
321 #endif
324 #else
325 (void)o_rFontPaths;
326 #endif
331 void X11SalInstance::FillFontPathList( std::list< rtl::OString >& o_rFontPaths )
333 Display *pDisplay = GetX11SalData()->GetDisplay()->GetDisplay();
335 DBG_ASSERT( pDisplay, "No Display !" );
336 if( pDisplay )
338 // get font paths to look for fonts
339 int nPaths = 0, i;
340 char** pPaths = XGetFontPath( pDisplay, &nPaths );
342 bool bServerDirs = false;
343 for( i = 0; i < nPaths; i++ )
345 OString aPath( pPaths[i] );
346 sal_Int32 nPos = 0;
347 if( ! bServerDirs
348 && ( nPos = aPath.indexOf( ':' ) ) > 0
349 && ( !aPath.copy(nPos).equals( ":unscaled" ) ) )
351 bServerDirs = true;
352 getServerDirectories( o_rFontPaths );
354 else
356 psp::normPath( aPath );
357 o_rFontPaths.push_back( aPath );
361 if( nPaths )
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" );
372 #ifdef SOLARIS
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");
379 if ( lang != NULL )
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() )
390 ByteString aLine;
391 aStream.ReadLine( aLine );
392 // need an OString for normpath
393 OString aNLine( aLine );
394 psp::normPath( aNLine );
395 aLine = 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 )
399 continue;
400 o_rFontPaths.push_back( aLine );
403 #endif /* SOLARIS */