File diff r7192:543ddd0c4867 → r7193:2cae66b95848
src/driver.h
Show inline comments
 
@@ -13,14 +13,12 @@
 

	
 
bool GetDriverParamBool(const char* const* parm, const char* name);
 
int GetDriverParamInt(const char* const* parm, const char* name, int def);
 

	
 
class Driver {
 
public:
 
	virtual bool CanProbe() = 0;
 

	
 
	virtual const char *Start(const char * const *parm) = 0;
 

	
 
	virtual void Stop() = 0;
 

	
 
	virtual ~Driver() { }
 

	
 
@@ -37,12 +35,13 @@ DECLARE_POSTFIX_INCREMENT(Driver::Type);
 

	
 

	
 
class DriverFactoryBase {
 
private:
 
	Driver::Type type;
 
	char *name;
 
	int priority;
 
	typedef std::map<std::string, DriverFactoryBase *> Drivers;
 

	
 
	static Drivers &GetDrivers()
 
	{
 
		static Drivers &s_drivers = *new Drivers();
 
		return s_drivers;
 
@@ -63,19 +62,20 @@ private:
 
protected:
 
	/**
 
	 * Register a driver internally, based on its name.
 
	 * @param name the name of the driver.
 
	 * @note an assert() will be trigger if 2 driver with the same name try to register.
 
	 */
 
	void RegisterDriver(const char *name, Driver::Type type)
 
	void RegisterDriver(const char *name, Driver::Type type, int priority)
 
	{
 
		/* Don't register nameless Drivers */
 
		if (name == NULL) return;
 

	
 
		this->name = strdup(name);
 
		this->type = type;
 
		this->priority = priority;
 

	
 
		/* Prefix the name with driver type to make it unique */
 
		char buf[32];
 
		strecpy(buf, GetDriverTypeName(type), lastof(buf));
 
		strecpy(buf + 5, name, lastof(buf));
 

	
 
@@ -98,35 +98,33 @@ public:
 
	static Driver *SelectDriver(const char *name, Driver::Type type)
 
	{
 
		if (GetDrivers().size() == 0) return NULL;
 

	
 
		if (*name == '\0') {
 
			/* Probe for this driver */
 
			Drivers::iterator it = GetDrivers().begin();
 
			for (; it != GetDrivers().end(); ++it) {
 
				DriverFactoryBase *d = (*it).second;
 
			for (int priority = 10; priority >= 0; priority--) {
 
				Drivers::iterator it = GetDrivers().begin();
 
				for (; it != GetDrivers().end(); ++it) {
 
					DriverFactoryBase *d = (*it).second;
 

	
 
				/* Check driver type */
 
				if (d->type != type) continue;
 
					/* Check driver type */
 
					if (d->type != type) continue;
 
					if (d->priority != priority) continue;
 

	
 
				Driver *newd = d->CreateInstance();
 
				if (!newd->CanProbe()) {
 
					DEBUG(driver, 1, "Skipping probe of driver '%s'", d->name);
 
				} else {
 
					Driver *newd = d->CreateInstance();
 
					const char *err = newd->Start(NULL);
 
					if (err == NULL) {
 
						DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name);
 
						delete *GetActiveDriver(type);
 
						*GetActiveDriver(type) = newd;
 
						return newd;
 
					}
 

	
 
					DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err);
 
					delete newd;
 
				}
 

	
 
				delete newd;
 
			}
 
			error("Couldn't find any suitable %s driver", GetDriverTypeName(type));
 
		} else {
 
			char *parm;
 
			char buffer[256];
 
			const char *parms[32];
 
@@ -182,16 +180,20 @@ public:
 
	 */
 
	static char *GetDriversInfo(char *p, const char *last)
 
	{
 
		for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
 
			p += snprintf(p, last - p, "List of %s drivers:\n", GetDriverTypeName(type));
 

	
 
			Drivers::iterator it = GetDrivers().begin();
 
			for (; it != GetDrivers().end(); it++) {
 
				DriverFactoryBase *d = (*it).second;
 
				if (d->type == type) p += snprintf(p, last - p, "%18s: %s\n", d->name, d->GetDescription());
 
			for (int priority = 10; priority >= 0; priority--) {
 
				Drivers::iterator it = GetDrivers().begin();
 
				for (; it != GetDrivers().end(); it++) {
 
					DriverFactoryBase *d = (*it).second;
 
					if (d->type != type) continue;
 
					if (d->priority != priority) continue;
 
					p += snprintf(p, last - p, "%18s: %s\n", d->name, d->GetDescription());
 
				}
 
			}
 

	
 
			p += snprintf(p, last - p, "\n");
 
		}
 

	
 
		return p;