1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: svpelement.cxx,v $
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>
45 #include "svpframe.hxx"
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/ip.h>
56 using namespace basegfx
;
58 class SvpElementContainer
60 std::list
< SvpElement
* > m_aElements
;
64 SvpElementContainer();
65 ~SvpElementContainer();
67 void registerElement( SvpElement
* pElement
) { m_aElements
.push_back(pElement
); }
68 void deregisterElement( SvpElement
* pElement
) { m_aElements
.remove(pElement
); }
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();
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 );
92 if( setsockopt(m_nSocket
, SOL_SOCKET
, SO_REUSEADDR
,
93 (char*)&nOn
, sizeof(nOn
)) )
95 perror( "SvpElementContainer: changing socket options failed" );
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" );
112 if( listen( m_nSocket
, 0 ) )
114 perror( "SvpElementContainer: listen() failed" );
119 m_aThread
= osl_createThread( SvpContainerThread
, this );
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;
142 int nLocalSocket
= accept( m_nSocket
, NULL
, NULL
);
143 if( nLocalSocket
< 0 )
146 perror( "accept() failed" );
150 Application::PostUserEvent( LINK( this, SvpElementContainer
, processRequest
), (void*)nLocalSocket
);
157 static const char* matchType( SvpElement
* pEle
)
159 if( dynamic_cast<SvpSalBitmap
*>(pEle
) )
161 else if( dynamic_cast<SvpSalFrame
*>(pEle
) )
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 );
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();
212 SvpSalBitmap
* pSalBitmap
= new SvpSalBitmap();
213 pSalBitmap
->setBitmap( rDevice
);
214 Bitmap
aBitmap( pSalBitmap
);
215 SvMemoryStream
aStream( 256, 256 );
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
);
226 else if( aCommand
.compareTo( "quit", 4 ) == 0 )
232 write( nFile
, aAnswer
.getStr(), aAnswer
.getLength() );
240 using namespace basebmp
;
242 SvpElement::SvpElement()
244 #if defined WITH_SVP_LISTENING
245 SvpElementContainer::get().registerElement( this );
249 SvpElement::~SvpElement()
251 #if defined WITH_SVP_LISTENING
252 SvpElementContainer::get().deregisterElement( this );
256 sal_uInt32
SvpElement::getBitCountFromScanlineFormat( sal_Int32 nFormat
)
258 sal_uInt32 nBitCount
= 1;
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
:
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
:
273 case Format::EIGHT_BIT_PAL
:
274 case Format::EIGHT_BIT_GREY
:
277 case Format::SIXTEEN_BIT_LSB_TC_MASK
:
278 case Format::SIXTEEN_BIT_MSB_TC_MASK
:
281 case Format::TWENTYFOUR_BIT_TC_MASK
:
284 case Format::THIRTYTWO_BIT_TC_MASK
:
288 DBG_ERROR( "unsupported basebmp format" );