Audacity  2.2.2
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ModuleManager Class Referencefinal

#include <ModuleManager.h>

Inheritance diagram for ModuleManager:
ModuleManagerInterface

Public Member Functions

void RegisterModule (ModuleInterface *module) override
 
void Initialize (CommandHandler &cmdHandler)
 
int Dispatch (ModuleDispatchTypes type)
 
bool DiscoverProviders ()
 
void FindAllPlugins (PluginIDList &providers, wxArrayString &paths)
 
wxArrayString FindPluginsForProvider (const PluginID &provider, const wxString &path)
 
bool RegisterEffectPlugin (const PluginID &provider, const wxString &path, wxString &errMsg)
 
IdentInterfaceCreateProviderInstance (const PluginID &provider, const wxString &path)
 
IdentInterfaceCreateInstance (const PluginID &provider, const wxString &path)
 
void DeleteInstance (const PluginID &provider, IdentInterface *instance)
 
bool IsProviderValid (const PluginID &provider, const wxString &path)
 
bool IsPluginValid (const PluginID &provider, const wxString &path, bool bFast)
 

Static Public Member Functions

static ModuleManagerGet ()
 

Private Member Functions

 ModuleManager ()
 
 ~ModuleManager ()
 
void InitializeBuiltins ()
 
ModuleInterfaceLoadModule (const wxString &path)
 

Private Attributes

friend ModuleInterfaceDeleter
 
ModuleMainMap mModuleMains
 
ModuleMap mDynModules
 
LibraryMap mLibs
 
std::vector< std::unique_ptr
< Module > > 
mModules
 

Static Private Attributes

static std::unique_ptr
< ModuleManager
mInstance {}
 

Detailed Description

Definition at line 77 of file ModuleManager.h.

Constructor & Destructor Documentation

ModuleManager::ModuleManager ( )
private

Definition at line 202 of file ModuleManager.cpp.

203 {
204 }
ModuleManager::~ModuleManager ( )
private

Definition at line 206 of file ModuleManager.cpp.

References mDynModules.

207 {
208  mDynModules.clear();
209  builtinModuleList().clear();
210 }
ModuleMap mDynModules

Member Function Documentation

IdentInterface * ModuleManager::CreateInstance ( const PluginID provider,
const wxString &  path 
)

Definition at line 566 of file ModuleManager.cpp.

References mDynModules.

Referenced by PluginDescriptor::GetInstance().

568 {
569  if (mDynModules.find(providerID) == mDynModules.end())
570  {
571  return NULL;
572  }
573 
574  return mDynModules[providerID]->CreateInstance(path);
575 }
ModuleMap mDynModules
IdentInterface * ModuleManager::CreateProviderInstance ( const PluginID provider,
const wxString &  path 
)

Definition at line 555 of file ModuleManager.cpp.

References LoadModule(), and mDynModules.

Referenced by FindAllPlugins(), FindPluginsForProvider(), and PluginDescriptor::GetInstance().

557 {
558  if (path.IsEmpty() && mDynModules.find(providerID) != mDynModules.end())
559  {
560  return mDynModules[providerID].get();
561  }
562 
563  return LoadModule(path);
564 }
ModuleInterface * LoadModule(const wxString &path)
ModuleMap mDynModules
void ModuleManager::DeleteInstance ( const PluginID provider,
IdentInterface instance 
)

Definition at line 577 of file ModuleManager.cpp.

References mDynModules.

Referenced by PluginDescriptor::DeleteInstance().

579 {
580  if (mDynModules.find(providerID) == mDynModules.end())
581  {
582  return;
583  }
584 
585  mDynModules[providerID]->DeleteInstance(instance);
586 }
ModuleMap mDynModules
bool ModuleManager::DiscoverProviders ( )

Definition at line 348 of file ModuleManager.cpp.

References AudacityApp::AddMultiPathsToPathList(), AudacityApp::AddUniquePathToPathList(), ModuleInterface::AutoRegisterPlugins(), AudacityApp::FindFilesInPathList(), PluginManager::Get(), InitializeBuiltins(), LoadModule(), FileNames::ModulesDir(), PluginManager::RegisterPlugin(), and wxGetApp().

Referenced by PluginManager::Initialize().

349 {
351 
352  wxArrayString provList;
353  wxArrayString pathList;
354 
355  // Code from LoadLadspa that might be useful in load modules.
356  wxString pathVar = wxString::FromUTF8(getenv("AUDACITY_MODULES_PATH"));
357 
358  if (pathVar != wxT(""))
359  {
360  wxGetApp().AddMultiPathsToPathList(pathVar, pathList);
361  }
362  else
363  {
365  }
366 
367 #if defined(__WXMSW__)
368  wxGetApp().FindFilesInPathList(wxT("*.dll"), pathList, provList);
369 #elif defined(__WXMAC__)
370  wxGetApp().FindFilesInPathList(wxT("*.dylib"), pathList, provList);
371 #else
372  wxGetApp().FindFilesInPathList(wxT("*.so"), pathList, provList);
373 #endif
374 
376 
377  for (int i = 0, cnt = provList.GetCount(); i < cnt; i++)
378  {
379  ModuleInterface *module = LoadModule(provList[i]);
380  if (module)
381  {
382  // Register the provider
383  pm.RegisterPlugin(module);
384 
385  // Now, allow the module to auto-register children
386  module->AutoRegisterPlugins(pm);
387  }
388  }
389 
390  return true;
391 }
virtual bool AutoRegisterPlugins(PluginManagerInterface &pluginManager)=0
static void AddMultiPathsToPathList(const wxString &multiPathString, wxArrayString &pathList)
static void FindFilesInPathList(const wxString &pattern, const wxArrayString &pathList, wxArrayString &results, int flags=wxDIR_FILES)
ModuleInterface * LoadModule(const wxString &path)
void InitializeBuiltins()
PluginManager maintains a list of all plug ins. That covers modules, effects, generators, analysis-effects, commands. It also has functions for shared and private configs - which need to move out.
static void AddUniquePathToPathList(const wxString &path, wxArrayString &pathList)
const PluginID & RegisterPlugin(ModuleInterface *module) override
static wxString ModulesDir()
Definition: FileNames.cpp:253
AudacityApp & wxGetApp()
static PluginManager & Get()
int ModuleManager::Dispatch ( ModuleDispatchTypes  type)

Definition at line 324 of file ModuleManager.cpp.

References mModules.

Referenced by CreateNewAudacityProject(), AudacityProject::OnCloseWindow(), AudacityApp::OnInit(), QuitAudacity(), and AudacityProject::RebuildMenuBar().

325 {
326  for (const auto &module: mModules) {
327  module->Dispatch(type);
328  }
329  return 0;
330 }
std::vector< std::unique_ptr< Module > > mModules
void ModuleManager::FindAllPlugins ( PluginIDList providers,
wxArrayString &  paths 
)

Definition at line 493 of file ModuleManager.cpp.

References CreateProviderInstance(), ModuleInterface::FindPluginPaths(), PluginManager::Get(), PluginManager::GetFirstPlugin(), PluginDescriptor::GetID(), PluginManager::GetNextPlugin(), PluginDescriptor::GetPath(), and PluginTypeModule.

494 {
496 
497  wxArrayString modIDs;
498  wxArrayString modPaths;
500  while (plug)
501  {
502  modIDs.push_back(plug->GetID());
503  modPaths.push_back(plug->GetPath());
504  plug = pm.GetNextPlugin(PluginTypeModule);
505  }
506 
507  for (size_t i = 0, cnt = modIDs.size(); i < cnt; i++)
508  {
509  PluginID providerID = modIDs[i];
510 
511  ModuleInterface *module =
512  static_cast<ModuleInterface *>(CreateProviderInstance(providerID, modPaths[i]));
513 
514  if (!module)
515  continue;
516 
517  wxArrayString newpaths = module->FindPluginPaths(pm);
518  for (size_t i = 0, cnt = newpaths.size(); i < cnt; i++)
519  {
520  providers.push_back(providerID);
521  paths.push_back(newpaths[i]);
522  }
523  }
524 }
const PluginDescriptor * GetFirstPlugin(int type)
wxString PluginID
Definition: Types.h:209
IdentInterface * CreateProviderInstance(const PluginID &provider, const wxString &path)
const PluginDescriptor * GetNextPlugin(int type)
const wxString & GetPath() const
virtual wxArrayString FindPluginPaths(PluginManagerInterface &pluginManager)=0
PluginManager maintains a list of all plug ins. That covers modules, effects, generators, analysis-effects, commands. It also has functions for shared and private configs - which need to move out.
const wxString & GetID() const
static PluginManager & Get()
wxArrayString ModuleManager::FindPluginsForProvider ( const PluginID provider,
const wxString &  path 
)

Definition at line 526 of file ModuleManager.cpp.

References CreateProviderInstance(), PluginManager::Get(), and mDynModules.

Referenced by PluginManager::CheckForUpdates().

528 {
529  // Instantiate if it hasn't already been done
530  if (mDynModules.find(providerID) == mDynModules.end())
531  {
532  // If it couldn't be created, just give up and return an empty list
533  if (!CreateProviderInstance(providerID, path))
534  {
535  return wxArrayString();
536  }
537  }
538 
539  return mDynModules[providerID]->FindPluginPaths(PluginManager::Get());
540 }
IdentInterface * CreateProviderInstance(const PluginID &provider, const wxString &path)
ModuleMap mDynModules
static PluginManager & Get()
ModuleManager & ModuleManager::Get ( )
static
void ModuleManager::Initialize ( CommandHandler cmdHandler)

Definition at line 213 of file ModuleManager.cpp.

References _(), AudacityApp::AddMultiPathsToPathList(), AudacityApp::AddUniquePathToPathList(), AudacityApp::audacityPathList, AudacityApp::FindFilesInPathList(), Get(), ModulePrefs::GetModuleStatus(), kModuleAsk, kModuleDisabled, kModuleEnabled, kModuleFailed, kModuleNew, mainPanelFnName, mModules, pPanelHijack, ScriptCommandRelay::Run(), scriptFn, scriptFnName, ScriptCommandRelay::SetCommandHandler(), ModulePrefs::SetModuleStatus(), ScriptCommandRelay::SetRegScriptServerFunc(), ShowMultiDialog(), and wxGetApp().

Referenced by AudacityApp::OnInit().

214 {
215  wxArrayString audacityPathList = wxGetApp().audacityPathList;
216  wxArrayString pathList;
217  wxArrayString files;
218  wxString pathVar;
219  size_t i;
220 
221  // Code from LoadLadspa that might be useful in load modules.
222  pathVar = wxGetenv(wxT("AUDACITY_MODULES_PATH"));
223  if (pathVar != wxT(""))
224  wxGetApp().AddMultiPathsToPathList(pathVar, pathList);
225 
226  for (i = 0; i < audacityPathList.GetCount(); i++) {
227  wxString prefix = audacityPathList[i] + wxFILE_SEP_PATH;
228  wxGetApp().AddUniquePathToPathList(prefix + wxT("modules"),
229  pathList);
230  }
231 
232  #if defined(__WXMSW__)
233  wxGetApp().FindFilesInPathList(wxT("*.dll"), pathList, files);
234  #else
235  wxGetApp().FindFilesInPathList(wxT("*.so"), pathList, files);
236  #endif
237 
238  wxString saveOldCWD = ::wxGetCwd();
239  for (i = 0; i < files.GetCount(); i++) {
240  // As a courtesy to some modules that might be bridges to
241  // open other modules, we set the current working
242  // directory to be the module's directory.
243  wxString prefix = ::wxPathOnly(files[i]);
244  ::wxSetWorkingDirectory(prefix);
245 
246 #ifdef EXPERIMENTAL_MODULE_PREFS
247  int iModuleStatus = ModulePrefs::GetModuleStatus( files[i] );
248  if( iModuleStatus == kModuleDisabled )
249  continue;
250  if( iModuleStatus == kModuleFailed )
251  continue;
252  // New module? You have to go and explicitly enable it.
253  if( iModuleStatus == kModuleNew ){
254  // To ensure it is noted in config file and so
255  // appears on modules page.
257  continue;
258  }
259 
260  if( iModuleStatus == kModuleAsk )
261 #endif
262  // JKC: I don't like prompting for the plug-ins individually
263  // I think it would be better to show the module prefs page,
264  // and let the user decide for each one.
265  {
266  wxString ShortName = wxFileName( files[i] ).GetName();
267  wxString msg;
268  msg.Printf(_("Module \"%s\" found."), ShortName);
269  msg += _("\n\nOnly use modules from trusted sources");
270  const wxChar *buttons[] = {_("Yes"), _("No"), NULL}; // could add a button here for 'yes and remember that', and put it into the cfg file. Needs more thought.
271  int action;
272  action = ShowMultiDialog(msg, _("Audacity Module Loader"), buttons, _("Try and load this module?"), false);
273 #ifdef EXPERIMENTAL_MODULE_PREFS
274  // If we're not prompting always, accept the answer permanantly
275  if( iModuleStatus == kModuleNew ){
276  iModuleStatus = (action==1)?kModuleDisabled : kModuleEnabled;
277  ModulePrefs::SetModuleStatus( files[i], iModuleStatus );
278  }
279 #endif
280  if(action == 1){ // "No"
281  continue;
282  }
283  }
284 #ifdef EXPERIMENTAL_MODULE_PREFS
285  // Before attempting to load, we set the state to bad.
286  // That way, if we crash, we won't try again.
288 #endif
289 
290  auto umodule = std::make_unique<Module>(files[i]);
291  if (umodule->Load()) // it will get rejected if there are version problems
292  {
293  auto module = umodule.get();
294  Get().mModules.push_back(std::move(umodule));
295  // We've loaded and initialised OK.
296  // So look for special case functions:
297  wxLogNull logNo; // Don't show wxWidgets errors if we can't do these. (Was: Fix bug 544.)
298  // (a) for scripting.
299  if( scriptFn == NULL )
300  scriptFn = (tpRegScriptServerFunc)(module->GetSymbol(wxT(scriptFnName)));
301  // (b) for hijacking the entire Audacity panel.
302  if( pPanelHijack==NULL )
303  {
304  pPanelHijack = (tPanelFn)(module->GetSymbol(wxT(mainPanelFnName)));
305  }
306 #ifdef EXPERIMENTAL_MODULE_PREFS
307  // Loaded successfully, restore the status.
308  ModulePrefs::SetModuleStatus( files[i], iModuleStatus);
309 #endif
310  }
311  }
312  ::wxSetWorkingDirectory(saveOldCWD);
313 
314  // After loading all the modules, we may have a registered scripting function.
315  if(scriptFn)
316  {
319  NonGuiThread::StartChild(&ScriptCommandRelay::Run);
320  }
321 }
pwxWindow(* tPanelFn)(int)
static ModuleManager & Get()
static tpRegScriptServerFunc scriptFn
static void SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn)
static tPanelFn pPanelHijack
static void AddMultiPathsToPathList(const wxString &multiPathString, wxArrayString &pathList)
static void FindFilesInPathList(const wxString &pattern, const wxArrayString &pathList, wxArrayString &results, int flags=wxDIR_FILES)
std::vector< std::unique_ptr< Module > > mModules
static void Run()
Calls the script function, passing it the function for obeying commands.
static void SetModuleStatus(const wxString &fname, int iStatus)
static int GetModuleStatus(const wxString &fname)
int ShowMultiDialog(const wxString &message, const wxString &title, const wxChar **buttons, const wxString &boxMsg, bool log)
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
static void AddUniquePathToPathList(const wxString &path, wxArrayString &pathList)
#define mainPanelFnName
int(* tpRegScriptServerFunc)(tpExecScriptServerFunc pFn)
wxArrayString audacityPathList
A list of directories that should be searched for Audacity files (plug-ins, help files, etc.).
Definition: AudacityApp.h:138
AudacityApp & wxGetApp()
#define scriptFnName
static void SetCommandHandler(CommandHandler &ch)
void ModuleManager::InitializeBuiltins ( )
private

Definition at line 393 of file ModuleManager.cpp.

References ModuleInterface::AutoRegisterPlugins(), PluginManager::Get(), mDynModules, and PluginManager::RegisterPlugin().

Referenced by DiscoverProviders().

394 {
396 
397  for (auto moduleMain : builtinModuleList())
398  {
399  ModuleInterfaceHandle module {
400  moduleMain(this, NULL), ModuleInterfaceDeleter{}
401  };
402 
403  if (module->Initialize())
404  {
405  // Register the provider
406  ModuleInterface *pInterface = module.get();
407  const PluginID & id = pm.RegisterPlugin(pInterface);
408 
409  // Need to remember it
410  mDynModules[id] = std::move(module);
411 
412  // Allow the module to auto-register children
413  pInterface->AutoRegisterPlugins(pm);
414  }
415  else
416  {
417  // Don't leak! Destructor of module does that.
418  }
419  }
420 }
virtual bool AutoRegisterPlugins(PluginManagerInterface &pluginManager)=0
wxString PluginID
Definition: Types.h:209
ModuleMap mDynModules
PluginManager maintains a list of all plug ins. That covers modules, effects, generators, analysis-effects, commands. It also has functions for shared and private configs - which need to move out.
const PluginID & RegisterPlugin(ModuleInterface *module) override
std::unique_ptr< ModuleInterface, ModuleInterfaceDeleter > ModuleInterfaceHandle
Definition: ModuleManager.h:71
static PluginManager & Get()
bool ModuleManager::IsPluginValid ( const PluginID provider,
const wxString &  path,
bool  bFast 
)

Definition at line 606 of file ModuleManager.cpp.

References mDynModules.

Referenced by PluginManager::CheckForUpdates().

609 {
610  if (mDynModules.find(providerID) == mDynModules.end())
611  {
612  return false;
613  }
614 
615  return mDynModules[providerID]->IsPluginValid(path, bFast);
616 }
ModuleMap mDynModules
bool ModuleManager::IsProviderValid ( const PluginID provider,
const wxString &  path 
)

Definition at line 588 of file ModuleManager.cpp.

Referenced by PluginManager::CheckForUpdates().

590 {
591  // Builtin modules do not have a path
592  if (path.IsEmpty())
593  {
594  return true;
595  }
596 
597  wxFileName lib(path);
598  if (lib.FileExists() || lib.DirExists())
599  {
600  return true;
601  }
602 
603  return false;
604 }
ModuleInterface * ModuleManager::LoadModule ( const wxString &  path)
private

Definition at line 422 of file ModuleManager.cpp.

References PluginManager::GetID(), IdentInterface::GetSymbol(), mDynModules, mLibs, and MODULE_ENTRY.

Referenced by CreateProviderInstance(), and DiscoverProviders().

423 {
424  auto lib = std::make_unique<wxDynamicLibrary>();
425 
426  if (lib->Load(path, wxDL_NOW))
427  {
428  bool success = false;
429  ModuleMain audacityMain = (ModuleMain) lib->GetSymbol(wxSTRINGIZE_T(MODULE_ENTRY),
430  &success);
431  if (success && audacityMain)
432  {
433  ModuleInterfaceHandle handle {
434  audacityMain(this, &path), ModuleInterfaceDeleter{}
435  };
436  if (handle)
437  {
438  if (handle->Initialize())
439  {
440 
441  auto module = handle.get();
442  mDynModules[PluginManager::GetID(module)] = std::move(handle);
443  mLibs[module] = std::move(lib);
444 
445  return module;
446  }
447  }
448  }
449 
450  lib->Unload();
451  }
452 
453  return NULL;
454 }
static PluginID GetID(ModuleInterface *module)
virtual IdentInterfaceSymbol GetSymbol()=0
ModuleInterface *(* ModuleMain)(ModuleManagerInterface *moduleManager, const wxString *path)
ModuleMap mDynModules
#define MODULE_ENTRY
std::unique_ptr< ModuleInterface, ModuleInterfaceDeleter > ModuleInterfaceHandle
Definition: ModuleManager.h:71
LibraryMap mLibs
bool ModuleManager::RegisterEffectPlugin ( const PluginID provider,
const wxString &  path,
wxString &  errMsg 
)

Definition at line 542 of file ModuleManager.cpp.

References PluginManagerInterface::DefaultRegistrationCallback(), and mDynModules.

Referenced by PluginRegistrationDialog::OnOK().

543 {
544  errMsg.clear();
545  if (mDynModules.find(providerID) == mDynModules.end())
546  {
547  return false;
548  }
549 
550  auto nFound = mDynModules[providerID]->DiscoverPluginsAtPath(path, errMsg, PluginManagerInterface::DefaultRegistrationCallback);
551 
552  return nFound > 0;
553 }
static const PluginID & DefaultRegistrationCallback(ModuleInterface *provider, IdentInterface *ident)
ModuleMap mDynModules
void ModuleManager::RegisterModule ( ModuleInterface module)
overridevirtual

Implements ModuleManagerInterface.

Definition at line 472 of file ModuleManager.cpp.

References PluginManager::Get(), PluginManager::GetID(), mDynModules, and PluginManager::RegisterPlugin().

473 {
474  std::unique_ptr<ModuleInterface> module{ inModule };
475 
476  PluginID id = PluginManager::GetID(module.get());
477 
478  if (mDynModules.find(id) != mDynModules.end())
479  {
480  // TODO: Should we complain about a duplicate registeration????
481  // PRL: Don't leak resources!
482  module->Terminate();
483  return;
484  }
485 
487  module.release(), ModuleInterfaceDeleter{}
488  };
489 
491 }
static PluginID GetID(ModuleInterface *module)
wxString PluginID
Definition: Types.h:209
ModuleMap mDynModules
const PluginID & RegisterPlugin(ModuleInterface *module) override
std::unique_ptr< ModuleInterface, ModuleInterfaceDeleter > ModuleInterfaceHandle
Definition: ModuleManager.h:71
static PluginManager & Get()
virtual void Terminate()=0

Member Data Documentation

ModuleMap ModuleManager::mDynModules
private
std::unique_ptr< ModuleManager > ModuleManager::mInstance {}
staticprivate

Definition at line 123 of file ModuleManager.h.

Referenced by Get().

LibraryMap ModuleManager::mLibs
private

Definition at line 127 of file ModuleManager.h.

Referenced by LoadModule(), and ModuleInterfaceDeleter::operator()().

ModuleMainMap ModuleManager::mModuleMains
private

Definition at line 125 of file ModuleManager.h.

std::vector<std::unique_ptr<Module> > ModuleManager::mModules
private

Definition at line 129 of file ModuleManager.h.

Referenced by Dispatch(), and Initialize().

friend ModuleManager::ModuleInterfaceDeleter
private

Definition at line 121 of file ModuleManager.h.


The documentation for this class was generated from the following files: