From 9977d659105937112587e7eb68c59fae7169d945 Mon Sep 17 00:00:00 2001
From: David Robillard A local file path string. This is identical in format to atom:String,
+except the string is a path. Since the ability to distinguish paths from plain
+strings is often necessary, paths MUST NOT be transmitted as atom:String. A URI string. This is identical in format to atom:String, except the string
-is a URI. This is occasionally useful when a URI is needed but mapping it is
-inappropriate. URIs should never be expressed as atom:String, because this
-makes it impossible to distinguish URIs from arbitrary strings, which makes
-functionality like mapping or rewriting URIs impossible. Similarly, there is
-deliberately no atom type for filesystem paths. To refer to a filename, use a
-complete file URI, with hostname if at all possible. This allows hosts to
-properly handle paths in situations like serialisation or network
-transmission. Pedantically, this is an IRI
, since UTF-8 characters are allowed.
This is not strictly a URI, since UTF-8 is allowed. Escaping and related +issues issues are the host's responsibility.
""" . atom:URID diff --git a/lv2/lv2plug.in/ns/ext/atom/forge.h b/lv2/lv2plug.in/ns/ext/atom/forge.h index 4db9638..06f24f5 100644 --- a/lv2/lv2plug.in/ns/ext/atom/forge.h +++ b/lv2/lv2plug.in/ns/ext/atom/forge.h @@ -85,6 +85,7 @@ typedef struct { LV2_URID Int32; LV2_URID Int64; LV2_URID Literal; + LV2_URID Path; LV2_URID Property; LV2_URID Resource; LV2_URID Sequence; @@ -172,6 +173,7 @@ lv2_atom_forge_init(LV2_Atom_Forge* forge, LV2_URID_Map* map) forge->Int32 = map->map(map->handle, LV2_ATOM_URI "#Int32"); forge->Int64 = map->map(map->handle, LV2_ATOM_URI "#Int64"); forge->Literal = map->map(map->handle, LV2_ATOM_URI "#Literal"); + forge->Path = map->map(map->handle, LV2_ATOM_URI "#Path"); forge->Property = map->map(map->handle, LV2_ATOM_URI "#Property"); forge->Resource = map->map(map->handle, LV2_ATOM_URI "#Resource"); forge->Sequence = map->map(map->handle, LV2_ATOM_URI "#Sequence"); @@ -322,13 +324,31 @@ lv2_atom_forge_uri(LV2_Atom_Forge* forge, const LV2_Atom_String a = { { forge->URI, len + 1 } }; LV2_Atom_String* out = (LV2_Atom_String*) lv2_atom_forge_write_nopad(forge, &a, sizeof(a)); - if (!out) { - return NULL; + if (out) { + if (!lv2_atom_forge_string_body(forge, uri, len)) { + out->atom.type = 0; + out->atom.size = 0; + out = NULL; + } } - if (!lv2_atom_forge_string_body(forge, uri, len)) { - out->atom.type = 0; - out->atom.size = 0; - return NULL; + return out; +} + +/** Write an atom:Path. Note that @p path need not be NULL terminated. */ +static inline LV2_Atom_String* +lv2_atom_forge_path(LV2_Atom_Forge* forge, + const uint8_t* path, + size_t len) +{ + const LV2_Atom_String a = { { forge->Path, len + 1 } }; + LV2_Atom_String* out = (LV2_Atom_String*) + lv2_atom_forge_write_nopad(forge, &a, sizeof(a)); + if (out) { + if (!lv2_atom_forge_string_body(forge, path, len)) { + out->atom.type = 0; + out->atom.size = 0; + out = NULL; + } } return out; } diff --git a/lv2/lv2plug.in/ns/ext/message/message.h b/lv2/lv2plug.in/ns/ext/message/message.h index 9c03713..728b836 100644 --- a/lv2/lv2plug.in/ns/ext/message/message.h +++ b/lv2/lv2plug.in/ns/ext/message/message.h @@ -27,21 +27,20 @@ #define LV2_MESSAGE_URI "http://lv2plug.in/ns/ext/message" -#define LV2_MESSAGE_Ack LV2_MESSAGE_URI "#Ack" -#define LV2_MESSAGE_Delete LV2_MESSAGE_URI "#Delete" -#define LV2_MESSAGE_Error LV2_MESSAGE_URI "#Error" -#define LV2_MESSAGE_Get LV2_MESSAGE_URI "#Get" -#define LV2_MESSAGE_Message LV2_MESSAGE_URI "#Message" -#define LV2_MESSAGE_Move LV2_MESSAGE_URI "#Move" -#define LV2_MESSAGE_Patch LV2_MESSAGE_URI "#Patch" -#define LV2_MESSAGE_Post LV2_MESSAGE_URI "#Post" -#define LV2_MESSAGE_Put LV2_MESSAGE_URI "#Put" -#define LV2_MESSAGE_Request LV2_MESSAGE_URI "#Request" -#define LV2_MESSAGE_Response LV2_MESSAGE_URI "#Response" -#define LV2_MESSAGE_Set LV2_MESSAGE_URI "#Set" - -#define LV2_MESSAGE_add LV2_MESSAGE_URI "#add" -#define LV2_MESSAGE_body LV2_MESSAGE_URI "#body" -#define LV2_MESSAGE_cause LV2_MESSAGE_URI "#cause" +#define LV2_MESSAGE__Ack LV2_MESSAGE_URI "#Ack" +#define LV2_MESSAGE__Delete LV2_MESSAGE_URI "#Delete" +#define LV2_MESSAGE__Error LV2_MESSAGE_URI "#Error" +#define LV2_MESSAGE__Get LV2_MESSAGE_URI "#Get" +#define LV2_MESSAGE__Message LV2_MESSAGE_URI "#Message" +#define LV2_MESSAGE__Move LV2_MESSAGE_URI "#Move" +#define LV2_MESSAGE__Patch LV2_MESSAGE_URI "#Patch" +#define LV2_MESSAGE__Post LV2_MESSAGE_URI "#Post" +#define LV2_MESSAGE__Put LV2_MESSAGE_URI "#Put" +#define LV2_MESSAGE__Request LV2_MESSAGE_URI "#Request" +#define LV2_MESSAGE__Response LV2_MESSAGE_URI "#Response" +#define LV2_MESSAGE__Set LV2_MESSAGE_URI "#Set" +#define LV2_MESSAGE__add LV2_MESSAGE_URI "#add" +#define LV2_MESSAGE__body LV2_MESSAGE_URI "#body" +#define LV2_MESSAGE__cause LV2_MESSAGE_URI "#cause" #endif /* LV2_MESSAGE_H */ diff --git a/lv2/lv2plug.in/ns/ext/state/state.h b/lv2/lv2plug.in/ns/ext/state/state.h index b450b93..aa272b7 100644 --- a/lv2/lv2plug.in/ns/ext/state/state.h +++ b/lv2/lv2plug.in/ns/ext/state/state.h @@ -36,10 +36,11 @@ extern "C" { #define LV2_STATE_URI "http://lv2plug.in/ns/ext/state" -#define LV2_STATE_INTERFACE_URI LV2_STATE_URI "#Interface" -#define LV2_STATE_PATH_URI LV2_STATE_URI "#Path" -#define LV2_STATE_MAP_PATH_URI LV2_STATE_URI "#mapPath" -#define LV2_STATE_MAKE_PATH_URI LV2_STATE_URI "#makePath" +#define LV2_STATE__Interface LV2_STATE_URI "#Interface" +#define LV2_STATE__State LV2_STATE_URI "#State" +#define LV2_STATE__state LV2_STATE_URI "#state" +#define LV2_STATE__mapPath LV2_STATE_URI "#mapPath" +#define LV2_STATE__makePath LV2_STATE_URI "#makePath" typedef void* LV2_State_Handle; typedef void* LV2_State_Map_Path_Handle; @@ -315,7 +316,7 @@ typedef struct { } LV2_State_Map_Path; /** - Feature data for state:makePath (@ref LV2_STATE_MAKE_PATH_URI). + Feature data for state:makePath (@ref LV2_STATE__makePath). */ typedef struct { diff --git a/lv2/lv2plug.in/ns/ext/state/state.ttl b/lv2/lv2plug.in/ns/ext/state/state.ttl index 6c0656a..4b86eb6 100644 --- a/lv2/lv2plug.in/ns/ext/state/state.ttl +++ b/lv2/lv2plug.in/ns/ext/state/state.ttl @@ -193,7 +193,9 @@ Map get_plugin_state(LV2_Handle instance)Plugins may need to refer to files (e.g. loaded samples) in their state. This is done by storing the file's path as a property just like any other value. However, there are some rules which MUST be followed when storing -paths, see state:mapPath for details.
+paths, see state:mapPath for details. Plugins MUST +use the type atom:Path +for all paths in their state.This feature maps absolute paths to/from abstract paths
which are
stored in state. To support this feature a host must pass an LV2_Feature with
URI LV2_STATE_MAP_PATH_URI and data pointed to an LV2_State_Map_Path to the
@@ -305,7 +306,6 @@ state:makePath
a lv2:Feature ;
rdfs:label "Support for creating new files and directories" ;
lv2:documentation """
-
This feature allows plugins to create new files and/or directories. To support this feature the host passes an LV2_Feature with URI LV2_STATE_MAKE_PATH_URI and data pointed to an LV2_State_Make_Path to the @@ -340,20 +340,3 @@ char* save_myfile(LV2_State_Make_Path* make_path) } """ . - -state:Path - a rdfs:Class ; - rdfs:label "Path" ; - lv2:documentation """ -
A path to a file or directory.
- -The format of a state:Path is a C string, possibly escaped or otherwise
-restricted in a system-specific manner. This URI (LV2_STATE_PATH_URI), mapped
-to an integer, MUST be used as the type
parameter for any files
-passed to the LV2_State_Store_Function; and will likewise be returned by the
-corresponding call to the LV2_State_Retrieve_Function.
When storing and retrieving a path, the plugin MUST NOT assume the same path -will be restored. However, the restored path will refer to a file with -equivalent contents to the original.
-""" . diff --git a/plugins/eg-sampler.lv2/sampler.c b/plugins/eg-sampler.lv2/sampler.c index 904f026..043f629 100644 --- a/plugins/eg-sampler.lv2/sampler.c +++ b/plugins/eg-sampler.lv2/sampler.c @@ -63,11 +63,10 @@ enum { static const char* default_sample_file = "monosample.wav"; typedef struct { - SF_INFO info; /**< Info about sample from sndfile */ - float* data; /**< Sample data in float */ - char* uri; /**< URI of file */ - const char* path; /**< Path of file (pointer into uri) */ - size_t uri_len; /**< Length of uri. */ + SF_INFO info; /**< Info about sample from sndfile */ + float* data; /**< Sample data in float */ + char* path; /**< Path of file */ + size_t path_len; /**< Length of path */ } Sample; typedef struct { @@ -110,41 +109,10 @@ typedef struct { Sample* sample; } SampleMessage; -static bool -parse_file_uri(const char* uri, - const char** host, size_t* host_len, - const char** path, size_t* path_len) -{ - if (strncmp(uri, "file://", strlen("file://"))) { - return false; - } - - *host = uri + strlen("file://"); - const char* host_end = *host; - for (; *host_end && *host_end != '/'; ++host_end) {} - - *host_len = host_end - *host; - *path = host_end; - *path_len = (uri + strlen(uri)) - host_end; - - return true; -} - static Sample* -load_sample(Sampler* plugin, const char* uri) +load_sample(Sampler* plugin, const char* path) { - const size_t uri_len = strlen(uri); - const char* host = NULL; - const char* path = NULL; - size_t host_len = 0; - size_t path_len = 0; - if (!parse_file_uri(uri, &host, &host_len, &path, &path_len)) { - fprintf(stderr, "Request to load bad file URI %s\n", uri); - return NULL; - } - - /* Probably should check if the host is local here, but we'll just - blissfully attempt to load the path on this machine... */ + const size_t path_len = strlen(path); printf("Loading sample %s\n", path); Sample* const sample = (Sample*)malloc(sizeof(Sample)); @@ -168,11 +136,10 @@ load_sample(Sampler* plugin, const char* uri) sf_close(sndfile); /* Fill sample struct and return it. */ - sample->data = data; - sample->uri = (char*)malloc(uri_len + 1); - sample->path = sample->uri + (path - uri); - sample->uri_len = uri_len; - memcpy(sample->uri, uri, uri_len + 1); + sample->data = data; + sample->path = (char*)malloc(path_len + 1); + sample->path_len = path_len; + memcpy(sample->path, path, path_len + 1); return sample; } @@ -181,14 +148,14 @@ static bool handle_set_message(Sampler* plugin, const LV2_Atom_Object* obj) { - /* Get file URI from message */ - const LV2_Atom* file_uri = get_msg_file_uri(&plugin->uris, obj); - if (!file_uri) { + /* Get file path from message */ + const LV2_Atom* file_path = get_msg_file_path(&plugin->uris, obj); + if (!file_path) { return false; } /* Load sample. */ - Sample* sample = load_sample(plugin, LV2_ATOM_BODY(file_uri)); + Sample* sample = load_sample(plugin, LV2_ATOM_BODY(file_path)); if (sample) { /* Loaded sample, send it to run() to be applied. */ const SampleMessage msg = { @@ -206,10 +173,12 @@ handle_set_message(Sampler* plugin, void free_sample(Sample* sample) { - fprintf(stderr, "Freeing %s\n", sample->uri); - free(sample->uri); - free(sample->data); - free(sample); + if (sample) { + fprintf(stderr, "Freeing %s\n", sample->path); + free(sample->path); + free(sample->data); + free(sample); + } } void* @@ -441,23 +410,10 @@ run(LV2_Handle instance, plugin->sample = m.sample; /* Send a notification that we're using a new sample. */ - lv2_atom_forge_audio_time(&plugin->forge, 0, 0); - - LV2_Atom_Forge_Frame set_frame; - lv2_atom_forge_blank(&plugin->forge, &set_frame, 1, uris->msg_Set); - - lv2_atom_forge_property_head(&plugin->forge, uris->msg_body, 0); - LV2_Atom_Forge_Frame body_frame; - lv2_atom_forge_blank(&plugin->forge, &body_frame, 2, 0); - - lv2_atom_forge_property_head(&plugin->forge, uris->eg_file, 0); - lv2_atom_forge_uri(&plugin->forge, - (const uint8_t*)plugin->sample->uri, - plugin->sample->uri_len); - - lv2_atom_forge_pop(&plugin->forge, &body_frame); - lv2_atom_forge_pop(&plugin->forge, &set_frame); + write_set_filename_msg(&plugin->forge, uris, + plugin->sample->path, + plugin->sample->path_len); } else { fprintf(stderr, "Unknown message from worker\n"); @@ -476,7 +432,7 @@ save(LV2_Handle instance, { LV2_State_Map_Path* map_path = NULL; for (int i = 0; features[i]; ++i) { - if (!strcmp(features[i]->URI, LV2_STATE_MAP_PATH_URI)) { + if (!strcmp(features[i]->URI, LV2_STATE__mapPath)) { map_path = (LV2_State_Map_Path*)features[i]->data; } } @@ -489,7 +445,7 @@ save(LV2_Handle instance, plugin->uris.eg_file, apath, strlen(plugin->sample->path) + 1, - plugin->uris.state_Path, + plugin->uris.atom_Path, LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE); free(apath); diff --git a/plugins/eg-sampler.lv2/sampler_ui.c b/plugins/eg-sampler.lv2/sampler_ui.c index 056f0bb..2c039a5 100644 --- a/plugins/eg-sampler.lv2/sampler_ui.c +++ b/plugins/eg-sampler.lv2/sampler_ui.c @@ -75,46 +75,18 @@ on_load_clicked(GtkWidget* widget, /* Got what we need, destroy the dialog. */ gtk_widget_destroy(dialog); - /* Build a complete file URI with hostname, e.g. file://hal/home/me/foo.wav - This is to precisely specify the file location, even if the plugin is - running on a different host (which is entirely possible). Plugins - should do this even though some hosts may not support such setups. - */ - const char* hostname = g_get_host_name(); - char* file_uri = g_filename_to_uri(filename, hostname, NULL); - const size_t file_uri_len = strlen(file_uri); - #define OBJ_BUF_SIZE 1024 uint8_t obj_buf[OBJ_BUF_SIZE]; lv2_atom_forge_set_buffer(&ui->forge, obj_buf, OBJ_BUF_SIZE); - /* Send [ - * a msg:Set ; - * msg:body [ - * eg-sampler:filename