tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file driver.cpp Base for all driver handling. */ belugas@6125: tron@2171: #include "stdafx.h" tron@2171: #include "openttd.h" tron@2210: #include "debug.h" tron@2171: #include "driver.h" tron@2171: peter1138@7170: #include "sound/sound_driver.hpp" peter1138@7170: #include "music/music_driver.hpp" peter1138@7170: #include "video/video_driver.hpp" tron@2222: rubidium@8275: VideoDriver *_video_driver; rubidium@8275: char _ini_videodriver[32]; rubidium@8275: int _num_resolutions; smatz@9533: Dimension _resolutions[32]; smatz@9533: Dimension _cur_resolution; rubidium@8275: peter1138@7170: SoundDriver *_sound_driver; rubidium@8275: char _ini_sounddriver[32]; rubidium@8275: peter1138@7170: MusicDriver *_music_driver; rubidium@8275: char _ini_musicdriver[32]; rubidium@8275: rubidium@8275: char _ini_blitter[32]; tron@2171: tron@2171: static const char* GetDriverParam(const char* const* parm, const char* name) tron@2171: { tron@2458: size_t len; tron@2171: tron@2224: if (parm == NULL) return NULL; tron@2224: tron@2224: len = strlen(name); tron@2171: for (; *parm != NULL; parm++) { tron@2171: const char* p = *parm; tron@2171: tron@2171: if (strncmp(p, name, len) == 0) { tron@2171: if (p[len] == '=') return p + len + 1; tron@2171: if (p[len] == '\0') return p + len; tron@2171: } tron@2171: } tron@2171: return NULL; tron@2171: } tron@2171: tron@2171: bool GetDriverParamBool(const char* const* parm, const char* name) tron@2171: { tron@2171: return GetDriverParam(parm, name) != NULL; tron@2171: } tron@2171: tron@2171: int GetDriverParamInt(const char* const* parm, const char* name, int def) tron@2171: { tron@2171: const char* p = GetDriverParam(parm, name); tron@2171: return p != NULL ? atoi(p) : def; tron@2171: } smatz@8844: smatz@8844: /** smatz@8844: * Find the requested driver and return its class. smatz@8844: * @param name the driver to select. smatz@8844: * @post Sets the driver so GetCurrentDriver() returns it too. smatz@8844: */ smatz@8844: const Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) smatz@8844: { smatz@8844: if (GetDrivers().size() == 0) return NULL; smatz@8844: smatz@8844: if (*name == '\0') { smatz@8844: /* Probe for this driver */ smatz@8844: for (int priority = 10; priority >= 0; priority--) { smatz@8844: Drivers::iterator it = GetDrivers().begin(); smatz@8844: for (; it != GetDrivers().end(); ++it) { smatz@8844: DriverFactoryBase *d = (*it).second; smatz@8844: smatz@8844: /* Check driver type */ smatz@8844: if (d->type != type) continue; smatz@8844: if (d->priority != priority) continue; smatz@8844: smatz@8844: Driver *newd = d->CreateInstance(); smatz@8844: const char *err = newd->Start(NULL); smatz@8844: if (err == NULL) { smatz@8844: DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name); smatz@8844: delete *GetActiveDriver(type); smatz@8844: *GetActiveDriver(type) = newd; smatz@8844: return newd; smatz@8844: } smatz@8844: smatz@8844: DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err); smatz@8844: delete newd; smatz@8844: } smatz@8844: } glx@9470: usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type)); smatz@8844: } else { smatz@8844: char *parm; smatz@8844: char buffer[256]; smatz@8844: const char *parms[32]; smatz@8844: smatz@8844: /* Extract the driver name and put parameter list in parm */ smatz@8844: strecpy(buffer, name, lastof(buffer)); smatz@8844: parm = strchr(buffer, ':'); smatz@8844: parms[0] = NULL; smatz@8844: if (parm != NULL) { smatz@8844: uint np = 0; smatz@8844: /* Tokenize the parm. */ smatz@8844: do { smatz@8844: *parm++ = '\0'; smatz@8844: if (np < lengthof(parms) - 1) parms[np++] = parm; smatz@8844: while (*parm != '\0' && *parm != ',') parm++; smatz@8844: } while (*parm == ','); smatz@8844: parms[np] = NULL; smatz@8844: } smatz@8844: smatz@8844: /* Find this driver */ smatz@8844: Drivers::iterator it = GetDrivers().begin(); smatz@8844: for (; it != GetDrivers().end(); ++it) { smatz@8844: DriverFactoryBase *d = (*it).second; smatz@8844: smatz@8844: /* Check driver type */ smatz@8844: if (d->type != type) continue; smatz@8844: smatz@8844: /* Check driver name */ smatz@8844: if (strcasecmp(buffer, d->name) != 0) continue; smatz@8844: smatz@8844: /* Found our driver, let's try it */ smatz@8844: Driver *newd = d->CreateInstance(); smatz@8844: smatz@8844: const char *err = newd->Start(parms); smatz@8844: if (err != NULL) { smatz@8844: delete newd; glx@9470: usererror("Unable to load driver '%s'. The error was: %s", d->name, err); smatz@8844: } smatz@8844: smatz@8844: DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name); smatz@8844: delete *GetActiveDriver(type); smatz@8844: *GetActiveDriver(type) = newd; smatz@8844: return newd; smatz@8844: } glx@9470: usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer); smatz@8844: } smatz@8844: } smatz@8844: smatz@8844: /** smatz@8844: * Register a driver internally, based on its name. smatz@8844: * @param name the name of the driver. smatz@8844: * @note an assert() will be trigger if 2 driver with the same name try to register. smatz@8844: */ smatz@8844: void DriverFactoryBase::RegisterDriver(const char *name, Driver::Type type, int priority) smatz@8844: { smatz@8844: /* Don't register nameless Drivers */ smatz@8844: if (name == NULL) return; smatz@8844: smatz@8844: this->name = strdup(name); smatz@8844: this->type = type; smatz@8844: this->priority = priority; smatz@8844: smatz@8844: /* Prefix the name with driver type to make it unique */ smatz@8844: char buf[32]; smatz@8844: strecpy(buf, GetDriverTypeName(type), lastof(buf)); smatz@8844: strecpy(buf + 5, name, lastof(buf)); smatz@8844: smatz@9582: const char *longname = strdup(buf); smatz@9582: smatz@9582: std::pair P = GetDrivers().insert(Drivers::value_type(longname, this)); smatz@8844: assert(P.second); smatz@8844: } smatz@8844: smatz@8844: /** smatz@8844: * Build a human readable list of available drivers, grouped by type. smatz@8844: */ smatz@8844: char *DriverFactoryBase::GetDriversInfo(char *p, const char *last) smatz@8844: { smatz@8844: for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) { smatz@8844: p += snprintf(p, last - p, "List of %s drivers:\n", GetDriverTypeName(type)); smatz@8844: smatz@8844: for (int priority = 10; priority >= 0; priority--) { smatz@8844: Drivers::iterator it = GetDrivers().begin(); smatz@8844: for (; it != GetDrivers().end(); it++) { smatz@8844: DriverFactoryBase *d = (*it).second; smatz@8844: if (d->type != type) continue; smatz@8844: if (d->priority != priority) continue; smatz@8844: p += snprintf(p, last - p, "%18s: %s\n", d->name, d->GetDescription()); smatz@8844: } smatz@8844: } smatz@8844: smatz@8844: p += snprintf(p, last - p, "\n"); smatz@8844: } smatz@8844: smatz@8844: return p; smatz@8844: } smatz@9489: smatz@9489: /** Frees memory used for this->name smatz@9489: */ smatz@9489: DriverFactoryBase::~DriverFactoryBase() { smatz@9489: if (this->name == NULL) return; smatz@9489: smatz@9489: /* Prefix the name with driver type to make it unique */ smatz@9489: char buf[32]; smatz@9489: strecpy(buf, GetDriverTypeName(type), lastof(buf)); smatz@9489: strecpy(buf + 5, this->name, lastof(buf)); smatz@9489: smatz@9582: Drivers::iterator it = GetDrivers().find(buf); smatz@9582: assert(it != GetDrivers().end()); smatz@9582: smatz@9582: const char *longname = (*it).first; smatz@9582: smatz@9582: GetDrivers().erase(it); smatz@9582: free((void *)longname); smatz@9582: smatz@9489: if (GetDrivers().empty()) delete &GetDrivers(); smatz@9582: free((void *)this->name); smatz@9489: }