Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 : #include <stdio.h>
22 : #include <stdlib.h>
23 :
24 : #include "osl/module.hxx"
25 :
26 : #include "unx/salunx.h"
27 : #include "unx/saldata.hxx"
28 : #include "unx/saldisp.hxx"
29 : #include "generic/geninst.h"
30 : #include "generic/genpspgraphics.h"
31 : #include "unx/salframe.h"
32 : #include "generic/genprn.h"
33 : #include "unx/sm.hxx"
34 :
35 : #include "vcl/apptypes.hxx"
36 : #include "vcl/helper.hxx"
37 :
38 : #include "salwtype.hxx"
39 : #include <sal/macros.h>
40 :
41 : // plugin factory function
42 : extern "C"
43 : {
44 0 : VCLPLUG_GEN_PUBLIC SalInstance* create_SalInstance()
45 : {
46 : /* #i92121# workaround deadlocks in the X11 implementation
47 : */
48 0 : static const char* pNoXInitThreads = getenv( "SAL_NO_XINITTHREADS" );
49 : /* #i90094#
50 : from now on we know that an X connection will be
51 : established, so protect X against itself
52 : */
53 0 : if( ! ( pNoXInitThreads && *pNoXInitThreads ) )
54 0 : XInitThreads();
55 :
56 0 : X11SalInstance* pInstance = new X11SalInstance( new SalYieldMutex() );
57 :
58 : // initialize SalData
59 0 : X11SalData *pSalData = new X11SalData( SAL_DATA_UNX, pInstance );
60 :
61 0 : pSalData->Init();
62 0 : pInstance->SetLib( pSalData->GetLib() );
63 :
64 0 : return pInstance;
65 : }
66 : }
67 :
68 0 : X11SalInstance::~X11SalInstance()
69 : {
70 : // close session management
71 0 : SessionManagerClient::close();
72 :
73 : // dispose SalDisplay list from SalData
74 : // would be done in a static destructor else which is
75 : // a little late
76 0 : GetGenericData()->Dispose();
77 0 : }
78 :
79 : // AnyInput from sv/mow/source/app/svapp.cxx
80 :
81 : struct PredicateReturn
82 : {
83 : sal_uInt16 nType;
84 : bool bRet;
85 : };
86 :
87 : extern "C" {
88 0 : Bool ImplPredicateEvent( Display *, XEvent *pEvent, char *pData )
89 : {
90 0 : PredicateReturn *pPre = (PredicateReturn *)pData;
91 :
92 0 : if ( pPre->bRet )
93 0 : return False;
94 :
95 : sal_uInt16 nType;
96 :
97 0 : switch( pEvent->type )
98 : {
99 : case ButtonPress:
100 : case ButtonRelease:
101 : case MotionNotify:
102 : case EnterNotify:
103 : case LeaveNotify:
104 0 : nType = VCL_INPUT_MOUSE;
105 0 : break;
106 :
107 : case KeyPress:
108 : //case KeyRelease:
109 0 : nType = VCL_INPUT_KEYBOARD;
110 0 : break;
111 : case Expose:
112 : case GraphicsExpose:
113 : case NoExpose:
114 0 : nType = VCL_INPUT_PAINT;
115 0 : break;
116 : default:
117 0 : nType = 0;
118 : }
119 :
120 0 : if ( (nType & pPre->nType) || ( ! nType && (pPre->nType & VCL_INPUT_OTHER) ) )
121 0 : pPre->bRet = true;
122 :
123 0 : return False;
124 : }
125 : }
126 :
127 0 : bool X11SalInstance::AnyInput(sal_uInt16 nType)
128 : {
129 0 : SalGenericData *pData = GetGenericData();
130 0 : Display *pDisplay = pData->GetSalDisplay()->GetDisplay();
131 0 : bool bRet = false;
132 :
133 0 : if( (nType & VCL_INPUT_TIMER) && (mpXLib && mpXLib->CheckTimeout(false)) )
134 0 : bRet = true;
135 0 : else if (XPending(pDisplay) )
136 : {
137 : PredicateReturn aInput;
138 : XEvent aEvent;
139 :
140 0 : aInput.bRet = false;
141 0 : aInput.nType = nType;
142 :
143 : XCheckIfEvent(pDisplay, &aEvent, ImplPredicateEvent,
144 0 : (char *)&aInput );
145 :
146 0 : bRet = aInput.bRet;
147 : }
148 : #if OSL_DEBUG_LEVEL > 1
149 : fprintf( stderr, "AnyInput 0x%x = %s\n", nType, bRet ? "true" : "false" );
150 : #endif
151 0 : return bRet;
152 : }
153 :
154 0 : void X11SalInstance::Yield( bool bWait, bool bHandleAllCurrentEvents )
155 : {
156 0 : mpXLib->Yield( bWait, bHandleAllCurrentEvents );
157 0 : }
158 :
159 0 : void* X11SalInstance::GetConnectionIdentifier( ConnectionIdentifierType& rReturnedType,
160 : int& rReturnedBytes )
161 : {
162 0 : static const char* pDisplay = getenv( "DISPLAY" );
163 0 : rReturnedType = AsciiCString;
164 0 : rReturnedBytes = pDisplay ? strlen( pDisplay )+1 : 1;
165 0 : return pDisplay ? (void*)pDisplay : (void*)"";
166 : }
167 :
168 0 : SalFrame *X11SalInstance::CreateFrame( SalFrame *pParent, sal_uLong nSalFrameStyle )
169 : {
170 0 : SalFrame *pFrame = new X11SalFrame( pParent, nSalFrameStyle );
171 :
172 0 : return pFrame;
173 : }
174 :
175 0 : SalFrame* X11SalInstance::CreateChildFrame( SystemParentData* pParentData, sal_uLong nStyle )
176 : {
177 0 : SalFrame* pFrame = new X11SalFrame( NULL, nStyle, pParentData );
178 :
179 0 : return pFrame;
180 : }
181 :
182 0 : void X11SalInstance::DestroyFrame( SalFrame* pFrame )
183 : {
184 0 : delete pFrame;
185 0 : }
186 :
187 0 : static void getServerDirectories( std::list< OString >& o_rFontPaths )
188 : {
189 : #ifdef LINUX
190 : /*
191 : * chkfontpath exists on some (RH derived) Linux distributions
192 : */
193 : static const char* pCommands[] = {
194 : "/usr/sbin/chkfontpath 2>/dev/null", "chkfontpath 2>/dev/null"
195 : };
196 0 : ::std::list< OString > aLines;
197 :
198 0 : for( unsigned int i = 0; i < SAL_N_ELEMENTS(pCommands); i++ )
199 : {
200 0 : FILE* pPipe = popen( pCommands[i], "r" );
201 0 : aLines.clear();
202 0 : if( pPipe )
203 : {
204 : char line[1024];
205 : char* pSearch;
206 0 : while( fgets( line, sizeof(line), pPipe ) )
207 : {
208 0 : int nLen = strlen( line );
209 0 : if( line[nLen-1] == '\n' )
210 0 : line[nLen-1] = 0;
211 0 : pSearch = strstr( line, ": " );
212 0 : if( pSearch )
213 0 : aLines.push_back( pSearch+2 );
214 : }
215 0 : if( ! pclose( pPipe ) )
216 0 : break;
217 : }
218 : }
219 :
220 0 : for( ::std::list< OString >::iterator it = aLines.begin(); it != aLines.end(); ++it )
221 : {
222 0 : if( ! access( it->getStr(), F_OK ) )
223 : {
224 0 : o_rFontPaths.push_back( *it );
225 : #if OSL_DEBUG_LEVEL > 1
226 : fprintf( stderr, "adding fs dir %s\n", it->getStr() );
227 : #endif
228 : }
229 0 : }
230 : #else
231 : (void)o_rFontPaths;
232 : #endif
233 0 : }
234 :
235 0 : void X11SalInstance::FillFontPathList( std::list< OString >& o_rFontPaths )
236 : {
237 0 : Display *pDisplay = GetGenericData()->GetSalDisplay()->GetDisplay();
238 :
239 : DBG_ASSERT( pDisplay, "No Display !" );
240 0 : if( pDisplay )
241 : {
242 : // get font paths to look for fonts
243 0 : int nPaths = 0, i;
244 0 : char** pPaths = XGetFontPath( pDisplay, &nPaths );
245 :
246 0 : bool bServerDirs = false;
247 0 : for( i = 0; i < nPaths; i++ )
248 : {
249 0 : OString aPath( pPaths[i] );
250 0 : sal_Int32 nPos = 0;
251 0 : if( ! bServerDirs
252 0 : && ( nPos = aPath.indexOf( ':' ) ) > 0
253 0 : && ( !aPath.copy(nPos).equals( ":unscaled" ) ) )
254 : {
255 0 : bServerDirs = true;
256 0 : getServerDirectories( o_rFontPaths );
257 : }
258 : else
259 : {
260 0 : psp::normPath( aPath );
261 0 : o_rFontPaths.push_back( aPath );
262 : }
263 0 : }
264 :
265 0 : if( nPaths )
266 0 : XFreeFontPath( pPaths );
267 : }
268 :
269 : // insert some standard directories
270 0 : o_rFontPaths.push_back( "/usr/openwin/lib/X11/fonts/TrueType" );
271 0 : o_rFontPaths.push_back( "/usr/openwin/lib/X11/fonts/Type1" );
272 0 : o_rFontPaths.push_back( "/usr/openwin/lib/X11/fonts/Type1/sun" );
273 0 : o_rFontPaths.push_back( "/usr/X11R6/lib/X11/fonts/truetype" );
274 0 : o_rFontPaths.push_back( "/usr/X11R6/lib/X11/fonts/Type1" );
275 :
276 : #ifdef SOLARIS
277 : /* cde specials, from /usr/dt/bin/Xsession: here are the good fonts,
278 : the OWfontpath file may contain as well multiple lines as a comma
279 : separated list of fonts in each line. to make it even more weird
280 : environment variables are allowed as well */
281 :
282 : const char* lang = getenv("LANG");
283 : if ( lang != NULL )
284 : {
285 : OUString aOpenWinDir( "/usr/openwin/lib/locale/" );
286 : aOpenWinDir += OUString::createFromAscii( lang );
287 : aOpenWinDir += "/OWfontpath";
288 :
289 : SvFileStream aStream( aOpenWinDir, STREAM_READ );
290 :
291 : // TODO: replace environment variables
292 : while( aStream.IsOpen() && ! aStream.IsEof() )
293 : {
294 : OString aLine;
295 : aStream.ReadLine( aLine );
296 : psp::normPath( aLine );
297 : // try to avoid bad fonts in some cases
298 : static bool bAvoid = (strncasecmp( lang, "ar", 2 ) == 0) || (strncasecmp( lang, "he", 2 ) == 0) || strncasecmp( lang, "iw", 2 ) == 0 || (strncasecmp( lang, "hi", 2 ) == 0);
299 : if( bAvoid && aLine.indexOf("iso_8859") != -1 )
300 : continue;
301 : o_rFontPaths.push_back( aLine );
302 : }
303 : }
304 : #endif /* SOLARIS */
305 0 : }
306 :
307 0 : extern "C" { static void SAL_CALL thisModule() {} }
308 :
309 0 : void X11SalInstance::AddToRecentDocumentList(const OUString& rFileUrl, const OUString& rMimeType, const OUString& rDocumentService)
310 : {
311 0 : const OUString SYM_ADD_TO_RECENTLY_USED_FILE_LIST("add_to_recently_used_file_list");
312 0 : const OUString LIB_RECENT_FILE("librecentfile.so");
313 : typedef void (*PFUNC_ADD_TO_RECENTLY_USED_LIST)(const OUString&, const OUString&, const OUString&);
314 :
315 0 : PFUNC_ADD_TO_RECENTLY_USED_LIST add_to_recently_used_file_list = 0;
316 :
317 0 : osl::Module module;
318 0 : module.loadRelative( &thisModule, LIB_RECENT_FILE );
319 0 : if (module.is())
320 0 : add_to_recently_used_file_list = (PFUNC_ADD_TO_RECENTLY_USED_LIST)module.getFunctionSymbol(SYM_ADD_TO_RECENTLY_USED_FILE_LIST);
321 0 : if (add_to_recently_used_file_list)
322 0 : add_to_recently_used_file_list(rFileUrl, rMimeType, rDocumentService);
323 0 : }
324 :
325 0 : void X11SalInstance::PostPrintersChanged()
326 : {
327 0 : SalDisplay* pDisp = GetGenericData()->GetSalDisplay();
328 0 : const std::list< SalFrame* >& rList = pDisp->getFrames();
329 0 : for( std::list< SalFrame* >::const_iterator it = rList.begin();
330 0 : it != rList.end(); ++it )
331 0 : pDisp->SendInternalEvent( *it, NULL, SALEVENT_PRINTERCHANGED );
332 0 : }
333 :
334 0 : GenPspGraphics *X11SalInstance::CreatePrintGraphics()
335 : {
336 0 : return new GenPspGraphics();
337 0 : }
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|