1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // Th current high-level preprocessor structure is:
22 // if !HAVE_FEATURE_UI
23 // => STATIC_SAL_INSTANCE
25 // ? !STATIC_SAL_INSTANCE
26 // ? UNIX_DESKTOP_DETECT
29 // ENABLE_HEADLESS just signifies the use of the SVP plugin!
31 #include <config_features.h>
32 #include <config_vclplug.h>
35 #include <desktop/crashreport.hxx>
36 #include <rtl/bootstrap.hxx>
37 #include <rtl/process.h>
38 #include <salinst.hxx>
39 #include <sal/log.hxx>
41 #include <vcl/svapp.hxx>
45 #define UNIX_DESKTOP_DETECT 1
46 #include <unx/desktops.hxx>
48 #define UNIX_DESKTOP_DETECT 0
52 #if defined(DISABLE_DYNLOADING) || !HAVE_FEATURE_UI
53 #define STATIC_SAL_INSTANCE 1
54 extern "C" SalInstance
* create_SalInstance();
56 #define STATIC_SAL_INSTANCE 0
57 #include <osl/module.hxx>
62 #include <UIKit/UIKit.h>
65 #elif defined(ANDROID)
66 #include <android/androidinst.hxx>
70 #include <o3tl/char16_t2wchar_t.hxx>
71 #include <salframe.hxx>
78 #include <headless/svpdata.hxx>
79 #include <headless/svpinst.hxx>
85 SalInstance
* svp_create_SalInstance()
87 SvpSalInstance
* pInstance
= new SvpSalInstance(std::make_unique
<SvpSalYieldMutex
>());
95 #if !STATIC_SAL_INSTANCE
96 oslModule pCloseModule
= nullptr;
98 extern "C" typedef SalInstance
* (*salFactoryProc
)();
100 SalInstance
* tryInstance( const OUString
& rModuleBase
, bool bForce
= false )
103 if (rModuleBase
== "svp")
104 return svp_create_SalInstance();
107 SalInstance
* pInst
= nullptr;
108 OUString
aUsedModuleBase(rModuleBase
);
109 if (aUsedModuleBase
== "kde5")
110 aUsedModuleBase
= "kf5";
115 "vclplug_" + aUsedModuleBase
+ "lo" SAL_DLLEXTENSION
);
118 if (aMod
.loadRelative(reinterpret_cast<oslGenericFunction
>(&tryInstance
), aModule
, SAL_LOADMODULE_GLOBAL
))
120 salFactoryProc aProc
= reinterpret_cast<salFactoryProc
>(aMod
.getFunctionSymbol("create_SalInstance"));
126 "sal plugin " << aModule
<< " produced instance " << pInst
);
129 pCloseModule
= static_cast<oslModule
>(aMod
);
133 * Recent GTK+ versions load their modules with RTLD_LOCAL, so we can
134 * not access the 'gnome_accessibility_module_shutdown' anymore.
135 * So make sure libgtk+ & co are still mapped into memory when
136 * atk-bridge's atexit handler gets called.
138 if (aUsedModuleBase
== "gtk4" || aUsedModuleBase
== "gtk3" ||
139 aUsedModuleBase
== "gtk3_kde5" || aUsedModuleBase
== "kf5" ||
140 aUsedModuleBase
== "qt5" || aUsedModuleBase
== "qt6" ||
141 aUsedModuleBase
== "win")
143 pCloseModule
= nullptr;
151 "could not load symbol create_SalInstance from shared object "
157 SAL_WARN("vcl.plugadapt", "could not load shared object " << aModule
);
161 SAL_INFO("vcl.plugadapt", "could not load shared object " << aModule
);
164 // coverity[leaked_storage] - this is on purpose
167 #endif // !STATIC_SAL_INSTANCE
169 #if UNIX_DESKTOP_DETECT
170 #ifndef DISABLE_DYNLOADING
171 extern "C" typedef DesktopType
Fn_get_desktop_environment();
173 extern "C" DesktopType
get_desktop_environment();
176 DesktopType
lcl_get_desktop_environment()
178 DesktopType ret
= DESKTOP_UNKNOWN
;
179 #ifdef DISABLE_DYNLOADING
180 ret
= get_desktop_environment();
182 OUString
aModule(DESKTOP_DETECTOR_DLL_NAME
);
183 oslModule aMod
= osl_loadModuleRelative(
184 reinterpret_cast< oslGenericFunction
>( &tryInstance
), aModule
.pData
,
185 SAL_LOADMODULE_DEFAULT
);
188 Fn_get_desktop_environment
* pSym
189 = reinterpret_cast<Fn_get_desktop_environment
*>(
190 osl_getAsciiFunctionSymbol(aMod
, "get_desktop_environment"));
194 osl_unloadModule( aMod
);
199 #if !STATIC_SAL_INSTANCE
200 const char* const* autodetect_plugin_list()
202 static const char* const pKDEFallbackList
[] =
219 static const char* const pStandardFallbackList
[] =
231 static const char* const pHeadlessFallbackList
[] =
238 DesktopType desktop
= lcl_get_desktop_environment();
239 const char * const * pList
= pStandardFallbackList
;
242 // no server at all: dummy plugin
243 if ( desktop
== DESKTOP_NONE
)
244 pList
= pHeadlessFallbackList
;
247 if ( desktop
== DESKTOP_GNOME
||
248 desktop
== DESKTOP_UNITY
||
249 desktop
== DESKTOP_XFCE
||
250 desktop
== DESKTOP_MATE
)
251 pList
= pStandardFallbackList
;
252 else if (desktop
== DESKTOP_PLASMA5
|| desktop
== DESKTOP_LXQT
)
253 pList
= pKDEFallbackList
;
257 #endif // !STATIC_SAL_INSTANCE
258 #endif // UNIX_DESKTOP_DETECT
260 #endif // HAVE_FEATURE_UI
262 // HACK to obtain Application::IsHeadlessModeEnabled early on, before
263 // Application::EnableHeadlessMode has potentially been called:
264 bool IsHeadlessModeRequested()
266 if (Application::IsHeadlessModeEnabled()) {
269 sal_uInt32 n
= rtl_getAppCommandArgCount();
270 for (sal_uInt32 i
= 0; i
< n
; ++i
) {
272 rtl_getAppCommandArg(i
, &arg
.pData
);
273 if ( arg
== "--headless" || arg
== "-headless" ) {
280 } // anonymous namespace
282 SalInstance
*CreateSalInstance()
285 rtl::Bootstrap::get("SAL_USE_VCLPLUGIN", aUsePlugin
);
286 SAL_INFO_IF(!aUsePlugin
.isEmpty(), "vcl.plugadapt", "Requested VCL plugin: " << aUsePlugin
);
288 if (Application::IsBitmapRendering() || (aUsePlugin
.isEmpty() && IsHeadlessModeRequested()))
291 if (aUsePlugin
== "svp")
293 Application::EnableBitmapRendering();
295 return svp_create_SalInstance();
301 #if STATIC_SAL_INSTANCE
302 return create_SalInstance();
304 #else // !STATIC_SAL_INSTANCE
305 SalInstance
*pInst
= nullptr;
307 if( !aUsePlugin
.isEmpty() )
308 pInst
= tryInstance( aUsePlugin
, true );
310 #if UNIX_DESKTOP_DETECT
311 const char* const* pPluginList
= pInst
? nullptr : autodetect_plugin_list();
312 for (int i
= 0; !pInst
&& pPluginList
[i
]; ++i
)
314 pInst
= tryInstance(OUString::createFromAscii(pPluginList
[i
]));
315 SAL_INFO_IF(pInst
, "vcl.plugadapt", "plugin autodetection: " << pPluginList
[i
]);
319 // fallback, try everything
320 static const char* const pPlugin
[] = {
323 #elif defined(MACOSX)
325 #else // !_WIN32 && !MACOSX
344 #endif // !_WIN32 && !MACOSX
348 for (int i
= 0; !pInst
&& pPlugin
[i
]; ++i
)
349 pInst
= tryInstance( OUString::createFromAscii( pPlugin
[ i
] ) );
353 std::fprintf( stderr
, "no suitable windowing system found, exiting.\n" );
358 #endif // !STATIC_SAL_INSTANCE
361 void DestroySalInstance( SalInstance
*pInst
)
364 #if !STATIC_SAL_INSTANCE
366 osl_unloadModule( pCloseModule
);
370 void SalAbort( const OUString
& rErrorText
, bool bDumpCore
)
372 if (GetSalInstance())
373 GetSalInstance()->BeforeAbort(rErrorText
, bDumpCore
);
376 if( rErrorText
.isEmpty() )
378 // make sure crash reporter is triggered
379 RaiseException( 0, EXCEPTION_NONCONTINUABLE
, 0, nullptr );
380 FatalAppExitW( 0, L
"Application Error" );
384 CrashReporter::addKeyValue("AbortMessage", rErrorText
, CrashReporter::Write
);
385 // make sure crash reporter is triggered
386 RaiseException( 0, EXCEPTION_NONCONTINUABLE
, 0, nullptr );
387 FatalAppExitW( 0, o3tl::toW(rErrorText
.getStr()) );
391 OUString
aError(rErrorText
.isEmpty() ? "Unspecified application error" : rErrorText
);
392 LOGE("SalAbort: '%s'", OUStringToOString(aError
, osl_getThreadTextEncoding()).getStr());
394 NSLog(@
"SalAbort: %s", OUStringToOString(rErrorText
, osl_getThreadTextEncoding()).getStr());
396 if( rErrorText
.isEmpty() )
397 std::fprintf( stderr
, "Unspecified Application Error\n" );
400 CrashReporter::addKeyValue("AbortMessage", rErrorText
, CrashReporter::Write
);
401 std::fprintf( stderr
, "%s\n", OUStringToOString(rErrorText
, osl_getThreadTextEncoding()).getStr() );
411 const OUString
& SalGetDesktopEnvironment()
414 static OUString
aDesktopEnvironment("headless");
415 #elif defined(_WIN32)
416 static OUString
aDesktopEnvironment( "Windows" );
417 #elif defined(MACOSX)
418 static OUString
aDesktopEnvironment( "MacOSX" );
419 #elif defined(EMSCRIPTEN)
420 static OUString
aDesktopEnvironment("WASM");
421 #elif defined(ANDROID)
422 static OUString
aDesktopEnvironment("android");
424 static OUString
aDesktopEnvironment("iOS");
425 #elif UNIX_DESKTOP_DETECT
426 // Order to match desktops.hxx' DesktopType
427 static const char * const desktop_strings
[] = {
428 "none", "unknown", "GNOME", "UNITY",
429 "XFCE", "MATE", "PLASMA5", "LXQT" };
430 static OUString aDesktopEnvironment
;
431 if( aDesktopEnvironment
.isEmpty())
433 aDesktopEnvironment
= OUString::createFromAscii(
434 desktop_strings
[lcl_get_desktop_environment()]);
437 static OUString
aDesktopEnvironment("unknown");
439 return aDesktopEnvironment
;
445 BOOL bIsRunning
= FALSE
;
446 // pvParam must be BOOL
447 return SystemParametersInfoW(SPI_GETSCREENREADER
, 0, &bIsRunning
, 0)
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */