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.

151 lines
3.5KB

  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. // We've succesfully open and linked the
  63. // plugin, so add it to the vector.
  64. int ID;
  65. ID = NewPlugin->GetID();
  66. NewPlugin->ID = ID;
  67. m_PluginVec.push_back(NewPlugin);
  68. return ID;
  69. }
  70. void PluginManager::UnLoadPlugin(PluginID ID)
  71. {
  72. if (IsValid(ID))
  73. {
  74. dlclose(GetPlugin(ID)->Handle);
  75. char *error;
  76. if ((error = dlerror()) != NULL)
  77. {
  78. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  79. }
  80. }
  81. }
  82. void PluginManager::UnloadAll()
  83. {
  84. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  85. i!=m_PluginVec.end(); i++)
  86. {
  87. dlclose((*i)->Handle);
  88. char *error;
  89. if ((error = dlerror()) != NULL)
  90. {
  91. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  92. }
  93. }
  94. }
  95. const HostsideInfo *PluginManager::GetPlugin(PluginID ID)
  96. {
  97. return GetPlugin_i(ID);
  98. }
  99. HostsideInfo *PluginManager::GetPlugin_i(PluginID ID)
  100. {
  101. HostsideInfo *ret = NULL;
  102. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  103. i!=m_PluginVec.end(); i++)
  104. {
  105. if ((*i)->ID==ID)
  106. {
  107. ret = *i;
  108. break;
  109. }
  110. }
  111. if (!ret)
  112. {
  113. char t[256];
  114. sprintf(t,"%d",ID);
  115. SpiralInfo::Alert("Plugin "+string(t)+" not found.");
  116. }
  117. return ret;
  118. }
  119. bool PluginManager::IsValid(PluginID ID)
  120. {
  121. const HostsideInfo *t = GetPlugin(ID);
  122. return (t && t->Handle!=NULL);
  123. }