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