/[svn]/linuxsampler/trunk/src/plugins/InstrumentEditorFactory.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/plugins/InstrumentEditorFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1896 by schoenebeck, Wed Jan 30 01:51:46 2008 UTC revision 1897 by persson, Sun May 10 09:31:51 2009 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2007, 2008 Christian Schoenebeck                        *   *   Copyright (C) 2007 - 2009 Christian Schoenebeck                       *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
6   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 21  Line 21 
21  #include "InstrumentEditorFactory.h"  #include "InstrumentEditorFactory.h"
22    
23  #include "../common/global_private.h"  #include "../common/global_private.h"
24    #include "../Sampler.h"
25    
26  #if defined(WIN32)  #if defined(WIN32)
27  #include <windows.h>  #include <windows.h>
# Line 38  Line 39 
39  # error "Configuration macro CONFIG_PLUGIN_DIR not defined!"  # error "Configuration macro CONFIG_PLUGIN_DIR not defined!"
40  #endif // CONFIG_PLUGIN_DIR  #endif // CONFIG_PLUGIN_DIR
41    
 #if defined(WIN32)  
 typedef void* (*InnerFactoryRegisterFunction)(void);  
 #endif  
   
42  namespace LinuxSampler {  namespace LinuxSampler {
43    
44      std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories;      std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories;
# Line 105  namespace LinuxSampler { Line 102  namespace LinuxSampler {
102          if (!bPluginsLoaded) {          if (!bPluginsLoaded) {
103              dmsg(1,("Loading instrument editor plugins..."));              dmsg(1,("Loading instrument editor plugins..."));
104              #if defined(WIN32)              #if defined(WIN32)
105              WIN32_FIND_DATA win32FindData;              String dir = Sampler::GetInstallDir();
106              const String plugindir = (String)CONFIG_PLUGIN_DIR + (String)("\\*.DLL");              if (dir.empty() || !LoadPlugins(dir + "\\plugins")) {
107              HANDLE hDir = FindFirstFile(plugindir.c_str(), &win32FindData);                  if (!LoadPlugins(CONFIG_PLUGIN_DIR)) {
             if (hDir == INVALID_HANDLE_VALUE) {  
                 if (GetLastError() != ERROR_FILE_NOT_FOUND) {  
108                      std::cerr << "Could not open instrument editor plugins "                      std::cerr << "Could not open instrument editor plugins "
109                                << "directory (" << CONFIG_PLUGIN_DIR << "), "                                << "directory (" << dir << "\\plugins or "
110                                << "Error: " << GetLastError() << std::endl;                                << CONFIG_PLUGIN_DIR << "), Error: "
111                  } else {                                << GetLastError() << std::endl;
112                      dmsg(1,("None"));                      return;
113                  }                  }
114                  // either dir doesn't exist or is empty              }
115                #else
116                if (!LoadPlugins(CONFIG_PLUGIN_DIR)) {
117                    std::cerr << "Could not open instrument editor plugins "
118                              << "directory (" << CONFIG_PLUGIN_DIR << "): "
119                              << strerror(errno) << std::endl;
120                  return;                  return;
121              }              }
122                #endif
123                bPluginsLoaded = true;
124                dmsg(1,("OK\n"));
125            }
126        }
127    
128              do {      bool InstrumentEditorFactory::LoadPlugins(String plugindir) {
129                  // skip directory entries          #if defined(WIN32)
130                  if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)          WIN32_FIND_DATA win32FindData;
131                      continue;          const String pluginpattern = plugindir + "\\*.dll";
132                  // dir entry name as full qualified path          HANDLE hDir = FindFirstFile(pluginpattern.c_str(), &win32FindData);
133                  const String sPath = (String)CONFIG_PLUGIN_DIR + ((String)"\\" + (String)win32FindData.cFileName);          if (hDir == INVALID_HANDLE_VALUE) {
134                  // load the DLL              if (GetLastError() != ERROR_FILE_NOT_FOUND) {
135                  HINSTANCE hinstLib;                  return false;
136                  void* pDLL = hinstLib = LoadLibrary(sPath.c_str());              } else {
137                  if (!pDLL) {                  dmsg(1,("None"));
138                      std::cerr << "Failed to load instrument editor plugin: "                  return true;
139                                << sPath << std::endl;              }
140                      continue;          }
                 }  
141    
142                  //(InnerFactory*) (*fn)(void);          do {
143                  InnerFactoryRegisterFunction fn =              // skip directory entries
144                      (InnerFactoryRegisterFunction)              if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
145                          GetProcAddress(                  continue;
146                              hinstLib,              // dir entry name as full qualified path
147                              "createInstrumentEditorInnerFactory"              const String sPath = plugindir + "\\" + win32FindData.cFileName;
148                          );              // load the DLL
149                  if (fn == NULL) {              HINSTANCE hinstLib;
150                      std::cerr << "ERROR: unable to find "              void* pDLL = hinstLib = LoadLibrary(sPath.c_str());
151                                   "createInstrumentEditorInnerFactory() "              if (!pDLL) {
152                                   "in DLL\n" << std::flush;                  std::cerr << "Failed to load instrument editor plugin: "
153                      FreeLibrary(hinstLib);                            << sPath << std::endl;
154                      continue;                  continue;
155                  }              }
156    
157                  // get the plugin instance and register it to the factory              InnerFactory* (*fn)() = (InnerFactory* (*)())
158                    GetProcAddress(hinstLib, "createInstrumentEditorInnerFactory");
159                if (fn == NULL) {
160                    std::cerr << "ERROR: unable to find "
161                                 "createInstrumentEditorInnerFactory() "
162                                 "in DLL\n" << std::flush;
163                    FreeLibrary(hinstLib);
164                    continue;
165                }
166    
167                  InnerFactory* pInnerFactory = (InnerFactory*)fn();              // get the plugin instance and register it to the factory
                 if (!pInnerFactory) {  
                     std::cerr << "ERROR: !pInnerFactory\n" << std::flush;  
                     FreeLibrary(hinstLib);  
                     continue;  
                 }  
                 InstrumentEditor* pEditor = pInnerFactory->Create();  
                 if (InnerFactories.count(pEditor->Name())) {  
                     std::cerr << "ERROR: a plugin with name '"  
                               << pEditor->Name()  
                               << "' already loaded (skipping)\n"  
                               << std::flush;  
                     pInnerFactory->Destroy(pEditor);  
                     FreeLibrary(hinstLib);  
                     continue;  
                 }  
                 InnerFactories[pEditor->Name()] = pInnerFactory;  
                 pInnerFactory->Destroy(pEditor);  
168    
169                  LoadedDLLs.push_back(pDLL);              InnerFactory* pInnerFactory = fn();
170              } while (FindNextFile(hDir, &win32FindData));              if (!pInnerFactory) {
171              if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir);                  std::cerr << "ERROR: !pInnerFactory\n" << std::flush;
172              #else // POSIX                  FreeLibrary(hinstLib);
173                          #if defined(__APPLE__)  /*  20071224 Toshi Nagata  */                  continue;
                         String Config_plugin_dir = (String)CONFIG_PLUGIN_DIR;  
                         if (Config_plugin_dir.find("~") == 0)  
                                    Config_plugin_dir.replace(0, 1, getenv("HOME"));  
                         DIR* hDir = opendir(Config_plugin_dir.c_str());  
                         #else  
             DIR* hDir = opendir(CONFIG_PLUGIN_DIR);  
                         #endif  
             if (!hDir) {  
                 std::cerr << "Could not open instrument editor plugins directory "  
                           << "(" << CONFIG_PLUGIN_DIR << "): "  
                           << strerror(errno) << std::endl;  
                 return;  
174              }              }
175              for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) {              InstrumentEditor* pEditor = pInnerFactory->Create();
176                  // dir entry name as full qualified path              if (InnerFactories.count(pEditor->Name())) {
177                          #if defined(__APPLE__)  /*  20080110 Toshi Nagata  */                  std::cerr << "ERROR: a plugin with name '"
178                  const String sPath = Config_plugin_dir + ("/" + String(pEntry->d_name));                            << pEditor->Name()
179                          #else                            << "' already loaded (skipping)\n"
180                  const String sPath = CONFIG_PLUGIN_DIR + ("/" + String(pEntry->d_name));                            << std::flush;
181                          #endif                  pInnerFactory->Destroy(pEditor);
182                  // skip entries that are not regular files                  FreeLibrary(hinstLib);
183                  struct stat entry_stat;                  continue;
184                  if (lstat(sPath.c_str(), &entry_stat) != 0 ||              }
185                     (entry_stat.st_mode & S_IFMT) != S_IFREG)              InnerFactories[pEditor->Name()] = pInnerFactory;
186                     continue;              pInnerFactory->Destroy(pEditor);
187                  // skip files that are not .so files  
188                  if (sPath.length() < 3 ||              LoadedDLLs.push_back(pDLL);
189                      sPath.substr(sPath.length() - 3) != ".so" &&          } while (FindNextFile(hDir, &win32FindData));
190                      sPath.find(".so.") == String::npos  
191                  ) continue;          if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir);
192                  // load the DLL (the plugins should register themselfes automatically)  
193                  void* pDLL = dlopen(sPath.c_str(), RTLD_NOW);          #else // POSIX
194                  if (pDLL) LoadedDLLs.push_back(pDLL);  
195                  else {          #if defined(__APPLE__)  /*  20071224 Toshi Nagata  */
196                      std::cerr << "Failed to load instrument editor plugin: '"          if (plugindir.find("~") == 0)
197                                << sPath << "', cause: " << dlerror() << std::endl;              plugindir.replace(0, 1, getenv("HOME"));
198                  }          #endif
199            DIR* hDir = opendir(plugindir.c_str());
200            if (!hDir) {
201                return false;
202            }
203            for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) {
204                // dir entry name as full qualified path
205                const String sPath = plugindir + "/" + pEntry->d_name;
206                // skip entries that are not regular files
207                struct stat entry_stat;
208                if (lstat(sPath.c_str(), &entry_stat) != 0 ||
209                    (entry_stat.st_mode & S_IFMT) != S_IFREG)
210                    continue;
211                // skip files that are not .so files
212                if (sPath.length() < 3 ||
213                    sPath.substr(sPath.length() - 3) != ".so" &&
214                    sPath.find(".so.") == String::npos)
215                    continue;
216                // load the DLL (the plugins should register themselfes automatically)
217                void* pDLL = dlopen(sPath.c_str(), RTLD_NOW);
218                if (pDLL) LoadedDLLs.push_back(pDLL);
219                else {
220                    std::cerr << "Failed to load instrument editor plugin: '"
221                              << sPath << "', cause: " << dlerror() << std::endl;
222              }              }
             closedir(hDir);  
             #endif  
             bPluginsLoaded = true;  
             dmsg(1,("OK\n"));  
223          }          }
224            closedir(hDir);
225            #endif
226            return true;
227      }      }
228    
229      void InstrumentEditorFactory::ClosePlugins() {      void InstrumentEditorFactory::ClosePlugins() {

Legend:
Removed from v.1896  
changed lines
  Added in v.1897

  ViewVC Help
Powered by ViewVC