diff options
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/jalv.c | 65 | ||||
-rw-r--r-- | src/jalv_internal.h | 9 | ||||
-rw-r--r-- | src/mapper.c | 92 | ||||
-rw-r--r-- | src/mapper.h | 44 | ||||
-rw-r--r-- | src/state.c | 25 | ||||
-rw-r--r-- | src/urids.c | 6 | ||||
-rw-r--r-- | src/urids.h | 4 |
8 files changed, 184 insertions, 62 deletions
diff --git a/meson.build b/meson.build index bb5d053..226b704 100644 --- a/meson.build +++ b/meson.build @@ -419,6 +419,7 @@ common_sources = files( 'src/jalv.c', 'src/log.c', 'src/lv2_evbuf.c', + 'src/mapper.c', 'src/nodes.c', 'src/process.c', 'src/state.c', @@ -10,6 +10,7 @@ #include "log.h" #include "lv2_evbuf.h" #include "macros.h" +#include "mapper.h" #include "nodes.h" #include "port.h" #include "state.h" @@ -36,7 +37,6 @@ #include "lv2/worker/worker.h" #include "serd/serd.h" #include "sratom/sratom.h" -#include "symap.h" #include "zix/allocator.h" #include "zix/attributes.h" #include "zix/filesystem.h" @@ -67,26 +67,6 @@ static ZixSem* exit_sem = NULL; ///< Exit semaphore used by signal handler -static LV2_URID -map_uri(LV2_URID_Map_Handle handle, const char* uri) -{ - Jalv* jalv = (Jalv*)handle; - zix_sem_wait(&jalv->symap_lock); - const LV2_URID id = symap_map(jalv->symap, uri); - zix_sem_post(&jalv->symap_lock); - return id; -} - -static const char* -unmap_uri(LV2_URID_Unmap_Handle handle, LV2_URID urid) -{ - Jalv* jalv = (Jalv*)handle; - zix_sem_wait(&jalv->symap_lock); - const char* uri = symap_unmap(jalv->symap, urid); - zix_sem_post(&jalv->symap_lock); - return uri; -} - /// These features have no data static const LV2_Feature static_features[] = { {LV2_STATE__loadDefaultState, NULL}, @@ -345,8 +325,11 @@ jalv_create_controls(Jalv* jalv, bool writable) } } - record = new_property_control( - jalv->world, property, &jalv->nodes, &jalv->map, &jalv->forge); + record = new_property_control(jalv->world, + property, + &jalv->nodes, + jalv_mapper_urid_map(jalv->mapper), + &jalv->forge); if (writable) { record->is_writable = true; @@ -405,7 +388,7 @@ jalv_send_to_plugin(void* const jalv_handle, jalv_log(JALV_LOG_ERR, "UI wrote with unsupported protocol %u (%s)\n", protocol, - unmap_uri(jalv, protocol)); + jalv_mapper_unmap_uri(jalv->mapper, protocol)); } if (st) { @@ -572,7 +555,7 @@ jalv_dump_atom(Jalv* const jalv, { if (jalv->opts.dump) { char* const str = sratom_to_turtle(jalv->sratom, - &jalv->unmap, + jalv_mapper_urid_unmap(jalv->mapper), "jalv:", NULL, NULL, @@ -776,14 +759,14 @@ static void jalv_init_features(Jalv* const jalv) { // urid:map - jalv->map.handle = jalv; - jalv->map.map = map_uri; - init_feature(&jalv->features.map_feature, LV2_URID__map, &jalv->map); + init_feature(&jalv->features.map_feature, + LV2_URID__map, + jalv_mapper_urid_map(jalv->mapper)); // urid:unmap - jalv->unmap.handle = jalv; - jalv->unmap.unmap = unmap_uri; - init_feature(&jalv->features.unmap_feature, LV2_URID__unmap, &jalv->unmap); + init_feature(&jalv->features.unmap_feature, + LV2_URID__unmap, + jalv_mapper_urid_unmap(jalv->mapper)); // state:makePath jalv->features.make_path.handle = jalv; @@ -936,8 +919,8 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) lilv_world_load_all(world); jalv->world = world; + jalv->mapper = jalv_mapper_new(); jalv->env = jalv_new_env(); - jalv->symap = symap_new(); jalv->block_length = 4096U; jalv->midi_buf_size = 1024U; jalv->msg_buf_size = 1024U; @@ -947,18 +930,19 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) jalv->log.urids = &jalv->urids; jalv->log.tracing = jalv->opts.trace; - zix_sem_init(&jalv->symap_lock, 1); zix_sem_init(&jalv->work_lock, 1); zix_sem_init(&jalv->done, 0); zix_sem_init(&jalv->paused, 0); - jalv_init_urids(jalv->symap, &jalv->urids); + jalv_init_urids(jalv->mapper, &jalv->urids); jalv_init_nodes(world, &jalv->nodes); jalv_init_features(jalv); - lv2_atom_forge_init(&jalv->forge, &jalv->map); + + LV2_URID_Map* const urid_map = jalv_mapper_urid_map(jalv->mapper); + lv2_atom_forge_init(&jalv->forge, urid_map); // Set up atom reading and writing environment - jalv->sratom = sratom_new(&jalv->map); + jalv->sratom = sratom_new(urid_map); sratom_set_env(jalv->sratom, jalv->env); // Create temporary directory for plugin state @@ -968,7 +952,7 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) } // Load initial state given in options if any - LilvState* state = initial_state(world, &jalv->map, jalv->opts.load); + LilvState* state = initial_state(world, urid_map, jalv->opts.load); if (jalv->opts.load && !state) { jalv_log(JALV_LOG_ERR, "Failed to load state from %s\n", jalv->opts.load); return -2; @@ -1017,7 +1001,7 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) LilvNode* preset = lilv_new_uri(jalv->world, jalv->opts.preset); jalv_load_presets(jalv, NULL, NULL); - state = lilv_state_new_from_world(jalv->world, &jalv->map, preset); + state = lilv_state_new_from_world(jalv->world, urid_map, preset); jalv->preset = state; lilv_node_free(preset); if (!state) { @@ -1033,7 +1017,7 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) if (!state) { // Not restoring state, load the plugin as a preset to get default state = lilv_state_new_from_world( - jalv->world, &jalv->map, lilv_plugin_get_uri(jalv->plugin)); + jalv->world, urid_map, lilv_plugin_get_uri(jalv->plugin)); } // Get a plugin UI @@ -1231,8 +1215,6 @@ jalv_close(Jalv* const jalv) for (LilvNode** n = (LilvNode**)&jalv->nodes; *n; ++n) { lilv_node_free(*n); } - symap_free(jalv->symap); - zix_sem_destroy(&jalv->symap_lock); #if USE_SUIL suil_host_free(jalv->ui_host); #endif @@ -1245,6 +1227,7 @@ jalv_close(Jalv* const jalv) sratom_free(jalv->sratom); serd_env_free(jalv->env); lilv_uis_free(jalv->uis); + jalv_mapper_free(jalv->mapper); lilv_world_free(jalv->world); zix_sem_destroy(&jalv->done); diff --git a/src/jalv_internal.h b/src/jalv_internal.h index dadd368..96e2c56 100644 --- a/src/jalv_internal.h +++ b/src/jalv_internal.h @@ -8,9 +8,9 @@ #include "control.h" #include "jalv_config.h" #include "log.h" +#include "mapper.h" #include "nodes.h" #include "options.h" -#include "symap.h" #include "types.h" #include "urids.h" #include "worker.h" @@ -65,17 +65,14 @@ typedef struct { /// Internal application state struct JalvImpl { JalvOptions opts; ///< Command-line options + LilvWorld* world; ///< Lilv World + JalvMapper* mapper; ///< URI mapper/unmapper JalvURIDs urids; ///< URIDs JalvNodes nodes; ///< Nodes JalvLog log; ///< Log for error/warning/debug messages LV2_Atom_Forge forge; ///< Atom forge - LilvWorld* world; ///< Lilv World - LV2_URID_Map map; ///< URI => Int map - LV2_URID_Unmap unmap; ///< Int => URI map SerdEnv* env; ///< Environment for RDF printing Sratom* sratom; ///< Atom serialiser - Symap* symap; ///< URI map - ZixSem symap_lock; ///< Lock for URI map JalvBackend* backend; ///< Audio system backend ZixRing* ui_to_plugin; ///< Port events from UI ZixRing* plugin_to_ui; ///< Port events from plugin diff --git a/src/mapper.c b/src/mapper.c new file mode 100644 index 0000000..27367e0 --- /dev/null +++ b/src/mapper.c @@ -0,0 +1,92 @@ +// Copyright 2012-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "mapper.h" + +#include "symap.h" + +#include "lv2/urid/urid.h" +#include "zix/sem.h" + +#include <stdint.h> +#include <stdlib.h> + +struct JalvMapperImpl { + Symap* symap; + ZixSem lock; + LV2_URID_Map map; + LV2_URID_Unmap unmap; +}; + +static LV2_URID +map_uri(LV2_URID_Map_Handle handle, const char* const uri) +{ + JalvMapper* const mapper = (JalvMapper*)handle; + zix_sem_wait(&mapper->lock); + + const LV2_URID id = symap_map(mapper->symap, uri); + + zix_sem_post(&mapper->lock); + return id; +} + +static const char* +unmap_uri(LV2_URID_Unmap_Handle handle, const LV2_URID urid) +{ + JalvMapper* const mapper = (JalvMapper*)handle; + zix_sem_wait(&mapper->lock); + + const char* const uri = symap_unmap(mapper->symap, urid); + + zix_sem_post(&mapper->lock); + return uri; +} + +JalvMapper* +jalv_mapper_new(void) +{ + JalvMapper* const mapper = (JalvMapper*)calloc(1, sizeof(JalvMapper)); + if (mapper) { + mapper->symap = symap_new(); + mapper->map.handle = mapper; + mapper->map.map = map_uri; + mapper->unmap.handle = mapper; + mapper->unmap.unmap = unmap_uri; + zix_sem_init(&mapper->lock, 1); + } + return mapper; +} + +void +jalv_mapper_free(JalvMapper* const mapper) +{ + if (mapper) { + zix_sem_destroy(&mapper->lock); + symap_free(mapper->symap); + free(mapper); + } +} + +LV2_URID_Map* +jalv_mapper_urid_map(JalvMapper* const mapper) +{ + return &mapper->map; +} + +LV2_URID_Unmap* +jalv_mapper_urid_unmap(JalvMapper* const mapper) +{ + return &mapper->unmap; +} + +LV2_URID +jalv_mapper_map_uri(JalvMapper* const mapper, const char* const sym) +{ + return symap_map(mapper->symap, sym); +} + +const char* +jalv_mapper_unmap_uri(const JalvMapper* mapper, uint32_t id) +{ + return symap_unmap(mapper->symap, id); +} diff --git a/src/mapper.h b/src/mapper.h new file mode 100644 index 0000000..d9cdf3e --- /dev/null +++ b/src/mapper.h @@ -0,0 +1,44 @@ +// Copyright 2012-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef JALV_MAPPER_H +#define JALV_MAPPER_H + +#include "attributes.h" + +#include "lv2/urid/urid.h" +#include "zix/attributes.h" + +// URI to URID mapping and unmapping +JALV_BEGIN_DECLS + +/// Opaque URI mapper implementation +typedef struct JalvMapperImpl JalvMapper; + +/// Allocate, configure, and return a new URI mapper +JalvMapper* +jalv_mapper_new(void); + +/// Free memory allocated by jalv_mapper_new() +void +jalv_mapper_free(JalvMapper* mapper); + +/// Return a pointer to the mapper's LV2 URID map +LV2_URID_Map* +jalv_mapper_urid_map(JalvMapper* mapper); + +/// Return a poitner to the mapper's LV2 URID unmap +LV2_URID_Unmap* +jalv_mapper_urid_unmap(JalvMapper* mapper); + +/// Map a URI string to a URID +LV2_URID +jalv_mapper_map_uri(JalvMapper* mapper, const char* sym); + +/// Unmap a URID back to a URI string if possible, or return NULL +ZIX_PURE_FUNC const char* +jalv_mapper_unmap_uri(const JalvMapper* mapper, LV2_URID id); + +JALV_END_DECLS + +#endif // JALV_MAPPER_H diff --git a/src/state.c b/src/state.c index b550c8f..0f83c2d 100644 --- a/src/state.c +++ b/src/state.c @@ -6,12 +6,14 @@ #include "comm.h" #include "jalv_internal.h" #include "log.h" +#include "mapper.h" #include "port.h" #include "string_utils.h" #include "lilv/lilv.h" #include "lv2/core/lv2.h" #include "lv2/state/state.h" +#include "lv2/urid/urid.h" #include "zix/attributes.h" #include "zix/ring.h" #include "zix/sem.h" @@ -51,12 +53,15 @@ get_port_value(const char* port_symbol, void jalv_save(Jalv* jalv, const char* dir) { + LV2_URID_Map* const map = jalv_mapper_urid_map(jalv->mapper); + LV2_URID_Unmap* const unmap = jalv_mapper_urid_unmap(jalv->mapper); + jalv->save_dir = jalv_strjoin(dir, "/"); LilvState* const state = lilv_state_new_from_instance(jalv->plugin, jalv->instance, - &jalv->map, + map, jalv->temp_dir, dir, dir, @@ -66,11 +71,8 @@ jalv_save(Jalv* jalv, const char* dir) LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE, NULL); - lilv_state_save( - jalv->world, &jalv->map, &jalv->unmap, state, NULL, dir, "state.ttl"); - + lilv_state_save(jalv->world, map, unmap, state, NULL, dir, "state.ttl"); lilv_state_free(state); - free(jalv->save_dir); jalv->save_dir = NULL; } @@ -145,7 +147,7 @@ set_port_value(const char* port_symbol, jalv_log(JALV_LOG_ERR, "Preset `%s' value has bad type <%s>\n", port_symbol, - jalv->unmap.unmap(jalv->unmap.handle, type)); + jalv_mapper_unmap_uri(jalv->mapper, type)); return; } @@ -215,7 +217,8 @@ int jalv_apply_preset(Jalv* jalv, const LilvNode* preset) { lilv_state_free(jalv->preset); - jalv->preset = lilv_state_new_from_world(jalv->world, &jalv->map, preset); + jalv->preset = lilv_state_new_from_world( + jalv->world, jalv_mapper_urid_map(jalv->mapper), preset); if (jalv->preset) { jalv_apply_state(jalv, jalv->preset); } @@ -229,10 +232,13 @@ jalv_save_preset(Jalv* jalv, const char* label, const char* filename) { + LV2_URID_Map* const map = jalv_mapper_urid_map(jalv->mapper); + LV2_URID_Unmap* const unmap = jalv_mapper_urid_unmap(jalv->mapper); + LilvState* const state = lilv_state_new_from_instance(jalv->plugin, jalv->instance, - &jalv->map, + map, jalv->temp_dir, dir, dir, @@ -246,8 +252,7 @@ jalv_save_preset(Jalv* jalv, lilv_state_set_label(state, label); } - int ret = lilv_state_save( - jalv->world, &jalv->map, &jalv->unmap, state, uri, dir, filename); + int ret = lilv_state_save(jalv->world, map, unmap, state, uri, dir, filename); lilv_state_free(jalv->preset); jalv->preset = state; diff --git a/src/urids.c b/src/urids.c index 022ab66..3d2da1c 100644 --- a/src/urids.c +++ b/src/urids.c @@ -3,7 +3,7 @@ #include "urids.h" -#include "symap.h" +#include "mapper.h" #include "lv2/atom/atom.h" #include "lv2/buf-size/buf-size.h" @@ -15,9 +15,9 @@ #include "lv2/ui/ui.h" void -jalv_init_urids(Symap* const symap, JalvURIDs* const urids) +jalv_init_urids(JalvMapper* const mapper, JalvURIDs* const urids) { -#define MAP_URI(uri) symap_map(symap, (uri)) +#define MAP_URI(uri) jalv_mapper_map_uri(mapper, (uri)) urids->atom_Chunk = MAP_URI(LV2_ATOM__Chunk); urids->atom_Float = MAP_URI(LV2_ATOM__Float); diff --git a/src/urids.h b/src/urids.h index 4cab740..50e6060 100644 --- a/src/urids.h +++ b/src/urids.h @@ -5,7 +5,7 @@ #define JALV_URIDS_H #include "attributes.h" -#include "symap.h" +#include "mapper.h" #include "lv2/urid/urid.h" @@ -48,7 +48,7 @@ typedef struct { } JalvURIDs; void -jalv_init_urids(Symap* symap, JalvURIDs* urids); +jalv_init_urids(JalvMapper* mapper, JalvURIDs* urids); JALV_END_DECLS |