Support LV2 atom sequence ports alongside old event ports.
[ardour.git] / libs / ardour / uri_map.cc
1 /*
2     Copyright (C) 2008-2011 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18 */
19
20 #include <cassert>
21 #include <iostream>
22
23 #include <stdint.h>
24 #include <string.h>
25
26 #include <glib.h>
27
28 #include "pbd/error.h"
29
30 #include "ardour/uri_map.h"
31
32 using namespace std;
33
34 namespace ARDOUR {
35
36
37 URIMap::URIMap()
38 {
39         _uri_map_feature_data.uri_to_id     = &URIMap::uri_map_uri_to_id;
40         _uri_map_feature_data.callback_data = this;
41         _uri_map_feature.URI                = LV2_URI_MAP_URI;
42         _uri_map_feature.data               = &_uri_map_feature_data;
43
44         _urid_map_feature_data.map    = &URIMap::urid_map;
45         _urid_map_feature_data.handle = this;
46         _urid_map_feature.URI         = LV2_URID_MAP_URI;
47         _urid_map_feature.data        = &_urid_map_feature_data;
48
49         _urid_unmap_feature_data.unmap  = &URIMap::urid_unmap;
50         _urid_unmap_feature_data.handle = this;
51         _urid_unmap_feature.URI         = LV2_URID_UNMAP_URI;
52         _urid_unmap_feature.data        = &_urid_unmap_feature_data;
53 }
54
55
56 uint32_t
57 URIMap::uri_to_id(const char* map,
58                   const char* uri)
59 {
60         const uint32_t id = static_cast<uint32_t>(g_quark_from_string(uri));
61         if (map && !strcmp(map, "http://lv2plug.in/ns/ext/event")) {
62                 GlobalToEvent::iterator i = _global_to_event.find(id);
63                 if (i != _global_to_event.end()) {
64                         return i->second;
65                 } else {
66                         if (_global_to_event.size() + 1 > UINT16_MAX) {
67                                 PBD::error << "Event URI " << uri << " ID out of range." << endl;
68                                 return 0;
69                         }
70                         const uint16_t ev_id = _global_to_event.size() + 1;
71                         assert(_event_to_global.find(ev_id) == _event_to_global.end());
72                         _global_to_event.insert(make_pair(id, ev_id));
73                         _event_to_global.insert(make_pair(ev_id, id));
74                         return ev_id;
75                 }
76         } else {
77                 return id;
78         }
79 }
80
81
82 const char*
83 URIMap::id_to_uri(const char*    map,
84                   const uint32_t id)
85 {
86         if (map && !strcmp(map, "http://lv2plug.in/ns/ext/event")) {
87                 EventToGlobal::iterator i = _event_to_global.find(id);
88                 if (i == _event_to_global.end()) {
89                         PBD::error << "Failed to unmap event URI " << id << endl;
90                         return NULL;
91                 }
92                 return g_quark_to_string(i->second);
93         } else {
94                 return g_quark_to_string(id);
95         }
96
97 }
98
99
100 uint32_t
101 URIMap::uri_map_uri_to_id(LV2_URI_Map_Callback_Data callback_data,
102                           const char*               map,
103                           const char*               uri)
104 {
105         URIMap* const me = (URIMap*)callback_data;
106         return me->uri_to_id(map, uri);
107 }
108
109
110 LV2_URID
111 URIMap::urid_map(LV2_URID_Map_Handle handle,
112                  const char*         uri)
113 {
114         URIMap* const me = (URIMap*)handle;
115         return me->uri_to_id(NULL, uri);
116 }
117
118
119 const char*
120 URIMap::urid_unmap(LV2_URID_Unmap_Handle handle,
121                    LV2_URID              urid)
122 {
123         URIMap* const me = (URIMap*)handle;
124         return me->id_to_uri(NULL, urid);
125 }
126
127
128 } // namespace ARDOUR
129