PWLib
1.10.10
|
00001 /* 00002 * pluginmgr.h 00003 * 00004 * Plugin Manager Class Declarations 00005 * 00006 * Portable Windows Library 00007 * 00008 * Contributor(s): Snark at GnomeMeeting 00009 * 00010 * $Log: pluginmgr.h,v $ 00011 * Revision 1.17.2.1 2006/01/27 03:43:24 csoutheren 00012 * Backported changes to CVS head into Phobos 00013 * 00014 * Revision 1.18 2006/01/21 13:43:05 dsandras 00015 * Allow the plugin manager to look for plugins in symlinked directories. 00016 * 00017 * Revision 1.17 2005/08/09 09:08:09 rjongbloed 00018 * Merged new video code from branch back to the trunk. 00019 * 00020 * Revision 1.16.6.1 2005/07/17 09:27:04 rjongbloed 00021 * Major revisions of the PWLib video subsystem including: 00022 * removal of F suffix on colour formats for vertical flipping, all done with existing bool 00023 * working through use of RGB and BGR formats so now consistent 00024 * cleaning up the plug in system to use virtuals instead of pointers to functions. 00025 * rewrite of SDL to be a plug in compatible video output device. 00026 * extensive enhancement of video test program 00027 * 00028 * Revision 1.16 2004/08/05 03:45:35 csoutheren 00029 * Fixed problems with plugin suffix not being propagated to sudirectories 00030 * 00031 * Revision 1.15 2004/06/24 23:10:27 csoutheren 00032 * Require plugins to have _pwplugin suffix 00033 * 00034 * Revision 1.14 2004/06/01 05:44:57 csoutheren 00035 * Added OnShutdown to allow cleanup on exit 00036 * 00037 * Revision 1.13 2004/05/19 06:54:11 csoutheren 00038 * Removed unused code 00039 * 00040 * Revision 1.12 2004/05/18 06:01:06 csoutheren 00041 * Deferred plugin loading until after main has executed by using abstract factory classes 00042 * 00043 * Revision 1.11 2004/05/17 06:05:20 csoutheren 00044 * Changed "make docs" to use doxygen 00045 * Added new config file and main page 00046 * 00047 * Revision 1.10 2004/04/22 11:43:47 csoutheren 00048 * Factored out functions useful for loading dynamic libraries 00049 * 00050 * Revision 1.9 2004/04/22 07:55:30 csoutheren 00051 * Fix problem with generic plugin manager having pure virtual. Thanks to Ben Lear 00052 * 00053 * Revision 1.8 2004/04/14 11:14:10 csoutheren 00054 * Final fix for generic plugin manager 00055 * 00056 * Revision 1.7 2004/04/14 10:57:38 csoutheren 00057 * Removed multiple definition of statc function in generic plugin functions 00058 * 00059 * Revision 1.6 2004/04/14 10:01:54 csoutheren 00060 * Fixed compile problem on Windows 00061 * 00062 * Revision 1.5 2004/04/14 08:12:02 csoutheren 00063 * Added support for generic plugin managers 00064 * 00065 * Revision 1.4 2004/03/23 04:43:42 csoutheren 00066 * Modified plugin manager to allow code modules to be notified when plugins 00067 * are loaded or unloaded 00068 * 00069 * Revision 1.3 2003/11/12 10:24:35 csoutheren 00070 * Changes to allow operation of static plugins under Windows 00071 * 00072 * Revision 1.2 2003/11/12 03:26:17 csoutheren 00073 * Initial version of plugin code from Snark of GnomeMeeting with changes 00074 * by Craig Southeren os Post Increment 00075 * 00076 * 00077 */ 00078 00079 #ifndef _PLUGINMGR_H 00080 #define _PLUGINMGR_H 00081 00082 #define DEFAULT_PLUGINDIR "/usr/lib/pwlib" 00083 00084 #include <ptlib/plugin.h> 00085 00086 template <class C> 00087 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL) 00088 { 00089 PDirectory dir = directory; 00090 if (!dir.Open()) { 00091 PTRACE(4, "Cannot open plugin directory " << dir); 00092 return; 00093 } 00094 PTRACE(4, "Enumerating plugin directory " << dir); 00095 do { 00096 PString entry = dir + dir.GetEntryName(); 00097 PDirectory subdir = entry; 00098 if (subdir.Open()) 00099 PLoadPluginDirectory<C>(obj, entry, suffix); 00100 else { 00101 PFilePath fn(entry); 00102 if ( 00103 (fn.GetType() *= PDynaLink::GetExtension()) && 00104 ( 00105 (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix) 00106 ) 00107 ) 00108 obj.LoadPlugin(entry); 00109 } 00110 } while (dir.Next()); 00111 } 00112 00114 // 00115 // Manager for plugins 00116 // 00117 00118 class PPluginManager : public PObject 00119 { 00120 PCLASSINFO(PPluginManager, PObject); 00121 00122 public: 00123 // functions to load/unload a dynamic plugin 00124 BOOL LoadPlugin (const PString & fileName); 00125 void LoadPluginDirectory (const PDirectory & dir); 00126 00127 // functions to access the plugins' services 00128 PStringList GetPluginTypes() const; 00129 PStringList GetPluginsProviding(const PString & serviceType) const; 00130 PPluginServiceDescriptor * GetServiceDescriptor(const PString & serviceName, const PString & serviceType) const; 00131 PObject * CreatePluginsDevice(const PString & serviceName, const PString & serviceType, int userData = 0) const; 00132 PObject * CreatePluginsDeviceByName(const PString & deviceName, const PString & serviceType, int userData = 0) const; 00133 PStringList GetPluginsDeviceNames(const PString & serviceName, const PString & serviceType, int userData = 0) const; 00134 00135 // function to register a service (used by the plugins themselves) 00136 BOOL RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor); 00137 00138 // Get the list of plugin directories 00139 static PStringArray GetPluginDirs(); 00140 00141 // static functions for accessing global instances of plugin managers 00142 static PPluginManager & GetPluginManager(); 00143 00161 void AddNotifier( 00162 const PNotifier & filterFunction, 00163 BOOL existing = FALSE 00164 ); 00165 00166 void RemoveNotifier( 00167 const PNotifier & filterFunction 00168 ); 00169 00170 protected: 00171 void CallNotifier(PDynaLink & dll, INT code); 00172 00173 PMutex pluginListMutex; 00174 PList<PDynaLink> pluginList; 00175 00176 PMutex serviceListMutex; 00177 PList<PPluginService> serviceList; 00178 00179 PMutex notifierMutex; 00180 PList<PNotifier> notifierList; 00181 }; 00182 00184 // 00185 // Manager for plugin modules 00186 // 00187 00188 class PPluginModuleManager : public PObject 00189 { 00190 public: 00191 typedef PDictionary<PString, PDynaLink> PluginListType; 00192 00193 PPluginModuleManager(const char * _signatureFunctionName, PPluginManager * pluginMgr = NULL); 00194 00195 BOOL LoadPlugin(const PString & fileName) 00196 { if (pluginMgr == NULL) return FALSE; else return pluginMgr->LoadPlugin(fileName); } 00197 00198 void LoadPluginDirectory(const PDirectory &directory) 00199 { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); } 00200 00201 virtual void OnLoadPlugin(PDynaLink & /*dll*/, INT /*code*/) 00202 { } 00203 00204 virtual PluginListType GetPluginList() const 00205 { return pluginList; } 00206 00207 virtual void OnShutdown() 00208 { } 00209 00210 protected: 00211 PluginListType pluginList; 00212 PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule); 00213 00214 protected: 00215 const char * signatureFunctionName; 00216 PPluginManager * pluginMgr; 00217 }; 00218 00219 #endif // ifndef _PLUGINMGR_H