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 
00040 const char *GetDriverParam(const char * const *parm, const char *name)
00041 {
00042   size_t len;
00043 
00044   if (parm == NULL) return NULL;
00045 
00046   len = strlen(name);
00047   for (; *parm != NULL; parm++) {
00048     const char *p = *parm;
00049 
00050     if (strncmp(p, name, len) == 0) {
00051       if (p[len] == '=')  return p + len + 1;
00052       if (p[len] == '\0') return p + len;
00053     }
00054   }
00055   return NULL;
00056 }
00057 
00064 bool GetDriverParamBool(const char * const *parm, const char *name)
00065 {
00066   return GetDriverParam(parm, name) != NULL;
00067 }
00068 
00076 int GetDriverParamInt(const char * const *parm, const char *name, int def)
00077 {
00078   const char *p = GetDriverParam(parm, name);
00079   return p != NULL ? atoi(p) : def;
00080 }
00081 
00088 Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type)
00089 {
00090   if (GetDrivers().size() == 0) return NULL;
00091 
00092   if (StrEmpty(name)) {
00093     /* Probe for this driver, but do not fall back to dedicated/null! */
00094     for (int priority = 10; priority > 0; priority--) {
00095       Drivers::iterator it = GetDrivers().begin();
00096       for (; it != GetDrivers().end(); ++it) {
00097         DriverFactoryBase *d = (*it).second;
00098 
00099         /* Check driver type */
00100         if (d->type != type) continue;
00101         if (d->priority != priority) continue;
00102 
00103         Driver *newd = d->CreateInstance();
00104         const char *err = newd->Start(NULL);
00105         if (err == NULL) {
00106           DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name);
00107           delete *GetActiveDriver(type);
00108           *GetActiveDriver(type) = newd;
00109           return newd;
00110         }
00111 
00112         DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err);
00113         delete newd;
00114       }
00115     }
00116     usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
00117   } else {
00118     char *parm;
00119     char buffer[256];
00120     const char *parms[32];
00121 
00122     /* Extract the driver name and put parameter list in parm */
00123     strecpy(buffer, name, lastof(buffer));
00124     parm = strchr(buffer, ':');
00125     parms[0] = NULL;
00126     if (parm != NULL) {
00127       uint np = 0;
00128       /* Tokenize the parm. */
00129       do {
00130         *parm++ = '\0';
00131         if (np < lengthof(parms) - 1) parms[np++] = parm;
00132         while (*parm != '\0' && *parm != ',') parm++;
00133       } while (*parm == ',');
00134       parms[np] = NULL;
00135     }
00136 
00137     /* Find this driver */
00138     Drivers::iterator it = GetDrivers().begin();
00139     for (; it != GetDrivers().end(); ++it) {
00140       DriverFactoryBase *d = (*it).second;
00141 
00142       /* Check driver type */
00143       if (d->type != type) continue;
00144 
00145       /* Check driver name */
00146       if (strcasecmp(buffer, d->name) != 0) continue;
00147 
00148       /* Found our driver, let's try it */
00149       Driver *newd = d->CreateInstance();
00150 
00151       const char *err = newd->Start(parms);
00152       if (err != NULL) {
00153         delete newd;
00154         usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
00155       }
00156 
00157       DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name);
00158       delete *GetActiveDriver(type);
00159       *GetActiveDriver(type) = newd;
00160       return newd;
00161     }
00162     usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
00163   }
00164 }
00165 
00173 void DriverFactoryBase::RegisterDriver(const char *name, Driver::Type type, int priority)
00174 {
00175   /* Don't register nameless Drivers */
00176   if (name == NULL) return;
00177 
00178   this->name = strdup(name);
00179   this->type = type;
00180   this->priority = priority;
00181 
00182   /* Prefix the name with driver type to make it unique */
00183   char buf[32];
00184   strecpy(buf, GetDriverTypeName(type), lastof(buf));
00185   strecpy(buf + 5, name, lastof(buf));
00186 
00187   const char *longname = strdup(buf);
00188 
00189   std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this));
00190   assert(P.second);
00191 }
00192 
00199 char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
00200 {
00201   for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
00202     p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
00203 
00204     for (int priority = 10; priority >= 0; priority--) {
00205       Drivers::iterator it = GetDrivers().begin();
00206       for (; it != GetDrivers().end(); it++) {
00207         DriverFactoryBase *d = (*it).second;
00208         if (d->type != type) continue;
00209         if (d->priority != priority) continue;
00210         p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
00211       }
00212     }
00213 
00214     p += seprintf(p, last, "\n");
00215   }
00216 
00217   return p;
00218 }
00219 
00223 DriverFactoryBase::~DriverFactoryBase()
00224 {
00225   if (this->name == NULL) return;
00226 
00227   /* Prefix the name with driver type to make it unique */
00228   char buf[32];
00229   strecpy(buf, GetDriverTypeName(type), lastof(buf));
00230   strecpy(buf + 5, this->name, lastof(buf));
00231 
00232   Drivers::iterator it = GetDrivers().find(buf);
00233   assert(it != GetDrivers().end());
00234 
00235   const char *longname = (*it).first;
00236 
00237   GetDrivers().erase(it);
00238   free((void *)longname);
00239 
00240   if (GetDrivers().empty()) delete &GetDrivers();
00241   free((void *)this->name);
00242 }

Generated on Fri Jun 3 05:18:50 2011 for OpenTTD by  doxygen 1.6.1