4 * Copyright 2009 Free Software Foundation, Inc.
\r
5 * ------------------------------------------------------------------------
\r
7 * GNU XBoard is free software: you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation, either version 3 of the License, or (at
\r
10 * your option) any later version.
\r
12 * GNU XBoard is distributed in the hope that it will be useful, but
\r
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
15 * General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
20 *------------------------------------------------------------------------
\r
21 ** See the file ChangeLog for a revision history. */
\r
23 #include "wplugin.h"
\r
25 static char * makePluginExeName( const char * name )
\r
27 char buf[ MAX_PATH ];
\r
31 strcat( buf, "plugins\\" );
\r
32 strcat( buf, name );
\r
33 strcat( buf, ".exe" );
\r
35 return strdup( buf );
\r
38 WbPlugin * wbpCreate( const char * name )
\r
43 // Create the plugin
\r
44 WbPlugin * plugin = (WbPlugin *) malloc( sizeof(WbPlugin) );
\r
46 memset( plugin, 0, sizeof(WbPlugin) );
\r
48 plugin->name_ = strdup( name );
\r
49 plugin->exe_name_ = makePluginExeName( name );
\r
50 plugin->hPipe_ = INVALID_HANDLE_VALUE;
\r
51 plugin->hProcess_ = INVALID_HANDLE_VALUE;
\r
53 // Create the named pipe for plugin communication
\r
55 strcpy( buf, "\\\\.\\pipe\\" );
\r
56 strcat( buf, name );
\r
58 plugin->hPipe_ = CreateNamedPipe( buf,
\r
64 1000, // Default timeout
\r
67 if( plugin->hPipe_ == INVALID_HANDLE_VALUE ) {
\r
68 DWORD err = GetLastError();
\r
73 // Create the plugin process
\r
76 PROCESS_INFORMATION pi;
\r
78 ZeroMemory( &si, sizeof(si) );
\r
79 ZeroMemory( &pi, sizeof(pi) );
\r
83 strcpy( buf, "\"" );
\r
84 strcat( buf, plugin->exe_name_ );
\r
85 strcat( buf, "\"" );
\r
87 strcpy( buf, "\"C:\\Program Files\\Borland\\Delphi5\\Projects\\History\\History.exe\"" );
\r
89 if( CreateProcess( NULL,
\r
93 FALSE, // Inherit handles
\r
94 0, // Creation flags
\r
95 NULL, // Environment
\r
96 NULL, // Current directory
\r
100 CloseHandle( pi.hThread );
\r
101 plugin->hProcess_ = pi.hProcess;
\r
108 // Destroy the plugin instance if something went wrong
\r
109 if( result != 0 ) {
\r
110 wbpDelete( plugin );
\r
117 void wbpDelete( WbPlugin * plugin )
\r
119 if( plugin != 0 ) {
\r
120 if( plugin->hPipe_ != INVALID_HANDLE_VALUE ) {
\r
121 CloseHandle( plugin->hPipe_ );
\r
124 if( plugin->hProcess_ != INVALID_HANDLE_VALUE ) {
\r
125 CloseHandle( plugin->hProcess_ );
\r
128 free( plugin->name_ );
\r
130 free( plugin->exe_name_ );
\r
133 plugin->exe_name_ = 0;
\r
134 plugin->hPipe_ = INVALID_HANDLE_VALUE;
\r
135 plugin->hProcess_ = INVALID_HANDLE_VALUE;
\r
141 int wbpSendMessage( WbPlugin * plugin, const char * msg, size_t msg_len )
\r
145 if( plugin != 0 && plugin->hPipe_ != INVALID_HANDLE_VALUE ) {
\r
149 while( ok && (msg_len > 0) ) {
\r
152 ok = WriteFile( plugin->hPipe_,
\r
159 if( cb > msg_len ) break; // Should *never* happen!
\r
167 if( zf >= 3 ) ok = FALSE;
\r
182 int wbpListInit( WbPluginList * list )
\r
184 list->item_count_ = 0;
\r
189 int wbpListAdd( WbPluginList * list, WbPlugin * plugin )
\r
193 if( plugin != 0 ) {
\r
194 if( list->item_count_ < MaxWbPlugins ) {
\r
195 list->item_[ list->item_count_ ] = plugin;
\r
196 list->item_count_++;
\r
205 WbPlugin * wbpListGet( WbPluginList * list, int index )
\r
207 WbPlugin * result = 0;
\r
209 if( index >= 0 && index < list->item_count_ ) {
\r
210 result = list->item_[ index ];
\r
216 int wbpListGetCount( WbPluginList * list )
\r
218 return list->item_count_;
\r
221 int wbpListDeleteAll( WbPluginList * list )
\r
225 for( i=0; i<list->item_count_; i++ ) {
\r
226 wbpDelete( list->item_[i] );
\r
229 return wbpListInit( list );
\r
232 int wbpListBroadcastMessage( WbPluginList * list, const char * msg, size_t msg_len )
\r
237 for( i=0; i<list->item_count_; i++ ) {
\r
238 if( wbpSendMessage( list->item_[i], msg, msg_len ) == 0 ) {
\r
242 // Error sending message to plugin...
\r