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