X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Furi_map.cc;h=163a460624fe8e544d3a2b6ad5a02963a2c33d9f;hb=07112b55e0bb7ceb9e5c05ab4df167ecaf7edd9b;hp=82869de1343f8d44a4e059be19d4a90c43d74b48;hpb=b65f8073ba306ac2d85133875746767e7c6b0eb6;p=ardour.git diff --git a/libs/ardour/uri_map.cc b/libs/ardour/uri_map.cc index 82869de134..163a460624 100644 --- a/libs/ardour/uri_map.cc +++ b/libs/ardour/uri_map.cc @@ -1,74 +1,111 @@ -/* This file is part of Ingen. - * Copyright (C) 2008 Dave Robillard - * - * Ingen is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 2 of the License, or (at your option) any later - * version. - * - * Ingen is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#define __STDC_LIMIT_MACROS 1 +/* + Copyright (C) 2008-2011 Paul Davis + Author: David Robillard + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + #include -#include +#include +#include + #include -#include "ardour/uri_map.h" +#include -using namespace std; +#include "pbd/error.h" + +#include "ardour/uri_map.h" namespace ARDOUR { - -URIMap::URIMap() - : next_uri_id(1) +static uint32_t +c_uri_map_uri_to_id(LV2_URI_Map_Callback_Data callback_data, + const char* map, + const char* uri) { - uri_map_feature_data.uri_to_id = &URIMap::uri_map_uri_to_id; - uri_map_feature_data.callback_data = this; - uri_map_feature.URI = LV2_URI_MAP_URI; - uri_map_feature.data = &uri_map_feature_data; + URIMap* const me = (URIMap*)callback_data; + const uint32_t id = me->uri_to_id(uri); + + /* The event context with the uri-map extension guarantees a value in the + range of uint16_t. Ardour used to map to a separate range to achieve + this, but unfortunately some plugins are broken and use the incorrect + context. To compensate, we simply use the same context for everything + and hope that anything in the event context gets mapped before + UINT16_MAX is reached (which will be fine unless something seriously + weird is going on). If this fails there is nothing we can do, die. + */ + assert(!map || strcmp(map, "http://lv2plug.in/ns/ext/event") + || id < UINT16_MAX); + + return id; } - -uint32_t -URIMap::uri_to_id(const char* map, - const char* uri) +static LV2_URID +c_urid_map(LV2_URID_Map_Handle handle, + const char* uri) { - return uri_map_uri_to_id(this, map, uri); + URIMap* const me = (URIMap*)handle; + return me->uri_to_id(uri); } +static const char* +c_urid_unmap(LV2_URID_Unmap_Handle handle, + LV2_URID urid) +{ + URIMap* const me = (URIMap*)handle; + return me->id_to_uri(urid); +} + +URIMap::URIMap() +{ + _uri_map_feature_data.uri_to_id = c_uri_map_uri_to_id; + _uri_map_feature_data.callback_data = this; + _uri_map_feature.URI = LV2_URI_MAP_URI; + _uri_map_feature.data = &_uri_map_feature_data; + + _urid_map_feature_data.map = c_urid_map; + _urid_map_feature_data.handle = this; + _urid_map_feature.URI = LV2_URID_MAP_URI; + _urid_map_feature.data = &_urid_map_feature_data; + + _urid_unmap_feature_data.unmap = c_urid_unmap; + _urid_unmap_feature_data.handle = this; + _urid_unmap_feature.URI = LV2_URID_UNMAP_URI; + _urid_unmap_feature.data = &_urid_unmap_feature_data; +} uint32_t -URIMap::uri_map_uri_to_id(LV2_URI_Map_Callback_Data callback_data, - const char* /*map*/, - const char* uri) +URIMap::uri_to_id(const char* uri) { - // TODO: map ignored, < UINT16_MAX assumed - - URIMap* me = (URIMap*)callback_data; - uint32_t ret = 0; - - Map::iterator i = me->uri_map.find(uri); - if (i != me->uri_map.end()) { - ret = i->second; - } else { - ret = me->next_uri_id++; - me->uri_map.insert(make_pair(string(uri), ret)); + const std::string urimm(uri); + const Map::const_iterator i = _map.find(urimm); + if (i != _map.end()) { + return i->second; } - - /*cout << "URI MAP (" << (map ? (void*)map : NULL) - << "): " << uri << " -> " << ret << endl;*/ - - assert(ret <= UINT16_MAX); - return ret; + const uint32_t id = _map.size() + 1; + _map.insert(std::make_pair(urimm, id)); + _unmap.insert(std::make_pair(id, urimm)); + return id; } +const char* +URIMap::id_to_uri(const uint32_t id) const +{ + const Unmap::const_iterator i = _unmap.find(id); + return (i != _unmap.end()) ? i->second.c_str() : NULL; +} } // namespace ARDOUR