OpenTTD Source  12.0-beta2
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 "error.h"
13 #include "sound/sound_driver.hpp"
14 #include "music/music_driver.hpp"
15 #include "video/video_driver.hpp"
16 #include "string_func.h"
17 #include "table/strings.h"
18 #include <string>
19 #include <sstream>
20 
21 #include "safeguards.h"
22 
23 std::string _ini_videodriver;
24 std::vector<Dimension> _resolutions;
27 
28 std::string _ini_sounddriver;
29 
30 std::string _ini_musicdriver;
31 
32 std::string _ini_blitter;
34 
41 const char *GetDriverParam(const StringList &parm, const char *name)
42 {
43  if (parm.empty()) return nullptr;
44 
45  size_t len = strlen(name);
46  for (auto &p : parm) {
47  if (p.compare(0, len, name) == 0) {
48  if (p.length() == len) return "";
49  if (p[len] == '=') return p.c_str() + len + 1;
50  }
51  }
52  return nullptr;
53 }
54 
61 bool GetDriverParamBool(const StringList &parm, const char *name)
62 {
63  return GetDriverParam(parm, name) != nullptr;
64 }
65 
73 int GetDriverParamInt(const StringList &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 std::string &name, Driver::Type type)
86 {
88  name.empty() ?
89  usererror("Failed to autoprobe %s driver", GetDriverTypeName(type)) :
90  usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type), name.c_str());
91  }
92 }
93 
101 bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
102 {
103  if (GetDrivers().size() == 0) return false;
104 
105  if (name.empty()) {
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  if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
117 
118  Driver *oldd = *GetActiveDriver(type);
119  Driver *newd = d->CreateInstance();
120  *GetActiveDriver(type) = newd;
121 
122  const char *err = newd->Start({});
123  if (err == nullptr) {
124  Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
125  delete oldd;
126  return true;
127  }
128 
129  *GetActiveDriver(type) = oldd;
130  Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, err);
131  delete newd;
132 
134  _video_hw_accel = false;
135  ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION);
137  }
138  }
139  }
140  usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
141  } else {
142  /* Extract the driver name and put parameter list in parm */
143  std::istringstream buffer(name);
144  std::string dname;
145  std::getline(buffer, dname, ':');
146 
147  std::string param;
148  std::vector<std::string> parms;
149  while (std::getline(buffer, param, ',')) {
150  parms.push_back(param);
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(dname.c_str(), 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 {} driver '{}'", 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), dname.c_str());
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 
216 DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
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  Drivers &drivers = GetDrivers();
225  assert(drivers.find(buf) == drivers.end());
226  drivers.insert(Drivers::value_type(buf, this));
227 }
228 
233 {
234  /* Prefix the name with driver type to make it unique */
235  char buf[32];
236  strecpy(buf, GetDriverTypeName(type), lastof(buf));
237  strecpy(buf + 5, this->name, lastof(buf));
238 
239  Drivers::iterator it = GetDrivers().find(buf);
240  assert(it != GetDrivers().end());
241 
242  GetDrivers().erase(it);
243  if (GetDrivers().empty()) delete &GetDrivers();
244 }
DriverFactoryBase::GetDriverTypeName
static const char * GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition: driver.h:97
DriverFactoryBase::SelectDriverImpl
static bool SelectDriverImpl(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:101
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:86
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:103
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
DriverFactoryBase::name
const char * name
The name of the drivers of this factory.
Definition: driver.h:67
_ini_blitter
std::string _ini_blitter
The blitter as stored in the configuration file.
Definition: driver.cpp:32
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:216
DriverFactoryBase::CreateInstance
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
DriverFactoryBase::~DriverFactoryBase
virtual ~DriverFactoryBase()
Frees memory used for this->name.
Definition: driver.cpp:232
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:41
DriverFactoryBase::GetDrivers
static Drivers & GetDrivers()
Get the map with drivers.
Definition: driver.h:75
_ini_musicdriver
std::string _ini_musicdriver
The music driver a stored in the configuration file.
Definition: driver.cpp:30
DriverFactoryBase::priority
int priority
The priority of this factory.
Definition: driver.h:66
DriverFactoryBase::Drivers
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition: driver.h:70
DriverFactoryBase::GetDriversInfo
static char * GetDriversInfo(char *p, const char *last)
Build a human readable list of available drivers, grouped by type.
Definition: driver.cpp:188
Driver::Start
virtual const char * Start(const StringList &parm)=0
Start this driver.
DriverFactoryBase::type
Driver::Type type
The type of driver.
Definition: driver.h:65
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
safeguards.h
music_driver.hpp
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:24
ErrorMessageData
The data of the error message.
Definition: error.h:29
error.h
stdafx.h
Driver::Type
Type
The type of driver.
Definition: driver.h:40
Driver::DT_END
@ DT_END
Helper for iteration.
Definition: driver.h:45
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:73
sound_driver.hpp
_rightclick_emulate
bool _rightclick_emulate
Whether right clicking is emulated.
Definition: driver.cpp:26
string_func.h
Driver::DT_BEGIN
@ DT_BEGIN
Helper for iteration.
Definition: driver.h:41
video_driver.hpp
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:44
_blitter_autodetected
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
Definition: driver.cpp:33
_ini_sounddriver
std::string _ini_sounddriver
The sound driver a stored in the configuration file.
Definition: driver.cpp:28
_video_hw_accel
bool _video_hw_accel
Whether to consider hardware accelerated video drivers.
Definition: video_driver.cpp:23
ScheduleErrorMessage
void ScheduleErrorMessage(const ErrorMessageData &data)
Schedule an error.
Definition: error_gui.cpp:457
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:114
DriverFactoryBase::GetDescription
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:138
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
DriverFactoryBase::SelectDriver
static void SelectDriver(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:85
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Driver
A driver for communicating with the user.
Definition: driver.h:23
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:61
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:25
DriverFactoryBase
Base for all driver factories.
Definition: driver.h:59
_ini_videodriver
std::string _ini_videodriver
The video driver a stored in the configuration file.
Definition: driver.cpp:23
debug.h