OpenTTD
driver.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "sound/sound_driver.hpp"
15 #include "music/music_driver.hpp"
16 #include "video/video_driver.hpp"
17 #include "string_func.h"
18 
19 #include "safeguards.h"
20 
22 std::vector<Dimension> _resolutions;
25 
27 
29 
30 char *_ini_blitter;
32 
39 const char *GetDriverParam(const char * const *parm, const char *name)
40 {
41  size_t len;
42 
43  if (parm == nullptr) return nullptr;
44 
45  len = strlen(name);
46  for (; *parm != nullptr; parm++) {
47  const char *p = *parm;
48 
49  if (strncmp(p, name, len) == 0) {
50  if (p[len] == '=') return p + len + 1;
51  if (p[len] == '\0') return p + len;
52  }
53  }
54  return nullptr;
55 }
56 
63 bool GetDriverParamBool(const char * const *parm, const char *name)
64 {
65  return GetDriverParam(parm, name) != nullptr;
66 }
67 
75 int GetDriverParamInt(const char * const *parm, const char *name, int def)
76 {
77  const char *p = GetDriverParam(parm, name);
78  return p != nullptr ? atoi(p) : def;
79 }
80 
87 void DriverFactoryBase::SelectDriver(const char *name, Driver::Type type)
88 {
89  if (!DriverFactoryBase::SelectDriverImpl(name, type)) {
90  StrEmpty(name) ?
91  usererror("Failed to autoprobe %s driver", GetDriverTypeName(type)) :
92  usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type), name);
93  }
94 }
95 
104 {
105  if (GetDrivers().size() == 0) return false;
106 
107  if (StrEmpty(name)) {
108  /* Probe for this driver, but do not fall back to dedicated/null! */
109  for (int priority = 10; priority > 0; priority--) {
110  Drivers::iterator it = GetDrivers().begin();
111  for (; it != GetDrivers().end(); ++it) {
112  DriverFactoryBase *d = (*it).second;
113 
114  /* Check driver type */
115  if (d->type != type) continue;
116  if (d->priority != priority) continue;
117 
118  Driver *oldd = *GetActiveDriver(type);
119  Driver *newd = d->CreateInstance();
120  *GetActiveDriver(type) = newd;
121 
122  const char *err = newd->Start(nullptr);
123  if (err == nullptr) {
124  DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name);
125  delete oldd;
126  return true;
127  }
128 
129  *GetActiveDriver(type) = oldd;
130  DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err);
131  delete newd;
132  }
133  }
134  usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
135  } else {
136  char *parm;
137  char buffer[256];
138  const char *parms[32];
139 
140  /* Extract the driver name and put parameter list in parm */
141  strecpy(buffer, name, lastof(buffer));
142  parm = strchr(buffer, ':');
143  parms[0] = nullptr;
144  if (parm != nullptr) {
145  uint np = 0;
146  /* Tokenize the parm. */
147  do {
148  *parm++ = '\0';
149  if (np < lengthof(parms) - 1) parms[np++] = parm;
150  while (*parm != '\0' && *parm != ',') parm++;
151  } while (*parm == ',');
152  parms[np] = nullptr;
153  }
154 
155  /* Find this driver */
156  Drivers::iterator it = GetDrivers().begin();
157  for (; it != GetDrivers().end(); ++it) {
158  DriverFactoryBase *d = (*it).second;
159 
160  /* Check driver type */
161  if (d->type != type) continue;
162 
163  /* Check driver name */
164  if (strcasecmp(buffer, d->name) != 0) continue;
165 
166  /* Found our driver, let's try it */
167  Driver *newd = d->CreateInstance();
168 
169  const char *err = newd->Start(parms);
170  if (err != nullptr) {
171  delete newd;
172  usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
173  }
174 
175  DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name);
176  delete *GetActiveDriver(type);
177  *GetActiveDriver(type) = newd;
178  return true;
179  }
180  usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer);
181  }
182 }
183 
190 char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
191 {
193  p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
194 
195  for (int priority = 10; priority >= 0; priority--) {
196  Drivers::iterator it = GetDrivers().begin();
197  for (; it != GetDrivers().end(); it++) {
198  DriverFactoryBase *d = (*it).second;
199  if (d->type != type) continue;
200  if (d->priority != priority) continue;
201  p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
202  }
203  }
204 
205  p += seprintf(p, last, "\n");
206  }
207 
208  return p;
209 }
210 
219  type(type), priority(priority), name(name), description(description)
220 {
221  /* Prefix the name with driver type to make it unique */
222  char buf[32];
223  strecpy(buf, GetDriverTypeName(type), lastof(buf));
224  strecpy(buf + 5, name, lastof(buf));
225 
226  const char *longname = stredup(buf);
227 
228  std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this));
229  assert(P.second);
230 }
231 
236 {
237  /* Prefix the name with driver type to make it unique */
238  char buf[32];
239  strecpy(buf, GetDriverTypeName(type), lastof(buf));
240  strecpy(buf + 5, this->name, lastof(buf));
241 
242  Drivers::iterator it = GetDrivers().find(buf);
243  assert(it != GetDrivers().end());
244 
245  const char *longname = (*it).first;
246 
247  GetDrivers().erase(it);
248  free(longname);
249 
250  if (GetDrivers().empty()) delete &GetDrivers();
251 }
const char * GetDriverParam(const char *const *parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:39
char * _ini_videodriver
The video driver a stored in the configuration file.
Definition: driver.cpp:21
virtual ~DriverFactoryBase()
Frees memory used for this->name.
Definition: driver.cpp:235
Base of all video drivers.
Helper for iteration.
Definition: driver.h:46
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:76
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:87
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
char * _ini_blitter
The blitter as stored in the configuration file.
Definition: driver.cpp:30
Functions related to debugging.
bool GetDriverParamBool(const char *const *parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:63
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:98
Base for all sound drivers.
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:218
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:23
static bool SelectDriverImpl(const char *name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:103
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
static char * GetDriversInfo(char *p, const char *last)
Build a human readable list of available drivers, grouped by type.
Definition: driver.cpp:190
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:22
int priority
The priority of this factory.
Definition: driver.h:67
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
Functions related to low-level strings.
const char * description
The description of this driver.
Definition: driver.h:69
char * _ini_sounddriver
The sound driver a stored in the configuration file.
Definition: driver.cpp:26
virtual const char * Start(const char *const *parm)=0
Start this driver.
Driver::Type type
The type of driver.
Definition: driver.h:66
Base for all music playback.
Definition of base types and functions in a cross-platform compatible way.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:94
A number of safeguards to prevent using unsafe methods.
Base for all driver factories.
Definition: driver.h:60
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Helper for iteration.
Definition: driver.h:42
A driver for communicating with the user.
Definition: driver.h:24
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
static void SelectDriver(const char *name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:87
Type
The type of driver.
Definition: driver.h:41
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
Definition: driver.cpp:31
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:75
bool _rightclick_emulate
Whether right clicking is emulated.
Definition: driver.cpp:24
char * _ini_musicdriver
The music driver a stored in the configuration file.
Definition: driver.cpp:28
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:130
Dimensions (a width and height) of a rectangle in 2D.
const char * name
The name of the drivers of this factory.
Definition: driver.h:68