You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.7KB

  1. /* SpiralSound
  2. * Copyleft (C) 2002 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <dlfcn.h>
  19. #include <stdio.h>
  20. #include "PluginManager.h"
  21. PluginManager *PluginManager::m_Singleton = NULL;
  22. PluginManager::PluginManager()
  23. {
  24. }
  25. PluginManager::~PluginManager()
  26. {
  27. UnloadAll();
  28. }
  29. PluginID PluginManager::LoadPlugin(const char *PluginName)
  30. {
  31. // Make a new plugin
  32. HostsideInfo *NewPlugin = new HostsideInfo;
  33. NewPlugin->Handle=NULL;
  34. NewPlugin->ID=PluginError;
  35. // Attempt to open the plugin
  36. NewPlugin->Handle = dlopen (PluginName, RTLD_NOW);
  37. if (NewPlugin->Handle==NULL)
  38. {
  39. SpiralInfo::Alert("Error loading plugin: \n"+string(dlerror()));
  40. return PluginError;
  41. }
  42. // Link the neccesary functions
  43. char *error;
  44. NewPlugin->CreateInstance = (SpiralPlugin*(*)()) dlsym(NewPlugin->Handle, "CreateInstance");
  45. if ((error = dlerror()) != NULL)
  46. {
  47. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  48. return PluginError;
  49. }
  50. NewPlugin->GetIcon = (char **(*)()) dlsym(NewPlugin->Handle, "GetIcon");
  51. if ((error = dlerror()) != NULL)
  52. {
  53. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  54. return PluginError;
  55. }
  56. NewPlugin->GetID = (int(*)()) dlsym(NewPlugin->Handle, "GetID");
  57. if ((error = dlerror()) != NULL)
  58. {
  59. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  60. return PluginError;
  61. }
  62. NewPlugin->GetGroupName = (string(*)()) dlsym(NewPlugin->Handle, "GetGroupName");
  63. if ((error = dlerror()) != NULL)
  64. {
  65. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  66. return PluginError;
  67. }
  68. // We've succesfully open and linked the
  69. // plugin, so add it to the vector.
  70. int ID;
  71. ID = NewPlugin->GetID();
  72. NewPlugin->ID = ID;
  73. m_PluginVec.push_back(NewPlugin);
  74. return ID;
  75. }
  76. void PluginManager::UnLoadPlugin(PluginID ID)
  77. {
  78. if (IsValid(ID))
  79. {
  80. dlclose(GetPlugin(ID)->Handle);
  81. char *error;
  82. if ((error = dlerror()) != NULL)
  83. {
  84. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  85. }
  86. }
  87. }
  88. void PluginManager::UnloadAll()
  89. {
  90. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  91. i!=m_PluginVec.end(); i++)
  92. {
  93. dlclose((*i)->Handle);
  94. char *error;
  95. if ((error = dlerror()) != NULL)
  96. {
  97. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  98. }
  99. }
  100. }
  101. const HostsideInfo *PluginManager::GetPlugin(PluginID ID)
  102. {
  103. return GetPlugin_i(ID);
  104. }
  105. HostsideInfo *PluginManager::GetPlugin_i(PluginID ID)
  106. {
  107. HostsideInfo *ret = NULL;
  108. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  109. i!=m_PluginVec.end(); i++)
  110. {
  111. if ((*i)->ID==ID)
  112. {
  113. ret = *i;
  114. break;
  115. }
  116. }
  117. if (!ret)
  118. {
  119. char t[256];
  120. sprintf(t,"%d",ID);
  121. SpiralInfo::Alert("Plugin "+string(t)+" not found.");
  122. }
  123. return ret;
  124. }
  125. bool PluginManager::IsValid(PluginID ID)
  126. {
  127. const HostsideInfo *t = GetPlugin(ID);
  128. return (t && t->Handle!=NULL);
  129. }