Ejemplo n.º 1
0
SLV2Instance
slv2_plugin_instantiate(SLV2Plugin               plugin,
                        double                   sample_rate,
                        const LV2_Feature*const* features)
{
	struct _Instance* result = NULL;

	const LV2_Feature** local_features = NULL;
	if (features == NULL) {
		local_features = malloc(sizeof(LV2_Feature));
		local_features[0] = NULL;
	}

	const char* const lib_uri = slv2_value_as_uri(slv2_plugin_get_library_uri(plugin));
	const char* const lib_path = slv2_uri_to_path(lib_uri);

	if (!lib_path)
		return NULL;

	dlerror();
	void* lib = dlopen(lib_path, RTLD_NOW);
	if (!lib) {
		fprintf(stderr, "Unable to open library %s (%s)\n", lib_path, dlerror());
		return NULL;
	}

	LV2_Descriptor_Function df = dlsym(lib, "lv2_descriptor");

	if (!df) {
		fprintf(stderr, "Could not find symbol 'lv2_descriptor', "
				"%s is not a LV2 plugin.\n", lib_path);
		dlclose(lib);
		return NULL;
	} else {
		// Search for plugin by URI

		// FIXME: Kludge to get bundle path (containing directory of binary)
		const char* bundle_path = slv2_uri_to_path(slv2_value_as_uri(
					slv2_plugin_get_bundle_uri(plugin)));

		//printf("Bundle path: %s\n", bundle_path);

		for (uint32_t i=0; 1; ++i) {

			const LV2_Descriptor* ld = df(i);

			if (!ld) {
				fprintf(stderr, "Did not find plugin %s in %s\n",
						slv2_value_as_uri(slv2_plugin_get_uri(plugin)), lib_path);
				dlclose(lib);
				break; // return NULL
			} else if (!strcmp(ld->URI, slv2_value_as_uri(slv2_plugin_get_uri(plugin)))) {

				assert(plugin->plugin_uri);

				//printf("Found %s at index %u in:\n\t%s\n\n",
				//		librdf_uri_as_string(plugin->plugin_uri), i, lib_path);

				assert(ld->instantiate);

				// Create SLV2Instance to return
				result = malloc(sizeof(struct _Instance));
				result->lv2_descriptor = ld;
                result->lv2_handle = ld->instantiate(ld, sample_rate, (char*)bundle_path,
						(features) ? features : local_features);
                struct _InstanceImpl* impl = malloc(sizeof(struct _InstanceImpl));
				impl->lib_handle = lib;
				result->pimpl = impl;

				break;
			}
		}
	}

	if (result) {
		assert(slv2_plugin_get_num_ports(plugin) > 0);

		// Failed to instantiate
		if (result->lv2_handle == NULL) {
			//printf("Failed to instantiate %s\n", plugin->plugin_uri);
			free(result);
			return NULL;
		}

		// "Connect" all ports to NULL (catches bugs)
		for (uint32_t i=0; i < slv2_plugin_get_num_ports(plugin); ++i)
			result->lv2_descriptor->connect_port(result->lv2_handle, i, NULL);
	}

	free(local_features);

	return result;
}
Ejemplo n.º 2
0
SLV2UIInstance
slv2_ui_instantiate(SLV2Plugin                     plugin,
                    SLV2UI                         ui,
                    LV2UI_Write_Function           write_function,
                    LV2UI_Controller               controller,
                    const LV2_Feature* const*      features)
{
    struct _SLV2UIInstance* result = NULL;

    bool local_features = (features == NULL);
    if (local_features) {
        features = malloc(sizeof(LV2_Feature));
        ((LV2_Feature**)features)[0] = NULL;
    }

    const char* const lib_uri = slv2_value_as_string(slv2_ui_get_binary_uri(ui));
    const char* const lib_path = slv2_uri_to_path(lib_uri);

    if (!lib_path)
        return NULL;

    dlerror();
    void* lib = dlopen(lib_path, RTLD_NOW);
    if (!lib) {
        fprintf(stderr, "Unable to open UI library %s (%s)\n", lib_path, dlerror());
        return NULL;
    }

    LV2UI_DescriptorFunction df = dlsym(lib, "lv2ui_descriptor");

    if (!df) {
        fprintf(stderr, "Could not find symbol 'lv2ui_descriptor', "
                "%s is not a LV2 plugin UI.\n", lib_path);
        dlclose(lib);
        return NULL;
    } else {

        const char* bundle_path = slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_bundle_uri(ui)));

        for (uint32_t i=0; 1; ++i) {

            const LV2UI_Descriptor* ld = df(i);

            if (!ld) {
                fprintf(stderr, "Did not find UI %s in %s\n",
                        slv2_value_as_uri(slv2_ui_get_uri(ui)), lib_path);
                dlclose(lib);
                break; // return NULL
            } else if (!strcmp(ld->URI, slv2_value_as_uri(slv2_ui_get_uri(ui)))) {

                assert(plugin->plugin_uri);

                printf("Found UI %s at index %u in:\n\t%s\n\n",
                       slv2_value_as_uri(plugin->plugin_uri), i, lib_path);

                assert(ld->instantiate);

                // Create SLV2UIInstance to return
                result = malloc(sizeof(struct _SLV2UIInstance));
                struct _SLV2UIInstanceImpl* impl = malloc(sizeof(struct _SLV2UIInstanceImpl));
                impl->lv2ui_descriptor = ld;
                impl->lv2ui_handle = ld->instantiate(ld,
                                                     slv2_value_as_uri(slv2_plugin_get_uri(plugin)),
                                                     (char*)bundle_path,
                                                     write_function,
                                                     controller,
                                                     &impl->widget,
                                                     features);
                impl->lib_handle = lib;
                result->pimpl = impl;
                break;
            }
        }
    }


    // Failed to instantiate
    if (result == NULL || result->pimpl->lv2ui_handle == NULL) {
        //printf("Failed to instantiate %s\n", plugin->plugin_uri);
        free(result);
        return NULL;
    }

    // Failed to create a widget, but still got a handle - this means that
    // the plugin is buggy
    if (result->pimpl->widget == NULL) {
        slv2_ui_instance_free(result);
        return NULL;
    }

    if (local_features)
        free((LV2_Feature**)features);

    return result;
}