merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / headless / svpelement.cxx
blob438de647b1415221a90ebb075bae65f9de7af4eb
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 * $RCSfile: svpelement.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "svpelement.hxx"
33 #include <basebmp/scanlineformats.hxx>
34 #include <tools/debug.hxx>
36 #if defined WITH_SVP_LISTENING
37 #include <osl/thread.h>
38 #include <vcl/svapp.hxx>
39 #include <rtl/strbuf.hxx>
40 #include <vcl/bitmap.hxx>
41 #include <tools/stream.hxx>
43 #include "svpvd.hxx"
44 #include "svpbmp.hxx"
45 #include "svpframe.hxx"
47 #include <list>
48 #include <hash_map>
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/ip.h>
53 #include <stdio.h>
54 #include <errno.h>
56 using namespace basegfx;
58 class SvpElementContainer
60 std::list< SvpElement* > m_aElements;
61 int m_nSocket;
62 oslThread m_aThread;
64 SvpElementContainer();
65 ~SvpElementContainer();
66 public:
67 void registerElement( SvpElement* pElement ) { m_aElements.push_back(pElement); }
68 void deregisterElement( SvpElement* pElement ) { m_aElements.remove(pElement); }
70 void run();
71 DECL_LINK(processRequest,void*);
73 static SvpElementContainer& get();
76 extern "C" void SAL_CALL SvpContainerThread( void* );
78 SvpElementContainer& SvpElementContainer::get()
80 static SvpElementContainer* pInstance = new SvpElementContainer();
81 return *pInstance;
84 SvpElementContainer::SvpElementContainer()
86 static const char* pEnv = getenv("SVP_LISTENER_PORT");
87 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
88 m_nSocket = socket( PF_INET, SOCK_STREAM, 0 );
89 if( m_nSocket >= 0)
91 int nOn = 1;
92 if( setsockopt(m_nSocket, SOL_SOCKET, SO_REUSEADDR,
93 (char*)&nOn, sizeof(nOn)) )
95 perror( "SvpElementContainer: changing socket options failed" );
96 close( m_nSocket );
98 else
100 struct sockaddr_in addr;
101 memset(&addr, 0, sizeof(struct sockaddr_in));
102 addr.sin_family = AF_INET;
103 addr.sin_port = htons(nPort);
104 addr.sin_addr.s_addr = INADDR_ANY;
105 if( bind(m_nSocket,(struct sockaddr*)&addr,sizeof(addr)) )
107 perror( "SvpElementContainer: bind() failed" );
108 close( m_nSocket );
110 else
112 if( listen( m_nSocket, 0 ) )
114 perror( "SvpElementContainer: listen() failed" );
115 close(m_nSocket);
117 else
119 m_aThread = osl_createThread( SvpContainerThread, this );
124 else
125 perror( "SvpElementContainer: socket() failed\n" );
128 SvpElementContainer::~SvpElementContainer()
132 void SAL_CALL SvpContainerThread(void* pSvpContainer)
134 ((SvpElementContainer*)pSvpContainer)->run();
137 void SvpElementContainer::run()
139 bool bRun = m_nSocket != 0;
140 while( bRun )
142 int nLocalSocket = accept( m_nSocket, NULL, NULL );
143 if( nLocalSocket < 0 )
145 bRun = false;
146 perror( "accept() failed" );
148 else
150 Application::PostUserEvent( LINK( this, SvpElementContainer, processRequest ), (void*)nLocalSocket );
153 if( m_nSocket )
154 close( m_nSocket );
157 static const char* matchType( SvpElement* pEle )
159 if( dynamic_cast<SvpSalBitmap*>(pEle) )
160 return "Bitmap";
161 else if( dynamic_cast<SvpSalFrame*>(pEle) )
162 return "Frame";
163 else if( dynamic_cast<SvpSalVirtualDevice*>(pEle) )
164 return "VirtualDevice";
165 return typeid(*pEle).name();
168 IMPL_LINK( SvpElementContainer, processRequest, void*, pSocket )
170 int nFile = (int)pSocket;
172 rtl::OStringBuffer aBuf( 256 ), aAnswer( 256 );
173 char c;
174 while( read( nFile, &c, 1 ) && c != '\n' )
175 aBuf.append( sal_Char(c) );
176 rtl::OString aCommand( aBuf.makeStringAndClear() );
177 if( aCommand.compareTo( "list", 4 ) == 0 )
179 std::hash_map< rtl::OString, std::list<SvpElement*>, rtl::OStringHash > aMap;
180 for( std::list< SvpElement* >::const_iterator it = m_aElements.begin();
181 it != m_aElements.end(); ++it )
183 std::list<SvpElement*>& rList = aMap[matchType(*it)];
184 rList.push_back( *it );
186 for( std::hash_map< rtl::OString, std::list<SvpElement*>, rtl::OStringHash>::const_iterator hash_it = aMap.begin();
187 hash_it != aMap.end(); ++hash_it )
189 aAnswer.append( "ElementType: " );
190 aAnswer.append( hash_it->first );
191 aAnswer.append( '\n' );
192 for( std::list<SvpElement*>::const_iterator it = hash_it->second.begin();
193 it != hash_it->second.end(); ++it )
195 aAnswer.append( sal_Int64(reinterpret_cast<sal_uInt32>(*it)), 16 );
196 aAnswer.append( '\n' );
200 else if( aCommand.compareTo( "get", 3 ) == 0 )
202 sal_IntPtr aId = aCommand.copy( 3 ).toInt64( 16 );
203 SvpElement* pElement = reinterpret_cast<SvpElement*>(aId);
204 for( std::list< SvpElement* >::const_iterator it = m_aElements.begin();
205 it != m_aElements.end(); ++it )
207 if( *it == pElement )
209 const basebmp::BitmapDeviceSharedPtr& rDevice = pElement->getDevice();
210 if( rDevice.get() )
212 SvpSalBitmap* pSalBitmap = new SvpSalBitmap();
213 pSalBitmap->setBitmap( rDevice );
214 Bitmap aBitmap( pSalBitmap );
215 SvMemoryStream aStream( 256, 256 );
216 aStream << aBitmap;
217 aStream.Seek( STREAM_SEEK_TO_END );
218 int nBytes = aStream.Tell();
219 aStream.Seek( STREAM_SEEK_TO_BEGIN );
220 aAnswer.append( (const sal_Char*)aStream.GetData(), nBytes );
222 break;
226 else if( aCommand.compareTo( "quit", 4 ) == 0 )
228 Application::Quit();
229 close( m_nSocket );
230 m_nSocket = 0;
232 write( nFile, aAnswer.getStr(), aAnswer.getLength() );
233 close( nFile );
235 return 0;
238 #endif
240 using namespace basebmp;
242 SvpElement::SvpElement()
244 #if defined WITH_SVP_LISTENING
245 SvpElementContainer::get().registerElement( this );
246 #endif
249 SvpElement::~SvpElement()
251 #if defined WITH_SVP_LISTENING
252 SvpElementContainer::get().deregisterElement( this );
253 #endif
256 sal_uInt32 SvpElement::getBitCountFromScanlineFormat( sal_Int32 nFormat )
258 sal_uInt32 nBitCount = 1;
259 switch( nFormat )
261 case Format::ONE_BIT_MSB_GREY:
262 case Format::ONE_BIT_LSB_GREY:
263 case Format::ONE_BIT_MSB_PAL:
264 case Format::ONE_BIT_LSB_PAL:
265 nBitCount = 1;
266 break;
267 case Format::FOUR_BIT_MSB_GREY:
268 case Format::FOUR_BIT_LSB_GREY:
269 case Format::FOUR_BIT_MSB_PAL:
270 case Format::FOUR_BIT_LSB_PAL:
271 nBitCount = 4;
272 break;
273 case Format::EIGHT_BIT_PAL:
274 case Format::EIGHT_BIT_GREY:
275 nBitCount = 8;
276 break;
277 case Format::SIXTEEN_BIT_LSB_TC_MASK:
278 case Format::SIXTEEN_BIT_MSB_TC_MASK:
279 nBitCount = 16;
280 break;
281 case Format::TWENTYFOUR_BIT_TC_MASK:
282 nBitCount = 24;
283 break;
284 case Format::THIRTYTWO_BIT_TC_MASK:
285 nBitCount = 32;
286 break;
287 default:
288 DBG_ERROR( "unsupported basebmp format" );
289 break;
291 return nBitCount;