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.

136 lines
3.1KB

  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. NewPlugin->CreateInstance = (SpiralPlugin*(*)()) dlsym(NewPlugin->Handle, "CreateInstance");
  44. NewPlugin->GetIcon = (char **(*)()) dlsym(NewPlugin->Handle, "GetIcon");
  45. NewPlugin->GetID = (int(*)()) dlsym(NewPlugin->Handle, "GetID");
  46. char *error;
  47. if ((error = dlerror()) != NULL)
  48. {
  49. SpiralInfo::Alert("Error linking to plugin: \n"+string(error));
  50. return PluginError;
  51. }
  52. // We've succesfully open and linked the
  53. // plugin, so add it to the vector.
  54. int ID;
  55. ID = NewPlugin->GetID();
  56. NewPlugin->ID = ID;
  57. m_PluginVec.push_back(NewPlugin);
  58. return ID;
  59. }
  60. void PluginManager::UnLoadPlugin(PluginID ID)
  61. {
  62. if (IsValid(ID))
  63. {
  64. dlclose(GetPlugin(ID)->Handle);
  65. char *error;
  66. if ((error = dlerror()) != NULL)
  67. {
  68. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  69. }
  70. }
  71. }
  72. void PluginManager::UnloadAll()
  73. {
  74. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  75. i!=m_PluginVec.end(); i++)
  76. {
  77. dlclose((*i)->Handle);
  78. char *error;
  79. if ((error = dlerror()) != NULL)
  80. {
  81. SpiralInfo::Alert("Error unlinking plugin: \n"+string(error));
  82. }
  83. }
  84. }
  85. const HostsideInfo *PluginManager::GetPlugin(PluginID ID)
  86. {
  87. return GetPlugin_i(ID);
  88. }
  89. HostsideInfo *PluginManager::GetPlugin_i(PluginID ID)
  90. {
  91. HostsideInfo *ret = NULL;
  92. for (vector<HostsideInfo*>::iterator i=m_PluginVec.begin();
  93. i!=m_PluginVec.end(); i++)
  94. {
  95. if ((*i)->ID==ID)
  96. {
  97. ret = *i;
  98. break;
  99. }
  100. }
  101. if (!ret)
  102. {
  103. char t[256];
  104. sprintf(t,"%d",ID);
  105. SpiralInfo::Alert("Plugin "+string(t)+" not found.");
  106. }
  107. return ret;
  108. }
  109. bool PluginManager::IsValid(PluginID ID)
  110. {
  111. const HostsideInfo *t = GetPlugin(ID);
  112. return (t && t->Handle!=NULL);
  113. }