2 Copyright (C) 2011 Paul Davis
3 Author: David Robillard
5 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
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_lv2_state_h__
21 #define __ardour_lv2_state_h__
29 #include "pbd/error.h"
31 #include "ardour/uri_map.h"
32 #include "lv2/lv2plug.in/ns/ext/state/state.h"
40 LV2State(const LV2Plugin& plug, URIMap& map) : plugin(plug), uri_map(map) {}
43 inline Value(uint32_t k, const void* v, size_t s, uint32_t t, uint32_t f)
44 : key(k), value(v), size(s), type(t), flags(f)
54 typedef std::map<uint32_t, std::string> URIs;
55 typedef std::map<uint32_t, Value> Values;
57 uint32_t file_id_to_runtime_id(uint32_t file_id) const {
58 URIs::const_iterator i = uris.find(file_id);
59 if (i == uris.end()) {
60 PBD::error << "LV2 state refers to undefined URI ID" << endmsg;
63 return uri_map.uri_to_id(NULL, i->second.c_str());
66 int add_uri(uint32_t file_id, const char* str) {
67 // TODO: check for clashes (invalid file)
68 uris.insert(std::make_pair(file_id, str));
72 int add_value(uint32_t file_key,
77 const uint32_t key = file_id_to_runtime_id(file_key);
78 const uint32_t type = file_id_to_runtime_id(file_type);
80 PBD::error << "Invalid file key or type" << endmsg;
84 Values::const_iterator i = values.find(key);
85 if (i != values.end()) {
86 PBD::error << "LV2 state contains duplicate keys" << endmsg;
89 void* value_copy = malloc(size);
90 memcpy(value_copy, value, size); // FIXME: leak
93 Value(key, value_copy, size, type, flags)));
98 void read(RDFF file) {
99 RDFFChunk* chunk = (RDFFChunk*)malloc(sizeof(RDFFChunk));
101 while (!rdff_read_chunk(file, &chunk)) {
102 if (rdff_chunk_is_uri(chunk)) {
103 RDFFURIChunk* body = (RDFFURIChunk*)chunk->data;
104 add_uri(body->id, body->uri);
105 } else if (rdff_chunk_is_triple(chunk)) {
106 RDFFTripleChunk* body = (RDFFTripleChunk*)chunk->data;
107 add_value(body->predicate,
111 LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
117 void write(RDFF file) {
118 // Write all referenced URIs to state file
119 for (URIs::const_iterator i = uris.begin(); i != uris.end(); ++i) {
126 // Write all values to state file
127 for (Values::const_iterator i = values.begin(); i != values.end(); ++i) {
128 const uint32_t key = i->first;
129 const LV2State::Value& val = i->second;
130 rdff_write_triple(file,
139 const LV2Plugin& plugin;
145 } // namespace ARDOUR
147 #endif /* __ardour_lv2_state_h__ */