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 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
31 #include "osl/module.h"
32 #include "osl/process.h"
34 #include "rtl/ustrbuf.hxx"
37 #include "vcl/salinst.hxx"
38 #include "saldata.hxx"
46 typedef SalInstance
*(*salFactoryProc
)( oslModule pModule
);
49 static oslModule pCloseModule
= NULL
;
60 static const char * desktop_strings
[] = { "none", "unknown", "GNOME", "KDE", "KDE4", "CDE" };
62 static SalInstance
* tryInstance( const OUString
& rModuleBase
)
64 SalInstance
* pInst
= NULL
;
66 OUStringBuffer
aModName( 128 );
67 aModName
.appendAscii( SAL_DLLPREFIX
"vclplug_" );
68 aModName
.append( rModuleBase
);
69 aModName
.appendAscii( SAL_DLLPOSTFIX
);
70 aModName
.appendAscii( SAL_DLLEXTENSION
);
71 OUString aModule
= aModName
.makeStringAndClear();
73 oslModule aMod
= osl_loadModuleRelative(
74 reinterpret_cast< oslGenericFunction
>( &tryInstance
), aModule
.pData
,
75 SAL_LOADMODULE_DEFAULT
);
78 salFactoryProc aProc
= (salFactoryProc
)osl_getAsciiFunctionSymbol( aMod
, "create_SalInstance" );
81 pInst
= aProc( aMod
);
82 #if OSL_DEBUG_LEVEL > 1
83 std::fprintf( stderr
, "sal plugin %s produced instance %p\n",
84 OUStringToOString( aModule
, RTL_TEXTENCODING_ASCII_US
).getStr(),
90 GetSalData()->m_pPlugin
= aMod
;
93 osl_unloadModule( aMod
);
97 #if OSL_DEBUG_LEVEL > 1
98 std::fprintf( stderr
, "could not load symbol %s from shared object %s\n",
100 OUStringToOString( aModule
, RTL_TEXTENCODING_ASCII_US
).getStr() );
102 osl_unloadModule( aMod
);
105 #if OSL_DEBUG_LEVEL > 1
107 std::fprintf( stderr
, "could not load shared object %s\n",
108 OUStringToOString( aModule
, RTL_TEXTENCODING_ASCII_US
).getStr() );
114 static const rtl::OUString
& get_desktop_environment()
116 static rtl::OUString aRet
;
117 if( ! aRet
.getLength() )
119 OUStringBuffer
aModName( 128 );
120 aModName
.appendAscii( SAL_DLLPREFIX
"desktop_detector" );
121 aModName
.appendAscii( SAL_DLLPOSTFIX
);
122 aModName
.appendAscii( SAL_DLLEXTENSION
);
123 OUString aModule
= aModName
.makeStringAndClear();
125 oslModule aMod
= osl_loadModuleRelative(
126 reinterpret_cast< oslGenericFunction
>( &tryInstance
), aModule
.pData
,
127 SAL_LOADMODULE_DEFAULT
);
130 rtl::OUString (*pSym
)() = (rtl::OUString(*)())
131 osl_getAsciiFunctionSymbol( aMod
, "get_desktop_environment" );
135 osl_unloadModule( aMod
);
140 static const char* autodetect_plugin()
142 const rtl::OUString
& desktop( get_desktop_environment() );
143 const char * pRet
= "gen";
145 // no server at all: dummy plugin
146 if ( desktop
.equalsAscii( desktop_strings
[DESKTOP_NONE
] ) )
148 else if ( desktop
.equalsAscii( desktop_strings
[DESKTOP_GNOME
] ) )
150 else if( desktop
.equalsAscii( desktop_strings
[DESKTOP_KDE
] ) )
152 else if( desktop
.equalsAscii( desktop_strings
[DESKTOP_KDE4
] ) )
156 // #i95296# use the much nicer looking gtk plugin
157 // on desktops that set gtk variables (e.g. XFCE)
158 static const char* pEnv
= getenv( "GTK2_RC_FILES" );
159 if( pEnv
&& *pEnv
) // check for existance and non emptiness
163 #if OSL_DEBUG_LEVEL > 1
164 std::fprintf( stderr
, "plugin autodetection: %s\n", pRet
);
170 static SalInstance
* check_headless_plugin()
172 int nParams
= osl_getCommandArgCount();
174 for( int i
= 0; i
< nParams
; i
++ )
176 osl_getCommandArg( i
, &aParam
.pData
);
177 if( aParam
.equalsAscii( "-headless" ) )
178 return tryInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "svp" ) ) );
183 SalInstance
*CreateSalInstance()
185 SalInstance
* pInst
= NULL
;
187 static const char* pUsePlugin
= getenv( "SAL_USE_VCLPLUGIN" );
189 if( !(pUsePlugin
&& *pUsePlugin
) )
190 pInst
= check_headless_plugin();
192 if( ! pInst
&& !(pUsePlugin
&& *pUsePlugin
) )
193 pUsePlugin
= autodetect_plugin();
195 if( ! pInst
&& pUsePlugin
&& *pUsePlugin
)
196 pInst
= tryInstance( OUString::createFromAscii( pUsePlugin
) );
198 // fallback, try everything
199 const char* pPlugin
[] = { "gtk", "kde", "gen", 0 };
201 for ( int i
= 0; !pInst
&& pPlugin
[ i
]; ++i
)
202 pInst
= tryInstance( OUString::createFromAscii( pPlugin
[ i
] ) );
206 std::fprintf( stderr
, "no suitable windowing system found, exiting.\n" );
210 // acquire SolarMutex
211 pInst
->AcquireYieldMutex( 1 );
216 void DestroySalInstance( SalInstance
*pInst
)
218 // release SolarMutex
219 pInst
->ReleaseYieldMutex();
223 osl_unloadModule( pCloseModule
);
242 void SalAbort( const XubString
& rErrorText
)
244 if( !rErrorText
.Len() )
245 std::fprintf( stderr
, "Application Error" );
247 std::fprintf( stderr
, "%s", ByteString( rErrorText
, gsl_getSystemTextEncoding() ).GetBuffer() );
251 const OUString
& SalGetDesktopEnvironment()
253 return get_desktop_environment();