#include "video.h"
#include "storage.h"
#include <string>

#include <dynamic.h>
#ifdef Revision
#undef Revision
#endif
#define Revision "video.cpp $Revision: 1.1.1.1 $"

#ifdef HAVE_CONFIG_H
#include <conf_config.h>
#endif

	static Storage<std::string, Video_Interface*> _video_devices;
	static std::string last_error;

	bool video_create( std::string name, std::string driver, std::string * params, int params_num, bool replace )
	{
		if ( _video_devices.exist( name ) ) {
			if ( replace ) {
				Video_Interface *z;
				z=_video_devices.get( name );
				delete z;
				_video_devices.remove( name );
			} else {
				last_error = name + " already exist";
				return false;
			}
		}
		if ( !module_load( driver ) ) {
			last_error = dynamic_error();
			return false;
		}
		Dynamic_Module *mod;
		if ( ( mod = module_get ( driver ) ) == 0 ) {
			last_error = dynamic_error();
			return false;
		}
		if ( !mod->_symbol_load( "_get_video_driver" ) ) {
			module_remove( driver );
			last_error = dynamic_error();
			return false;
		}
		Video_Interface* ( *fun ) ( std::string * params, int params_num );
		if ( ( fun =( Video_Interface * ( * ) ( std::string *, int )) ( *mod ) [ "_get_video_driver" ] ) == 0 ) {
			module_remove( driver );
			mod->_symbol_remove( "_get_video_driver" );
			last_error = dynamic_error();
			return false;
		}
		Video_Interface *intf;
		intf=fun(params,params_num);
		last_error="";
		return _video_devices.insert( name, intf);
	}


	bool video_remove( std::string name )
	{
		last_error="";
		if ( !_video_devices.exist( name ) ) return true;
		Video_Interface *z;
		z=_video_devices.get( name );
		
		Dynamic_Module *mod;
		if ( ( mod = module_get ( z->_get_driver_name()) ) == 0 ) {
			last_error = dynamic_error();
		}
		else mod->_symbol_remove("_get_video_driver");
		module_remove( z->_get_driver_name() );
		return _video_devices.remove(name);
		
	}
	Video_Interface * video_get_interface( std::string name )
	{
		if ( _video_devices.exist( name ) ) return _video_devices.get( name );
		else return 0;
	}

	std::string video_error( )
	{
		return last_error;
	}
	std::string video_version( )
	{
		return VERSION;
	}


