fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / vcl / workben / svpclient.cxx
blobd5b19646023629a553e7ee19d9d314fd29891f9a
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 <sal/main.h>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/awt/ImageScaleMode.hpp>
24 #include <vcl/event.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/wrkwin.hxx>
27 #include <vcl/button.hxx>
28 #include <vcl/lstbox.hxx>
29 #include <vcl/imgctrl.hxx>
30 #include <vcl/bitmapex.hxx>
31 #include <tools/extendapplicationenvironment.hxx>
32 #include <tools/stream.hxx>
34 #include <rtl/strbuf.hxx>
35 #include <rtl/ustrbuf.hxx>
37 #include <math.h>
39 #include <comphelper/processfactory.hxx>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <cppuhelper/bootstrap.hxx>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 using namespace cppu;
52 using namespace comphelper;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
57 // Forward declaration
58 void Main();
60 SAL_IMPLEMENT_MAIN()
62 tools::extendApplicationEnvironment();
64 // create the global service-manager
65 Reference< XMultiServiceFactory > xFactory;
66 try
68 Reference< XComponentContext > xCtx = defaultBootstrap_InitialComponentContext();
69 xFactory = Reference< XMultiServiceFactory >( xCtx->getServiceManager(), UNO_QUERY );
70 if( xFactory.is() )
71 setProcessServiceFactory( xFactory );
73 catch(const com::sun::star::uno::Exception&)
77 if( ! xFactory.is() )
79 fprintf( stderr, "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
80 exit( 1 );
83 InitVCL();
84 ::Main();
85 DeInitVCL();
87 return 0;
90 class MyWin : public WorkWindow
92 PushButton m_aListButton;
93 ListBox m_aSvpBitmaps;
94 ImageControl m_aImage;
95 PushButton m_aQuitButton;
96 public:
97 MyWin( Window* pParent, WinBits nWinStyle );
99 void MouseMove( const MouseEvent& rMEvt );
100 void MouseButtonDown( const MouseEvent& rMEvt );
101 void MouseButtonUp( const MouseEvent& rMEvt );
102 void KeyInput( const KeyEvent& rKEvt );
103 void KeyUp( const KeyEvent& rKEvt );
104 void Paint( const Rectangle& rRect );
105 void Resize();
107 sal_Bool Close();
109 void parseList( const OString& rList );
110 OString processCommand( const OString& rCommand );
112 DECL_LINK( ListHdl, Button* );
113 DECL_LINK( SelectHdl, ListBox* );
114 DECL_LINK( QuitHdl, Button* );
117 void Main()
119 MyWin aMainWin( NULL, WB_STDWORK );
120 aMainWin.SetText( OUString( "SvpClient" ) );
121 aMainWin.Show();
123 Application::Execute();
126 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
127 WorkWindow( pParent, nWinStyle ),
128 m_aListButton( this, 0 ),
129 m_aSvpBitmaps( this, WB_BORDER ),
130 m_aImage( this, WB_BORDER ),
131 m_aQuitButton( this, 0 )
133 m_aListButton.SetPosSizePixel( Point( 10, 10 ), Size( 120, 25 ) );
134 m_aListButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "List Elements" ) ) );
135 m_aListButton.SetClickHdl( LINK( this, MyWin, ListHdl ) );
136 m_aListButton.Show();
138 m_aSvpBitmaps.SetPosSizePixel( Point( 10, 40 ), Size( 150, 150 ) );
139 m_aSvpBitmaps.SetSelectHdl( LINK( this, MyWin, SelectHdl ) );
140 m_aSvpBitmaps.Show();
142 m_aImage.SetPosSizePixel( Point( 170, 10 ), Size( 400, 400 ) );
143 m_aImage.SetScaleMode( com::sun::star::awt::ImageScaleMode::None );
144 m_aImage.Show();
146 m_aQuitButton.SetPosSizePixel( Point( 10, 300 ), Size( 120,25 ) );
147 m_aQuitButton.SetText( String( RTL_CONSTASCII_USTRINGPARAM( "Quit SVP server" ) ) );
148 m_aQuitButton.SetClickHdl( LINK( this, MyWin, QuitHdl ) );
149 m_aQuitButton.Show();
152 sal_Bool MyWin::Close()
154 sal_Bool bRet = WorkWindow::Close();
155 if( bRet )
156 Application::Quit();
157 return bRet;
160 void MyWin::parseList( const OString& rList )
162 sal_Int32 nTokenPos = 0;
163 OUString aElementType;
164 m_aSvpBitmaps.Clear();
165 while( nTokenPos >= 0 )
167 OString aLine = rList.getToken( 0, '\n', nTokenPos );
168 if( ! aLine.getLength() || *aLine.getStr() == '#' )
169 continue;
171 if( aLine.startsWith( "ElementType: " ) )
172 aElementType = OStringToOUString( aLine.copy( 13 ), RTL_TEXTENCODING_ASCII_US );
173 else
175 OUStringBuffer aNewElement( 64 );
176 aNewElement.append( aElementType );
177 aNewElement.appendAscii( ": " );
178 aNewElement.append( OStringToOUString( aLine, RTL_TEXTENCODING_ASCII_US ) );
179 m_aSvpBitmaps.InsertEntry( aNewElement.makeStringAndClear() );
184 OString MyWin::processCommand( const OString& rCommand )
186 static const char* pEnv = getenv("SVP_LISTENER_PORT");
187 OStringBuffer aAnswer;
188 int nPort = (pEnv && *pEnv) ? atoi(pEnv) : 8000;
189 int nSocket = socket( PF_INET, SOCK_STREAM, 0 );
190 if( nSocket >= 0)
192 struct sockaddr_in addr;
193 memset(&addr, 0, sizeof(struct sockaddr_in));
194 addr.sin_family = AF_INET;
195 addr.sin_port = htons(nPort);
196 addr.sin_addr.s_addr = INADDR_ANY;
197 if( connect( nSocket, (const sockaddr*)&addr, sizeof(addr) ) )
199 perror( "SvpElementContainer: connect() failed" );
200 close(nSocket);
202 else
204 write( nSocket, rCommand.getStr(), rCommand.getLength() );
205 write( nSocket, "\n", 1 );
206 char buf[256];
207 ssize_t nBytes = 0;
210 nBytes = read( nSocket, buf, sizeof(buf) );
211 aAnswer.append( buf, nBytes );
212 } while( nBytes == sizeof( buf ) );
215 else
216 perror( "SvpElementContainer: socket() failed\n" );
217 return aAnswer.makeStringAndClear();
220 IMPL_LINK( MyWin, ListHdl, Button*, )
222 parseList( processCommand( "list" ) );
223 return 0;
226 IMPL_LINK( MyWin, QuitHdl, Button*, )
228 processCommand( "quit" );
229 return 0;
232 IMPL_LINK( MyWin, SelectHdl, ListBox*, )
234 String aEntry = m_aSvpBitmaps.GetSelectEntry();
235 sal_uInt16 nPos = aEntry.SearchAscii( ": " );
236 if( nPos != STRING_NOTFOUND )
238 OStringBuffer aCommand( 64 );
239 aCommand.append( "get " );
240 aCommand.append( OUStringToOString( aEntry.Copy( nPos+2 ), RTL_TEXTENCODING_ASCII_US ) );
241 OString aAnswer( processCommand( aCommand.makeStringAndClear() ) );
242 SvMemoryStream aStream( aAnswer.getLength() );
243 aStream.Write( aAnswer.getStr(), aAnswer.getLength() );
244 aStream.Seek( STREAM_SEEK_TO_BEGIN );
245 Bitmap aBitmap;
246 aStream >> aBitmap;
247 fprintf( stderr, "got bitmap of size %ldx%ld\n",
248 sal::static_int_cast< long >(aBitmap.GetSizePixel().Width()),
249 sal::static_int_cast< long >(aBitmap.GetSizePixel().Height()));
250 Size aFixedSize( aBitmap.GetSizePixel() );
251 aFixedSize.Width() += 10;
252 aFixedSize.Height() += 10;
253 m_aImage.SetSizePixel( aFixedSize );
254 m_aImage.SetImage( Image( BitmapEx( aBitmap ) ) );
256 return 0;
260 void MyWin::MouseMove( const MouseEvent& rMEvt )
262 WorkWindow::MouseMove( rMEvt );
266 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
268 WorkWindow::MouseButtonDown( rMEvt );
272 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
274 WorkWindow::MouseButtonUp( rMEvt );
278 void MyWin::KeyInput( const KeyEvent& rKEvt )
280 WorkWindow::KeyInput( rKEvt );
284 void MyWin::KeyUp( const KeyEvent& rKEvt )
286 WorkWindow::KeyUp( rKEvt );
290 void MyWin::Paint( const Rectangle& rRect )
292 WorkWindow::Paint( rRect );
296 void MyWin::Resize()
298 WorkWindow::Resize();
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */