Revert "Revert "Revert "stronger typing for SwClient::GetRegisteredIn"" and fix SwIte...
[LibreOffice.git] / vcl / unx / generic / app / salinst.cxx
blob61c091a2d0155f59df54bc120e2c5f5968794345
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <stdlib.h>
22 #include <config_features.h>
23 #include <vcl/skia/SkiaHelper.hxx>
24 #include <config_skia.h>
25 #if HAVE_FEATURE_SKIA
26 #include <skia/x11/gdiimpl.hxx>
27 #include <skia/salbmp.hxx>
28 #endif
30 #include <headless/svpbmp.hxx>
31 #include <unx/saldata.hxx>
32 #include <unx/saldisp.hxx>
33 #include <unx/salinst.h>
34 #include <unx/geninst.h>
35 #include <unx/genpspgraphics.h>
36 #include <unx/salframe.h>
37 #include <unx/sm.hxx>
38 #include <unx/i18n_im.hxx>
40 #include <vcl/inputtypes.hxx>
42 #include <salwtype.hxx>
44 // plugin factory function
45 extern "C"
47 VCLPLUG_GEN_PUBLIC SalInstance* create_SalInstance()
49 /* #i92121# workaround deadlocks in the X11 implementation
51 static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" );
52 /* #i90094#
53 from now on we know that an X connection will be
54 established, so protect X against itself
56 if( ! ( pNoXInitThreads && *pNoXInitThreads ) )
57 XInitThreads();
59 X11SalInstance* pInstance = new X11SalInstance( std::make_unique<SalYieldMutex>() );
61 // initialize SalData
62 X11SalData *pSalData = new X11SalData();
64 pSalData->Init();
65 pInstance->SetLib( pSalData->GetLib() );
67 return pInstance;
71 X11SalInstance::X11SalInstance(std::unique_ptr<SalYieldMutex> pMutex)
72 : SalGenericInstance(std::move(pMutex))
73 , mpXLib(nullptr)
75 ImplSVData* pSVData = ImplGetSVData();
76 // [-loplugin:ostr] if we use a literal here, we get use-after-free on shutdown
77 pSVData->maAppData.mxToolkitName = OUString("x11");
78 m_bSupportsOpenGL = true;
79 #if HAVE_FEATURE_SKIA
80 X11SkiaSalGraphicsImpl::prepareSkia();
81 #if SKIA_USE_BITMAP32
82 if (SkiaHelper::isVCLSkiaEnabled())
83 m_bSupportsBitmap32 = true;
84 #endif
85 #endif
88 X11SalInstance::~X11SalInstance()
90 // close session management
91 SessionManagerClient::close();
93 // dispose SalDisplay list from SalData
94 // would be done in a static destructor else which is
95 // a little late
96 GetGenericUnixSalData()->Dispose();
98 #if HAVE_FEATURE_SKIA
99 SkiaHelper::cleanup();
100 #endif
103 SalX11Display* X11SalInstance::CreateDisplay() const
105 return new SalX11Display( mpXLib->GetDisplay() );
108 // AnyInput from sv/mow/source/app/svapp.cxx
110 namespace {
112 struct PredicateReturn
114 VclInputFlags nType;
115 bool bRet;
120 extern "C" {
121 static Bool ImplPredicateEvent( Display *, XEvent *pEvent, char *pData )
123 PredicateReturn *pPre = reinterpret_cast<PredicateReturn *>(pData);
125 if ( pPre->bRet )
126 return False;
128 VclInputFlags nType;
130 switch( pEvent->type )
132 case ButtonPress:
133 case ButtonRelease:
134 case MotionNotify:
135 case EnterNotify:
136 case LeaveNotify:
137 nType = VclInputFlags::MOUSE;
138 break;
140 case KeyPress:
141 //case KeyRelease:
142 nType = VclInputFlags::KEYBOARD;
143 break;
144 case Expose:
145 case GraphicsExpose:
146 case NoExpose:
147 nType = VclInputFlags::PAINT;
148 break;
149 default:
150 nType = VclInputFlags::NONE;
153 if ( (nType & pPre->nType) || ( nType == VclInputFlags::NONE && (pPre->nType & VclInputFlags::OTHER) ) )
154 pPre->bRet = true;
156 return False;
160 bool X11SalInstance::AnyInput(VclInputFlags nType)
162 GenericUnixSalData *pData = GetGenericUnixSalData();
163 Display *pDisplay = vcl_sal::getSalDisplay(pData)->GetDisplay();
164 bool bRet = false;
166 if( (nType & VclInputFlags::TIMER) && (mpXLib && mpXLib->CheckTimeout(false)) )
167 bRet = true;
169 if( !bRet && XPending(pDisplay) )
171 PredicateReturn aInput;
172 XEvent aEvent;
174 aInput.bRet = false;
175 aInput.nType = nType;
177 XCheckIfEvent(pDisplay, &aEvent, ImplPredicateEvent,
178 reinterpret_cast<char *>(&aInput) );
180 bRet = aInput.bRet;
182 #if OSL_DEBUG_LEVEL > 1
183 SAL_INFO("vcl.app", "AnyInput "
184 << std::showbase << std::hex
185 << static_cast<unsigned int>(nType)
186 << " = " << (bRet ? "true" : "false"));
187 #endif
188 return bRet;
191 bool X11SalInstance::DoYield(bool bWait, bool bHandleAllCurrentEvents)
193 return mpXLib->Yield( bWait, bHandleAllCurrentEvents );
196 OUString X11SalInstance::GetConnectionIdentifier()
198 static const char* pDisplay = getenv( "DISPLAY" );
199 return pDisplay ? OUString::createFromAscii(pDisplay) : OUString();
202 SalFrame *X11SalInstance::CreateFrame( SalFrame *pParent, SalFrameStyleFlags nSalFrameStyle )
204 SalFrame *pFrame = new X11SalFrame( pParent, nSalFrameStyle );
206 return pFrame;
209 SalFrame* X11SalInstance::CreateChildFrame( SystemParentData* pParentData, SalFrameStyleFlags nStyle )
211 SalFrame* pFrame = new X11SalFrame( nullptr, nStyle, pParentData );
213 return pFrame;
216 void X11SalInstance::DestroyFrame( SalFrame* pFrame )
218 delete pFrame;
221 void X11SalInstance::AfterAppInit()
223 assert( mpXLib->GetDisplay() );
224 assert( mpXLib->GetInputMethod() );
226 SalX11Display *pSalDisplay = CreateDisplay();
227 mpXLib->GetInputMethod()->CreateMethod( mpXLib->GetDisplay() );
228 pSalDisplay->SetupInput();
231 void X11SalInstance::AddToRecentDocumentList(const OUString&, const OUString&, const OUString&) {}
233 void X11SalInstance::PostPrintersChanged()
235 SalDisplay* pDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
236 for (auto pSalFrame : pDisp->getFrames() )
237 pDisp->PostEvent( pSalFrame, nullptr, SalEvent::PrinterChanged );
240 std::unique_ptr<GenPspGraphics> X11SalInstance::CreatePrintGraphics()
242 return std::make_unique<GenPspGraphics>();
245 std::shared_ptr<SalBitmap> X11SalInstance::CreateSalBitmap()
247 #if HAVE_FEATURE_SKIA
248 if (SkiaHelper::isVCLSkiaEnabled())
249 return std::make_shared<SkiaSalBitmap>();
250 #endif
251 return std::make_shared<SvpSalBitmap>();
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */