merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / plugadapt / salplug.cxx
blob10abd87e26f598e42380df808cbdc84ace1cf2a0
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 * 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"
40 #include <cstdio>
41 #include <unistd.h>
43 using namespace rtl;
45 extern "C" {
46 typedef SalInstance*(*salFactoryProc)( oslModule pModule);
49 static oslModule pCloseModule = NULL;
51 enum {
52 DESKTOP_NONE = 0,
53 DESKTOP_UNKNOWN,
54 DESKTOP_GNOME,
55 DESKTOP_KDE,
56 DESKTOP_KDE4,
57 DESKTOP_CDE
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 );
76 if( aMod )
78 salFactoryProc aProc = (salFactoryProc)osl_getAsciiFunctionSymbol( aMod, "create_SalInstance" );
79 if( aProc )
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(),
85 pInst );
86 #endif
87 if( pInst )
89 pCloseModule = aMod;
90 GetSalData()->m_pPlugin = aMod;
92 else
93 osl_unloadModule( aMod );
95 else
97 #if OSL_DEBUG_LEVEL > 1
98 std::fprintf( stderr, "could not load symbol %s from shared object %s\n",
99 "create_SalInstance",
100 OUStringToOString( aModule, RTL_TEXTENCODING_ASCII_US ).getStr() );
101 #endif
102 osl_unloadModule( aMod );
105 #if OSL_DEBUG_LEVEL > 1
106 else
107 std::fprintf( stderr, "could not load shared object %s\n",
108 OUStringToOString( aModule, RTL_TEXTENCODING_ASCII_US ).getStr() );
109 #endif
111 return pInst;
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 );
128 if( aMod )
130 rtl::OUString (*pSym)() = (rtl::OUString(*)())
131 osl_getAsciiFunctionSymbol( aMod, "get_desktop_environment" );
132 if( pSym )
133 aRet = pSym();
135 osl_unloadModule( aMod );
137 return aRet;
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] ) )
147 pRet = "svp";
148 else if ( desktop.equalsAscii( desktop_strings[DESKTOP_GNOME] ) )
149 pRet = "gtk";
150 else if( desktop.equalsAscii( desktop_strings[DESKTOP_KDE] ) )
151 pRet = "kde";
152 else if( desktop.equalsAscii( desktop_strings[DESKTOP_KDE4] ) )
153 pRet = "kde4";
154 else
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
160 pRet = "gtk";
163 #if OSL_DEBUG_LEVEL > 1
164 std::fprintf( stderr, "plugin autodetection: %s\n", pRet );
165 #endif
167 return pRet;
170 static SalInstance* check_headless_plugin()
172 int nParams = osl_getCommandArgCount();
173 OUString aParam;
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" ) ) );
180 return NULL;
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 ] ) );
204 if( ! pInst )
206 std::fprintf( stderr, "no suitable windowing system found, exiting.\n" );
207 _exit( 1 );
210 // acquire SolarMutex
211 pInst->AcquireYieldMutex( 1 );
213 return pInst;
216 void DestroySalInstance( SalInstance *pInst )
218 // release SolarMutex
219 pInst->ReleaseYieldMutex();
221 delete pInst;
222 if( pCloseModule )
223 osl_unloadModule( pCloseModule );
226 void InitSalData()
230 void DeInitSalData()
234 void InitSalMain()
238 void DeInitSalMain()
242 void SalAbort( const XubString& rErrorText )
244 if( !rErrorText.Len() )
245 std::fprintf( stderr, "Application Error" );
246 else
247 std::fprintf( stderr, "%s", ByteString( rErrorText, gsl_getSystemTextEncoding() ).GetBuffer() );
248 abort();
251 const OUString& SalGetDesktopEnvironment()
253 return get_desktop_environment();
256 SalData::SalData() :
257 m_pInstance(NULL),
258 m_pPlugin(NULL)
262 SalData::~SalData()