2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file driver.cpp Base for all driver handling. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
#include "stdafx.h"
|
2005-07-27 19:57:12 +00:00
|
|
|
#include "debug.h"
|
2007-07-05 12:23:54 +00:00
|
|
|
#include "sound/sound_driver.hpp"
|
|
|
|
#include "music/music_driver.hpp"
|
|
|
|
#include "video/video_driver.hpp"
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-01-13 21:41:24 +00:00
|
|
|
VideoDriver *_video_driver;
|
2009-01-08 11:06:07 +00:00
|
|
|
char *_ini_videodriver;
|
2008-01-13 21:41:24 +00:00
|
|
|
int _num_resolutions;
|
2008-06-16 19:38:41 +00:00
|
|
|
Dimension _resolutions[32];
|
|
|
|
Dimension _cur_resolution;
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
SoundDriver *_sound_driver;
|
2009-01-08 11:06:07 +00:00
|
|
|
char *_ini_sounddriver;
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
MusicDriver *_music_driver;
|
2009-01-08 11:06:07 +00:00
|
|
|
char *_ini_musicdriver;
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2009-01-08 11:06:07 +00:00
|
|
|
char *_ini_blitter;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *GetDriverParam(const char * const *parm, const char *name)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2005-09-25 09:04:59 +00:00
|
|
|
size_t len;
|
2005-07-29 06:20:28 +00:00
|
|
|
|
|
|
|
if (parm == NULL) return NULL;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2005-07-29 06:20:28 +00:00
|
|
|
len = strlen(name);
|
2005-07-23 15:16:57 +00:00
|
|
|
for (; *parm != NULL; parm++) {
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *p = *parm;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
if (strncmp(p, name, len) == 0) {
|
|
|
|
if (p[len] == '=') return p + len + 1;
|
|
|
|
if (p[len] == '\0') return p + len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
bool GetDriverParamBool(const char * const *parm, const char *name)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
return GetDriverParam(parm, name) != NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
int GetDriverParamInt(const char * const *parm, const char *name, int def)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *p = GetDriverParam(parm, name);
|
2005-07-23 15:16:57 +00:00
|
|
|
return p != NULL ? atoi(p) : def;
|
|
|
|
}
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the requested driver and return its class.
|
|
|
|
* @param name the driver to select.
|
|
|
|
* @post Sets the driver so GetCurrentDriver() returns it too.
|
|
|
|
*/
|
2009-05-24 20:29:04 +00:00
|
|
|
Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type)
|
2008-04-06 16:49:02 +00:00
|
|
|
{
|
|
|
|
if (GetDrivers().size() == 0) return NULL;
|
|
|
|
|
2009-01-08 11:06:07 +00:00
|
|
|
if (StrEmpty(name)) {
|
2008-04-06 16:49:02 +00:00
|
|
|
/* Probe for this driver */
|
|
|
|
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;
|
|
|
|
if (d->priority != priority) continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-06-05 20:54:52 +00:00
|
|
|
usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
|
2008-04-06 16:49:02 +00:00
|
|
|
} else {
|
|
|
|
char *parm;
|
|
|
|
char buffer[256];
|
|
|
|
const char *parms[32];
|
|
|
|
|
|
|
|
/* Extract the driver name and put parameter list in parm */
|
|
|
|
strecpy(buffer, name, lastof(buffer));
|
|
|
|
parm = strchr(buffer, ':');
|
|
|
|
parms[0] = NULL;
|
|
|
|
if (parm != NULL) {
|
|
|
|
uint np = 0;
|
|
|
|
/* Tokenize the parm. */
|
|
|
|
do {
|
|
|
|
*parm++ = '\0';
|
|
|
|
if (np < lengthof(parms) - 1) parms[np++] = parm;
|
|
|
|
while (*parm != '\0' && *parm != ',') parm++;
|
|
|
|
} while (*parm == ',');
|
|
|
|
parms[np] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find this driver */
|
|
|
|
Drivers::iterator it = GetDrivers().begin();
|
|
|
|
for (; it != GetDrivers().end(); ++it) {
|
|
|
|
DriverFactoryBase *d = (*it).second;
|
|
|
|
|
|
|
|
/* Check driver type */
|
|
|
|
if (d->type != type) continue;
|
|
|
|
|
|
|
|
/* Check driver name */
|
|
|
|
if (strcasecmp(buffer, d->name) != 0) continue;
|
|
|
|
|
|
|
|
/* Found our driver, let's try it */
|
|
|
|
Driver *newd = d->CreateInstance();
|
|
|
|
|
|
|
|
const char *err = newd->Start(parms);
|
|
|
|
if (err != NULL) {
|
|
|
|
delete newd;
|
2008-06-05 20:54:52 +00:00
|
|
|
usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name);
|
|
|
|
delete *GetActiveDriver(type);
|
|
|
|
*GetActiveDriver(type) = newd;
|
|
|
|
return newd;
|
|
|
|
}
|
2008-06-05 20:54:52 +00:00
|
|
|
usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 DriverFactoryBase::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));
|
|
|
|
|
2008-06-24 09:15:45 +00:00
|
|
|
const char *longname = strdup(buf);
|
|
|
|
|
|
|
|
std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this));
|
2008-04-06 16:49:02 +00:00
|
|
|
assert(P.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a human readable list of available drivers, grouped by type.
|
|
|
|
*/
|
|
|
|
char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
|
|
|
|
{
|
|
|
|
for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
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;
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "\n");
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2008-06-11 12:46:28 +00:00
|
|
|
|
|
|
|
/** Frees memory used for this->name
|
|
|
|
*/
|
|
|
|
DriverFactoryBase::~DriverFactoryBase() {
|
|
|
|
if (this->name == NULL) return;
|
|
|
|
|
|
|
|
/* Prefix the name with driver type to make it unique */
|
|
|
|
char buf[32];
|
|
|
|
strecpy(buf, GetDriverTypeName(type), lastof(buf));
|
|
|
|
strecpy(buf + 5, this->name, lastof(buf));
|
|
|
|
|
2008-06-24 09:15:45 +00:00
|
|
|
Drivers::iterator it = GetDrivers().find(buf);
|
|
|
|
assert(it != GetDrivers().end());
|
|
|
|
|
|
|
|
const char *longname = (*it).first;
|
|
|
|
|
|
|
|
GetDrivers().erase(it);
|
|
|
|
free((void *)longname);
|
|
|
|
|
2008-06-11 12:46:28 +00:00
|
|
|
if (GetDrivers().empty()) delete &GetDrivers();
|
2008-06-24 09:15:45 +00:00
|
|
|
free((void *)this->name);
|
2008-06-11 12:46:28 +00:00
|
|
|
}
|