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.

160 lines
3.8KB

  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 "SpiralInfo.h"
  21. #include "PluginManager.h"
  22. PluginManager *PluginManager::m_Singleton = NULL;
  23. PluginManager::PluginManager()
  24. {
  25. }
  26. PluginManager::~PluginManager()
  27. {
  28. UnloadAll();
  29. }
  30. PluginID PluginManager::LoadPlugin(const char *PluginName)
  31. {
  32. // Make a new plugin
  33. HostsideInfo *NewPlugin = new HostsideInfo;
  34. NewPlugin->Handle=NULL;
  35. NewPlugin->ID=PluginError;
  36. // Attempt to open the plugin
  37. NewPlugin->Handle = dlopen (PluginName, RTLD_NOW);
  38. if (NewPlugin->Handle==NULL)
  39. {
  40. SpiralInfo::Alert("Error loading ["+string(PluginName)+"]: \n"+string(dlerror()));
  41. return PluginError;
  42. }
  43. // Link the neccesary functions
  44. char *error;
  45. NewPlugin->CreateInstance = (SpiralPlugin*(*)()) dlsym(NewPlugin->Handle, "SpiralPlugin_CreateInstance");
  46. if ((error = dlerror()) != NULL)
  47. {
  48. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  49. return PluginError;
  50. }
  51. NewPlugin->GetIcon = (char **(*)()) dlsym(NewPlugin->Handle, "SpiralPlugin_GetIcon");
  52. if ((error = dlerror()) != NULL)
  53. {
  54. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  55. return PluginError;
  56. }
  57. NewPlugin->GetID = (int(*)()) dlsym(NewPlugin->Handle, "SpiralPlugin_GetID");
  58. if ((error = dlerror()) != NULL)
  59. {
  60. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  61. return PluginError;
  62. }
  63. NewPlugin->GetGroupName = (string(*)()) dlsym(NewPlugin->Handle, "SpiralPlugin_GetGroupName");
  64. if ((error = dlerror()) != NULL)
  65. {
  66. SpiralInfo::Alert("Error linking to plugin "+string(PluginName)+"\n"+string(error));
  67. return PluginError;
  68. }
  69. // We've succesfully open and linked the
  70. // plugin, so add it to the vector.
  71. int ID;
  72. ID = NewPlugin->GetID();
  73. NewPlugin->ID = ID;
  74. m_PluginVec.push_back(NewPlugin);
  75. return ID;
  76. }
  77. void PluginManager::UnLoadPlugin(PluginID ID)
  78. {
  79. if (IsValid(ID))
  80. {
  81. dlclose(GetPlugin(ID)->Handle);
  82. char *error;
  83. if ((error = dlerror()) != NULL)
  84. {
  85. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  86. }
  87. }
  88. }
  89. void PluginManager::UnloadAll()
  90. {
  91. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  92. i!=m_PluginVec.end(); i++)
  93. {
  94. dlclose((*i)->Handle);
  95. char *error;
  96. if ((error = dlerror()) != NULL)
  97. {
  98. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  99. }
  100. }
  101. }
  102. const HostsideInfo *PluginManager::GetPlugin(PluginID ID)
  103. {
  104. return GetPlugin_i(ID);
  105. }
  106. HostsideInfo *PluginManager::GetPlugin_i(PluginID ID)
  107. {
  108. HostsideInfo *ret = NULL;
  109. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  110. i!=m_PluginVec.end(); i++)
  111. {
  112. if ((*i)->ID==ID)
  113. {
  114. ret = *i;
  115. break;
  116. }
  117. }
  118. if (!ret)
  119. {
  120. char t[256];
  121. sprintf(t,"%d",ID);
  122. SpiralInfo::Alert("Plugin "+string(t)+" not found.");
  123. }
  124. return ret;
  125. }
  126. bool PluginManager::IsValid(PluginID ID)
  127. {
  128. const HostsideInfo *t = GetPlugin(ID);
  129. return (t && t->Handle!=NULL);
  130. }