# driver.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "sound/sound_driver.hpp"
    00015 #include "music/music_driver.hpp"
    00016 #include "video/video_driver.hpp"
    00017 #include "string_func.h"
    00018
    00019 VideoDriver *_video_driver;
    00020 char *_ini_videodriver;
    00021 int _num_resolutions;
    00022 Dimension _resolutions[32];
    00023 Dimension _cur_resolution;
    00024 bool _rightclick_emulate;
    00025
    00026 SoundDriver *_sound_driver;
    00027 char *_ini_sounddriver;
    00028
    00029 MusicDriver *_music_driver;
    00030 char *_ini_musicdriver;
    00031
    00032 char *_ini_blitter;
    00033
    00034 const char *GetDriverParam(const char * const *parm, const char *name)
    00035 {
    00036   size_t len;
    00037
    00038   if (parm == NULL) return NULL;
    00039
    00040   len = strlen(name);
    00041   for (; *parm != NULL; parm++) {
    00042     const char *p = *parm;
    00043
    00044     if (strncmp(p, name, len) == 0) {
    00045       if (p[len] == '=')  return p + len + 1;
    00046       if (p[len] == '\0') return p + len;
    00047     }
    00048   }
    00049   return NULL;
    00050 }
    00051
    00052 bool GetDriverParamBool(const char * const *parm, const char *name)
    00053 {
    00054   return GetDriverParam(parm, name) != NULL;
    00055 }
    00056
    00057 int GetDriverParamInt(const char * const *parm, const char *name, int def)
    00058 {
    00059   const char *p = GetDriverParam(parm, name);
    00060   return p != NULL ? atoi(p) : def;
    00061 }
    00062
    00069 Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type)
    00070 {
    00071   if (GetDrivers().size() == 0) return NULL;
    00072
    00073   if (StrEmpty(name)) {
    00074     /* Probe for this driver, but do not fall back to dedicated/null! */
    00075     for (int priority = 10; priority > 0; priority--) {
    00076       Drivers::iterator it = GetDrivers().begin();
    00077       for (; it != GetDrivers().end(); ++it) {
    00078         DriverFactoryBase *d = (*it).second;
    00079
    00080         /* Check driver type */
    00081         if (d->type != type) continue;
    00082         if (d->priority != priority) continue;
    00083
    00084         Driver *newd = d->CreateInstance();
    00085         const char *err = newd->Start(NULL);
    00086         if (err == NULL) {
    00087           DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name);
    00088           delete *GetActiveDriver(type);
    00089           *GetActiveDriver(type) = newd;
    00090           return newd;
    00091         }
    00092
    00093         DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err);
    00094         delete newd;
    00095       }
    00096     }
    00097     usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
    00098   } else {
    00099     char *parm;
    00100     char buffer[256];
    00101     const char *parms[32];
    00102
    00103     /* Extract the driver name and put parameter list in parm */
    00104     strecpy(buffer, name, lastof(buffer));
    00105     parm = strchr(buffer, ':');
    00106     parms[0] = NULL;
    00107     if (parm != NULL) {
    00108       uint np = 0;
    00109       /* Tokenize the parm. */
    00110       do {
    00111         *parm++ = '\0';
    00112         if (np < lengthof(parms) - 1) parms[np++] = parm;
    00113         while (*parm != '\0' && *parm != ',') parm++;
    00114       } while (*parm == ',');
    00115       parms[np] = NULL;
    00116     }
    00117
    00118     /* Find this driver */
    00119     Drivers::iterator it = GetDrivers().begin();
    00120     for (; it != GetDrivers().end(); ++it) {
    00121       DriverFactoryBase *d = (*it).second;
    00122
    00123       /* Check driver type */
    00124       if (d->type != type) continue;
    00125
    00126       /* Check driver name */
    00127       if (strcasecmp(buffer, d->name) != 0) continue;
    00128
    00129       /* Found our driver, let's try it */
    00130       Driver *newd = d->CreateInstance();
    00131
    00132       const char *err = newd->Start(parms);
    00133       if (err != NULL) {
    00134         delete newd;
    00135         usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
    00136       }
    00137
    00138       DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name);
    00139       delete *GetActiveDriver(type);
    00140       *GetActiveDriver(type) = newd;
    00141       return newd;
    00142     }
    00143     usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
    00144   }
    00145 }
    00146
    00154 void DriverFactoryBase::RegisterDriver(const char *name, Driver::Type type, int priority)
    00155 {
    00156   /* Don't register nameless Drivers */
    00157   if (name == NULL) return;
    00158
    00159   this->name = strdup(name);
    00160   this->type = type;
    00161   this->priority = priority;
    00162
    00163   /* Prefix the name with driver type to make it unique */
    00164   char buf[32];
    00165   strecpy(buf, GetDriverTypeName(type), lastof(buf));
    00166   strecpy(buf + 5, name, lastof(buf));
    00167
    00168   const char *longname = strdup(buf);
    00169
    00170   std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this));
    00171   assert(P.second);
    00172 }
    00173
    00177 char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
    00178 {
    00179   for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
    00180     p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
    00181
    00182     for (int priority = 10; priority >= 0; priority--) {
    00183       Drivers::iterator it = GetDrivers().begin();
    00184       for (; it != GetDrivers().end(); it++) {
    00185         DriverFactoryBase *d = (*it).second;
    00186         if (d->type != type) continue;
    00187         if (d->priority != priority) continue;
    00188         p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
    00189       }
    00190     }
    00191
    00192     p += seprintf(p, last, "\n");
    00193   }
    00194
    00195   return p;
    00196 }
    00197
    00201 DriverFactoryBase::~DriverFactoryBase()
    00202 {
    00203   if (this->name == NULL) return;
    00204
    00205   /* Prefix the name with driver type to make it unique */
    00206   char buf[32];
    00207   strecpy(buf, GetDriverTypeName(type), lastof(buf));
    00208   strecpy(buf + 5, this->name, lastof(buf));
    00209
    00210   Drivers::iterator it = GetDrivers().find(buf);
    00211   assert(it != GetDrivers().end());
    00212
    00213   const char *longname = (*it).first;
    00214
    00215   GetDrivers().erase(it);
    00216   free((void *)longname);
    00217
    00218   if (GetDrivers().empty()) delete &GetDrivers();
    00219   free((void *)this->name);
    00220 }