/* 
 * host.c
 *
 * simple program showing how LADSPA v1.2 extended port info can be
 * evaluated.
 */
#include <stdio.h>
#include <dlfcn.h>

#include "ladspa.h"

/* this function shows host access to v1.2 extensions, given a plugin
 * descriptor structure.
 */
void evaluate_descriptor (const LADSPA_Descriptor * d)
{
	const LADSPA_PortValueEnum * e;
	const LADSPA_PortInfo * info;

	unsigned long i;

	/* iterate all ports and show the extra information v1.2 provides. */
	for (info = d->PortInfo, i = 0; i < d->PortCount; ++i, ++info)
	{
		printf ("--------------------------------------------\n");
		printf ("port: %s\n", d->PortNames[i]);

		printf (" default: %.1f\n", info->Default);
		
		/* libc handles char * NULL for us here. */
		printf (" unit: %s\n", info->Unit); 

		/* iterate this port's value <-> label enumeration */
		for (e = info->ValueEnum; e && e->Label; ++e)
			printf ("  value: %.2f = %s\n", e->Value, e->Label);
	}

}

/* the rest of this program is the usual plugin loading and descriptor
 * loading work, nothing new, just your daily routine.
 */
void quit (char * s, int c)
{
	fprintf (c ? stderr : stdout, s);
	exit (c);
}

int main (int argc, char ** argv)
{
	void * dlhandle;
	LADSPA_Descriptor_Function get_descriptor;
	const LADSPA_Descriptor * d;

	if (argc != 2)
		quit ("give me a plugin location\n", 1);

	dlhandle = dlopen (argv[1], RTLD_LAZY);
	if (!dlhandle)
	{
		fprintf (stderr, "dlopen error: %s\n", dlerror());
		quit ("can't open shared object\n", 2);
	}
	
	get_descriptor = (LADSPA_Descriptor_Function) 
		dlsym (dlhandle, "ladspa_descriptor");
	
	if (!get_descriptor)
		quit ("no ladspa_descriptor symbol found\n", 3);

	d = get_descriptor (0);
	if (d == 0)
		quit ("no plugin found\n", 4);
	
	/* now we have the plugin descriptor, let's go to work. */
	printf ("properties: 0x%x\n", d->Properties);

	/* evaluate API version first. */
	if (!LADSPA_HAS_VERSION (d->Properties))
		quit ("plugin is not aware of versioning\n", 0);

	printf ("version: %d.%d\n", d->Version.major, d->Version.minor);
	if (d->Version.major < 2 && d->Version.minor < 2)
		quit ("plugin is v1.1 or before, quitting\n", 0);

	evaluate_descriptor (d);

	return 0;
}

