update credits
[LibreOffice.git] / extensions / source / plugin / unx / plugcon.cxx
blob7683825eabadc26227bbc30b74ee814f3e701dc0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #ifdef AIX
31 #define _LINUX_SOURCE_COMPAT
32 #include <sys/timer.h>
33 #undef _LINUX_SOURCE_COMPAT
34 #endif
36 #include <plugin/unx/plugcon.hxx>
38 #include <cstdarg>
39 #include <vector>
41 sal_uInt32 PluginConnector::GetStreamID( NPStream* pStream )
43 size_t nLen = m_aNPWrapStreams.size();
44 for( size_t i = 0; i < nLen; i++ )
45 if( m_aNPWrapStreams[ i ] == pStream )
46 return static_cast<sal_uInt32>(i);
47 SAL_WARN("extensions.plugin", "NPStream has no ID");
48 return UnknownStreamID;
51 sal_uInt32 PluginConnector::GetNPPID( NPP instance )
53 size_t nLen = m_aInstances.size();
54 for( size_t i=0; i <nLen; i++ )
55 if( m_aInstances[ i ]->instance == instance )
56 return static_cast<sal_uInt32>(i);
57 SAL_WARN("extensions.plugin", "NPP has no ID");
59 return UnknownNPPID;
62 struct PtrStruct
64 char* pData;
65 sal_uLong nBytes;
67 PtrStruct( char* i_pData, sal_uLong i_nBytes )
68 : pData( i_pData ), nBytes( i_nBytes ) {}
71 sal_uLong PluginConnector::FillBuffer( char*& rpBuffer,
72 const char* pFunction,
73 sal_uLong nFunctionLen,
74 va_list ap )
76 std::vector< PtrStruct > aList;
77 aList.reserve( 5 );
79 sal_uLong nDataSize = nFunctionLen + sizeof( sal_uLong );
80 char* pNext;
82 do {
83 pNext = va_arg( ap, char* );
84 if( pNext )
86 aList.push_back( PtrStruct( pNext, va_arg( ap, sal_uLong ) ) );
87 nDataSize += aList.back().nBytes + sizeof(sal_uLong);
89 } while( pNext );
91 rpBuffer = new char[ nDataSize ];
92 char* pRun = rpBuffer;
93 memcpy( pRun, &nFunctionLen, sizeof( nFunctionLen ) );
94 pRun += sizeof( nFunctionLen );
95 memcpy( pRun, pFunction, nFunctionLen );
96 pRun += nFunctionLen;
98 for( std::vector<PtrStruct>::const_iterator it = aList.begin(); it != aList.end(); ++it )
100 memcpy( pRun, &it->nBytes, sizeof( sal_uLong ) );
101 pRun += sizeof( sal_uLong );
102 memcpy( pRun, it->pData, it->nBytes );
103 pRun += it->nBytes;
105 return nDataSize;
108 MediatorMessage* PluginConnector::Transact( const char* pFunction,
109 sal_uLong nFunctionLen, ... )
111 va_list ap;
112 char* pBuffer;
114 va_start( ap, nFunctionLen );
115 sal_uLong nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
116 va_end( ap );
117 MediatorMessage* pRet = TransactMessage( nSize, pBuffer );
118 delete[] pBuffer;
119 return pRet;
122 MediatorMessage* PluginConnector::Transact( sal_uInt32 nFunction, ... )
124 va_list ap;
125 char* pBuffer;
127 va_start( ap, nFunction );
128 sal_uLong nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
129 va_end( ap );
130 MediatorMessage* pRet = TransactMessage( nSize, pBuffer );
131 delete[] pBuffer;
132 return pRet;
135 sal_uLong PluginConnector::Send( sal_uInt32 nFunction, ... )
137 va_list ap;
138 char* pBuffer;
140 va_start( ap, nFunction );
141 sal_uLong nSize = FillBuffer( pBuffer, (char*)&nFunction, sizeof( nFunction ), ap );
142 va_end( ap );
143 sal_uLong nRet = SendMessage( nSize, pBuffer );
144 delete[] pBuffer;
145 return nRet;
148 void PluginConnector::Respond( sal_uLong nID,
149 char* pFunction,
150 sal_uLong nFunctionLen, ... )
152 va_list ap;
153 char* pBuffer;
155 va_start( ap, nFunctionLen );
156 sal_uLong nSize = FillBuffer( pBuffer, pFunction, nFunctionLen, ap );
157 va_end( ap );
158 SendMessage( nSize, pBuffer, nID | ( 1 << 24 ) );
159 delete[] pBuffer;
162 MediatorMessage* PluginConnector::WaitForAnswer( sal_uLong nMessageID )
164 if( ! m_bValid )
165 return NULL;
167 nMessageID &= 0x00ffffff;
168 while( m_pListener )
171 osl::MutexGuard aGuard( m_aQueueMutex );
172 for( size_t i = 0; i < m_aMessageQueue.size(); i++ )
174 MediatorMessage* pMessage = m_aMessageQueue[ i ];
175 sal_uLong nID = pMessage->m_nID;
176 if( ( nID & 0xff000000 ) &&
177 ( ( nID & 0x00ffffff ) == nMessageID ) )
179 m_aMessageQueue.erase( m_aMessageQueue.begin() + i );
180 return pMessage;
184 if( ! m_aMessageQueue.empty() )
185 CallWorkHandler();
186 WaitForMessage( 2000 );
188 return NULL;
191 ConnectorInstance::ConnectorInstance( NPP inst, char* type,
192 int args, char* pargnbuf, sal_uLong nargnbytes,
193 char* pargvbuf, sal_uLong nargvbytes,
194 char* savedata, sal_uLong savebytes ) :
195 instance( inst ),
196 pShell( NULL ),
197 pWidget( NULL ),
198 pForm( NULL ),
199 pGtkWindow( NULL ),
200 pGtkWidget( NULL ),
201 bShouldUseXEmbed( false ),
202 nArg( args ),
203 pArgnBuf( pargnbuf ),
204 pArgvBuf( pargvbuf )
206 memset( &window, 0, sizeof(window) );
207 pMimeType = new char[ strlen( type ) + 1 ];
208 strcpy( pMimeType, type );
209 aData.len = savebytes;
210 aData.buf = savedata;
211 argn = new char*[ nArg ];
212 argv = new char*[ nArg ];
213 int i;
214 char* pRun = pArgnBuf;
215 for( i = 0; i < nArg; i++ )
217 argn[i] = pRun;
218 while( *pRun != 0 && (sal_uLong)(pRun - pArgnBuf) < nargnbytes )
219 pRun++;
220 if( (sal_uLong)(pRun - pArgnBuf) < nargnbytes )
221 pRun++;
223 pRun = pArgvBuf;
224 for( i = 0; i < nArg; i++ )
226 argv[i] = pRun;
227 while( *pRun != 0 && (sal_uLong)(pRun - pArgvBuf) < nargvbytes )
228 pRun++;
229 if( (sal_uLong)(pRun - pArgvBuf) < nargvbytes )
230 pRun++;
234 ConnectorInstance::~ConnectorInstance()
236 delete [] pMimeType;
237 delete [] argn;
238 delete [] argv;
239 delete [] pArgnBuf;
240 delete [] pArgvBuf;
241 delete [] (char*)aData.buf;
244 const char* GetCommandName( CommandAtoms eCommand )
246 switch( eCommand )
248 case eNPN_GetURL: return "NPN_GetURL";
249 case eNPN_GetURLNotify: return "NPN_GetURLNotify";
250 case eNPN_DestroyStream: return "NPN_DestroyStream";
251 case eNPN_NewStream: return "NPN_NewStream";
252 case eNPN_PostURLNotify: return "NPN_PostURLNotify";
253 case eNPN_PostURL: return "NPN_PostURL";
254 case eNPN_RequestRead: return "NPN_RequestRead";
255 case eNPN_Status: return "NPN_Status";
256 case eNPN_Version: return "NPN_Version";
257 case eNPN_Write: return "NPN_Write";
258 case eNPN_UserAgent: return "NPN_UserAgent";
260 case eNPP_DestroyStream: return "NPP_DestroyStream";
261 case eNPP_Destroy: return "NPP_Destroy";
262 case eNPP_DestroyPhase2: return "NPP_DestroyPhase2";
263 case eNPP_NewStream: return "NPP_NewStream";
264 case eNPP_New: return "NPP_New";
265 case eNPP_SetWindow: return "NPP_SetWindow";
266 case eNPP_StreamAsFile: return "NPP_StreamAsFile";
267 case eNPP_URLNotify: return "NPP_URLNotify";
268 case eNPP_WriteReady: return "NPP_WriteReady";
269 case eNPP_Write: return "NPP_Write";
270 case eNPP_GetMIMEDescription: return "NPP_GetMIMEDescription";
271 case eNPP_Initialize: return "NPP_Initialize";
272 case eNPP_Shutdown: return "NPP_Shutdown";
274 case eMaxCommand: return "eMaxCommand";
275 default: return "unknown command";
277 return NULL;
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */