/[svn]/linuxsampler/trunk/src/hostplugins/lv2/PluginLv2.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/hostplugins/lv2/PluginLv2.cpp

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

revision 2174 by capela, Tue Apr 12 15:19:56 2011 UTC revision 2340 by capela, Fri Mar 30 23:54:44 2012 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2008 Andreas Persson                                    *   *   Copyright (C) 2008 - 2012 Andreas Persson                             *
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 39  namespace { Line 39  namespace {
39          Out[0] = 0;          Out[0] = 0;
40          Out[1] = 0;          Out[1] = 0;
41          UriMap = 0;          UriMap = 0;
42          PathSupport = 0;          MapPath = 0;
43          NewFileSupport = 0;          MakePath = 0;
44          for (int i = 0 ; Features[i] ; i++) {          for (int i = 0 ; Features[i] ; i++) {
45              dmsg(2, ("linuxsampler: host feature: %s\n", Features[i]->URI));              dmsg(2, ("linuxsampler: init feature: %s\n", Features[i]->URI));
46              if (!strcmp(Features[i]->URI, LV2_URI_MAP_URI)) {              if (!strcmp(Features[i]->URI, LV2_URI_MAP_URI)) {
47                  UriMap = (LV2_URI_Map_Feature*)Features[i]->data;                  UriMap = (LV2_URI_Map_Feature*)Features[i]->data;
48              } else if (!strcmp(Features[i]->URI, LV2_FILES_PATH_SUPPORT_URI)) {              } else if (!strcmp(Features[i]->URI, LV2_STATE__mapPath)) {
49                  PathSupport = (LV2_Files_Path_Support*)Features[i]->data;                  MapPath = (LV2_State_Map_Path*)Features[i]->data;
50              } else if (!strcmp(Features[i]->URI, LV2_FILES_NEW_FILE_SUPPORT_URI)) {              } else if (!strcmp(Features[i]->URI, LV2_STATE__makePath)) {
51                  NewFileSupport = (LV2_Files_New_File_Support*)Features[i]->data;                  MakePath = (LV2_State_Make_Path*)Features[i]->data;
52              }              }
53          }          }
54    
# Line 103  namespace { Line 103  namespace {
103      }      }
104    
105      String PluginLv2::PathToState(const String& path) {      String PluginLv2::PathToState(const String& path) {
106          if (PathSupport) {          if (MapPath) {
107              char* cstr = PathSupport->abstract_path(PathSupport->host_data,              char* cstr = MapPath->abstract_path(MapPath->handle, path.c_str());
                                                     path.c_str());  
108              const String abstract_path(cstr);              const String abstract_path(cstr);
109              free(cstr);              free(cstr);
110              return abstract_path;              return abstract_path;
# Line 114  namespace { Line 113  namespace {
113      }      }
114    
115      String PluginLv2::PathFromState(const String& path) {      String PluginLv2::PathFromState(const String& path) {
116          if (PathSupport) {          if (MapPath) {
117              char* cstr = PathSupport->absolute_path(PathSupport->host_data,              char* cstr = MapPath->absolute_path(MapPath->handle, path.c_str());
                                                     path.c_str());  
118              const String abstract_path(cstr);              const String abstract_path(cstr);
119              free(cstr);              free(cstr);
120              return abstract_path;              return abstract_path;
# Line 124  namespace { Line 122  namespace {
122          return path;          return path;
123      }      }
124    
125      void PluginLv2::Save(LV2_Persist_Store_Function store, void* host_data) {      void PluginLv2::SetStateFeatures(const LV2_Feature* const* Features)
126          if (NewFileSupport) {      {
127              char* path = NewFileSupport->new_file_path(PathSupport->host_data,          for (int i = 0 ; Features[i] ; i++) {
128                                                         "linuxsampler");              dmsg(2, ("linuxsampler: state feature: %s\n", Features[i]->URI));
129              dmsg(2, ("saving to file %s\n", path));              if (!strcmp(Features[i]->URI, LV2_STATE__mapPath)) {
130                    MapPath = (LV2_State_Map_Path*)Features[i]->data;
131                } else if (!strcmp(Features[i]->URI, LV2_STATE__makePath)) {
132                    MakePath = (LV2_State_Make_Path*)Features[i]->data;
133                }
134            }
135        }
136    
137              std::ofstream out(path);      LV2_State_Status PluginLv2::Save(
138                LV2_State_Store_Function store, LV2_State_Handle handle,
139                uint32_t flags, const LV2_Feature* const* features)
140        {
141            LV2_State_Map_Path*  OldMapPath  = MapPath;
142            LV2_State_Make_Path* OldMakePath = MakePath;
143            SetStateFeatures(features);
144    
145            if (MakePath && MapPath) {
146                char* abs_path = MakePath->path(MakePath->handle, "linuxsampler");
147                dmsg(2, ("saving to file %s\n", abs_path));
148    
149                std::ofstream out(abs_path);
150              out << GetState();              out << GetState();
151    
152              const String abstract_path = PathToState(path);              char* path = MapPath->abstract_path(MapPath->handle, abs_path);
153    
154              store(host_data,              store(handle,
155                    uri_to_id(NULL, NS_LS "state-file"),                    uri_to_id(NULL, NS_LS "state-file"),
156                    abstract_path.c_str(),                    path,
157                    abstract_path.length() + 1,                    strlen(path) + 1,
158                    uri_to_id(NULL, LV2_FILES_URI "#AbstractPath"),                    uri_to_id(NULL, NS_ATOM "Path"),
159                    LV2_PERSIST_IS_PORTABLE);                    LV2_STATE_IS_PORTABLE);
160    
161                free(path);
162                free(abs_path);
163          } else {          } else {
164              dmsg(2, ("saving to string\n"));              dmsg(2, ("saving to string\n"));
165    
166              std::ostringstream out;              std::ostringstream out;
167              out << GetState();              out << GetState();
168    
169              store(host_data,              store(handle,
170                    uri_to_id(NULL, NS_LS "state-string"),                    uri_to_id(NULL, NS_LS "state-string"),
171                    out.str().c_str(),                    out.str().c_str(),
172                    out.str().length() + 1,                    out.str().length() + 1,
173                    uri_to_id(NULL, NS_ATOM "String"),                    uri_to_id(NULL, NS_ATOM "String"),
174                    LV2_PERSIST_IS_POD | LV2_PERSIST_IS_PORTABLE);                    LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
175          }          }
176          dmsg(2, ("saving done\n"));          dmsg(2, ("saving done\n"));
177    
178            MapPath  = OldMapPath;
179            MakePath = OldMakePath;
180    
181            return LV2_STATE_SUCCESS;
182      }      }
183    
184      void PluginLv2::Restore(LV2_Persist_Retrieve_Function retrieve, void* data) {      LV2_State_Status PluginLv2::Restore(
185                LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle,
186                uint32_t rflags, const LV2_Feature* const* features)
187        {
188            LV2_State_Map_Path*  OldMapPath  = MapPath;
189            LV2_State_Make_Path* OldMakePath = MakePath;
190            SetStateFeatures(features);
191    
192          size_t   size;          size_t   size;
193          uint32_t type;          uint32_t type;
194          uint32_t flags;          uint32_t flags;
195    
196          const void* value = retrieve(          const void* value = retrieve(
197              data,              handle,
198              uri_to_id(NULL, NS_LS "state-file"),              uri_to_id(NULL, NS_LS "state-file"),
199              &size, &type, &flags);              &size, &type, &flags);
   
200          if (value) {          if (value) {
201              assert(type == uri_to_id(NULL, LV2_FILES_URI "#AbstractPath"));              // Restore from state-file
202              const String path = PathFromState((const char*)value);              assert(type == uri_to_id(NULL, NS_ATOM "Path"));
203                const String path((const char*)value);
204              dmsg(2, ("linuxsampler: restoring from file %s\n", path.c_str()));              dmsg(2, ("linuxsampler: restoring from file %s\n", path.c_str()));
205              std::ifstream in(path.c_str());              std::ifstream in(path.c_str());
206              String state;              String state;
207              std::getline(in, state, '\0');              std::getline(in, state, '\0');
208              SetState(state);              SetState(state);
209              return;          } else if ((value = retrieve(handle,
210          }                                       uri_to_id(NULL, NS_LS "state-string"),
211                                         &size, &type, &flags))) {
212          value = retrieve(              // Restore from state-string
                 data,  
                 uri_to_id(NULL, NS_LS "state-string"),  
                 &size, &type, &flags);  
         if (value) {  
213              dmsg(2, ("linuxsampler: restoring from string\n"));              dmsg(2, ("linuxsampler: restoring from string\n"));
214              assert(type == uri_to_id(NULL, NS_ATOM "String"));              assert(type == uri_to_id(NULL, NS_ATOM "String"));
215              String state((const char*)value);              String state((const char*)value);
216              SetState(state);              SetState(state);
217              return;          } else {
218                // No valid state found, reset to default state
219                dmsg(2, ("linuxsampler: restoring default state\n"));
220                SetState(DefaultState);
221          }          }
222    
223          // No valid state found, reset to default state          MapPath  = OldMapPath;
224          dmsg(2, ("linuxsampler: restoring default state\n"));          MakePath = OldMakePath;
225          SetState(DefaultState);  
226            return LV2_STATE_SUCCESS;
227      }      }
228    
229      LV2_Handle instantiate(const LV2_Descriptor* descriptor,      LV2_Handle instantiate(const LV2_Descriptor* descriptor,
# Line 221  namespace { Line 252  namespace {
252          delete static_cast<PluginLv2*>(instance);          delete static_cast<PluginLv2*>(instance);
253      }      }
254    
255      void save(LV2_Handle handle, LV2_Persist_Store_Function store, void* callback_data) {      LV2_State_Status save(LV2_Handle handle, LV2_State_Store_Function store,
256              return static_cast<PluginLv2*>(handle)->Save(store, callback_data);                            LV2_State_Handle state,
257                              uint32_t flags, const LV2_Feature* const* features) {
258            return static_cast<PluginLv2*>(handle)->Save(
259                store, state, flags, features);
260      }      }
261    
262      void restore(LV2_Handle handle, LV2_Persist_Retrieve_Function store, void* callback_data) {      LV2_State_Status restore(LV2_Handle handle, LV2_State_Retrieve_Function retrieve,
263          return static_cast<PluginLv2*>(handle)->Restore(store, callback_data);                               LV2_State_Handle state,
264                                 uint32_t flags, const LV2_Feature* const* features) {
265            return static_cast<PluginLv2*>(handle)->Restore(
266                retrieve, state, flags, features);
267      }      }
268    
269      PluginInfo PluginInfo::Instance;      PluginInfo PluginInfo::Instance;
# Line 240  namespace { Line 277  namespace {
277          Lv2.instantiate = instantiate;          Lv2.instantiate = instantiate;
278          Lv2.run = run;          Lv2.run = run;
279          Lv2.extension_data = extension_data;          Lv2.extension_data = extension_data;
280          Persist.save = save;          StateInterface.save = save;
281          Persist.restore = restore;          StateInterface.restore = restore;
282      }      }
283    
284    
285      const void* extension_data(const char* uri) {      const void* extension_data(const char* uri) {
286          dmsg(2, ("linuxsampler: extension_data %s\n", uri));          dmsg(2, ("linuxsampler: extension_data %s\n", uri));
287          if (strcmp(uri, LV2_PERSIST_URI) == 0) {          if (strcmp(uri, LV2_STATE__interface) == 0) {
288              return PluginInfo::Lv2PersistDescriptor();              return PluginInfo::Lv2StateInterface();
289          }          }
290          return 0;          return 0;
291      }      }

Legend:
Removed from v.2174  
changed lines
  Added in v.2340

  ViewVC Help
Powered by ViewVC