
/* LiViDO host loader example
 *
 * this code is describing how to access a livido plugin
 * make it with:
 * $ gcc $CFLAGS -o host_example host_example.c
 * $ ld -E -z now -shared plugin_host.o -o plugin_host.so
 *
 * (C) 2004 Denis "jaromil" Rojo - http://rastasoft.org
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This source code 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.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: host_example.c,v 1.2 2004/04/06 08:39:40 jaromil Exp $
 *
 */

/** @file host_example.c
    
    @brief LiViDO loader host implementation example
    
    @todo comment the loader host example
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#include <livido.h>


int main(int argc, char **argv) {

  void *plugin_handle;
  livido_setup_f *plugin_setup;
  livido_class_t *plugin_class;
  livido_instance_t *plugin_instance;
  livido_parameter_t *plugin_param;

  if(argc<2) {
    fprintf(stderr,"we miss an argument: a livido plugin.so file\n");
    exit(0);
  }

  plugin_handle = dlopen(argv[1],RTLD_NOW);
  if(!plugin_handle) {
    fprintf(stderr,"error on dlopen :%s : %s\n",argv[1],dlerror());
    exit(0);
  }

  plugin_setup = dlsym(plugin_handle,"livido_setup");
  if(!plugin_setup) {
    fprintf(stderr,"error on dlsym :livido_setup : %s\n",dlerror());
    exit(0);
  }

  plugin_class = (*plugin_setup)();
  if(!plugin_class) {
    fprintf(stderr,"error calling livido_setup");
    exit(0);
  } 

  while(plugin_class->name != NULL) {

    fprintf(stderr,"found plugin class %s version %u\n",
      plugin_class->name, plugin_class->version);
    fprintf(stderr,"%s, by %s\n\n",
      plugin_class->desc, plugin_class->author);

    plugin_param = plugin_class->parameters;

    // browse parameters
    while(plugin_param->name != NULL) {
	    // arg, this switch wouldn't be needed in c++ :( 
      switch(plugin_param->type) {
	case LVD_PARAM_BOOL:
	  fprintf(stderr,"parameter \"%s\" default %u\n",
	          plugin_param->name,
		  *(int*)plugin_param->def);
	break;

        case LVD_PARAM_INT:
          fprintf(stderr,"parameter \"%s\" min %i max %i default %i\n",
                  plugin_param->name,
	          *(int*)plugin_param->min,
	          *(int*)plugin_param->max,
	          *(int*)plugin_param->def);
      	break;

        case LVD_PARAM_FLOAT:
	  fprintf(stderr,"parameter \"%s\" min %.2f max %.2f default %.2f\n",
                  plugin_param->name,
	          *(float*)plugin_param->min,
	          *(float*)plugin_param->max,
	          *(float*)plugin_param->def);
	break;

	case LVD_PARAM_COLOR:
	  fprintf(stderr,"parameter \"%s\" default #%X%X%X\n",
	          plugin_param->name,
		  ((unsigned int*)plugin_param->def)[0],
		  ((unsigned int*)plugin_param->def)[1],
		  ((unsigned int*)plugin_param->def)[2]);
        break;

	case LVD_PARAM_COORD:
	  fprintf(stderr,"parameter \"%s\" min %i,%i max %i,%i default %i,%i\n",
	          plugin_param->name,
		  ((int*)plugin_param->min)[0],((int*)plugin_param->min)[1],
		  ((int*)plugin_param->max)[0],((int*)plugin_param->max)[1],
		  ((int*)plugin_param->def)[0],((int*)plugin_param->def)[1]);
	break;

	case LVD_PARAM_STRING:
	  fprintf(stderr,"parameter \"%s\" default %s\n",
	          plugin_param->name, (char*)plugin_param->def);
	break;

	case LVD_PARAM_DATA:
	  fprintf(stderr,"parameter \"%s\" default %p\n",
	          plugin_param->name, plugin_param->def);
	break;

      }

      fprintf(stderr,"%s\n\n",plugin_param->hint);
      plugin_param++;
    }

    fprintf(stderr,"proceed to test plugin class methods:\n");

    LIVIDO_CREATE_INSTANCE(plugin_class,plugin_instance);
    fprintf(stderr,"new filter instance created with address %p\n",
		    plugin_instance);
   
    fprintf(stderr,"initializing filter instance %p\n",plugin_instance);
    plugin_instance = (*plugin_class->init)(plugin_instance);
    
    fprintf(stderr,"calling filter process:\n");
    (*plugin_class->process)(plugin_instance,NULL,NULL);
    
    fprintf(stderr,"releasing instance\n");
    (*plugin_class->deinit)(plugin_instance);

    plugin_class++; // there can be more classes
  }

  fprintf(stderr,"\n\n\nfin qui, tutto bene! :)\n");
  exit(1);
}

