Update ooo320-m1
[ooovba.git] / extensions / source / plugin / unx / sysplug.cxx
blobc82fe1b0b63262e105409b6c10bed7885690ada1
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: sysplug.cxx,v $
10 * $Revision: 1.10 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include <cstdarg>
35 #include <sys/types.h>
36 #include <signal.h>
37 #include <sys/wait.h>
38 #include <osl/file.hxx>
39 #include <osl/thread.h>
40 #include <rtl/bootstrap.hxx>
42 #include <plugin/impl.hxx>
44 int UnxPluginComm::nConnCounter = 0;
46 UnxPluginComm::UnxPluginComm(
47 const String& /*mimetype*/,
48 const String& library,
49 XLIB_Window aParent,
50 int nDescriptor1,
51 int nDescriptor2
52 ) :
53 PluginComm( ::rtl::OUStringToOString( library, osl_getThreadTextEncoding() ), false ),
54 PluginConnector( nDescriptor2 )
56 char pDesc[32];
57 char pWindow[32];
58 sprintf( pWindow, "%d", (int)aParent );
59 sprintf( pDesc, "%d", nDescriptor1 );
60 ByteString aLib( library, osl_getThreadTextEncoding() );
61 rtl::OString path;
62 if (!getPluginappPath(&path)) {
63 fprintf( stderr, "cannot construct path to pluginapp.bin\n" );
64 m_nCommPID = -1;
65 return;
68 char const* pArgs[5];
69 pArgs[0] = path.getStr();
70 pArgs[1] = pDesc;
71 pArgs[2] = aLib.GetBuffer();
72 pArgs[3] = pWindow;
73 pArgs[4] = NULL;
75 #if OSL_DEBUG_LEVEL > 1
76 m_nCommPID = 10;
77 fprintf( stderr, "Try to launch: %s %s %s %s, descriptors are %d, %d\n", pArgs[0], pArgs[1], pArgs[2], pArgs[3], nDescriptor1, nDescriptor2 );
78 #endif
80 if( ! ( m_nCommPID = fork() ) )
82 execvp( pArgs[0], const_cast< char ** >(pArgs) );
83 fprintf( stderr, "Error: could not exec %s\n", pArgs[0] );
84 _exit(255);
87 if( m_nCommPID != -1 )
89 // wait for pluginapp.bin to start up
90 if( ! WaitForMessage( 5000 ) )
92 fprintf( stderr, "Timeout on command: %s %s %s %s\n", pArgs[0], pArgs[1], pArgs[2], pArgs[3] );
93 invalidate();
95 else
97 MediatorMessage* pMessage = GetNextMessage( TRUE );
98 Respond( pMessage->m_nID,
99 const_cast<char*>("init ack"),8,
100 NULL );
101 delete pMessage;
102 NPP_Initialize();
107 UnxPluginComm::~UnxPluginComm()
109 NPP_Shutdown();
110 if( m_nCommPID != -1 && m_nCommPID != 0 )
112 int status = 16777216;
113 pid_t nExit = waitpid( m_nCommPID, &status, WUNTRACED );
114 #if OSL_DEBUG_LEVEL > 1
115 fprintf( stderr, "child %d (plugin app child %d) exited with status %d\n", nExit, m_nCommPID, WEXITSTATUS(status) );
116 #else
117 (void)nExit;
118 #endif
122 bool UnxPluginComm::getPluginappPath(rtl::OString * path) {
123 OSL_ASSERT(path != NULL);
124 rtl::OUString p(
125 RTL_CONSTASCII_USTRINGPARAM("$OOO_BASE_DIR/program/pluginapp.bin"));
126 rtl::Bootstrap::expandMacros(p);
127 return
128 (osl::FileBase::getSystemPathFromFileURL(p, p) ==
129 osl::FileBase::E_None) &&
130 p.convertToString(
131 path, osl_getThreadTextEncoding(),
132 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
133 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR));