OpenTTD Source: src/driver.cpp Source File

OpenTTD Source 1.11.0-RC1

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;
25 Dimension _cur_resolution;
26 bool _rightclick_emulate;
27
28 std::string _ini_sounddriver;
29
30 std::string _ini_musicdriver;
31
32 std::string _ini_blitter;
33 bool _blitter_autodetected;
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 {
87 if (!DriverFactoryBase::SelectDriverImpl(name, type)) {
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 %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 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
134 _video_hw_accel = false;
135 ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION);
136 ScheduleErrorMessage(msg);
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 %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), dname.c_str());
179 }
180 }
181
188 char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
189 {
190 for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
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 std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(buf, this));
225 assert(P.second);
226 }
227
231 DriverFactoryBase::~DriverFactoryBase()
232 {
233 /* Prefix the name with driver type to make it unique */
234 char buf[32];
235 strecpy(buf, GetDriverTypeName(type), lastof(buf));
236 strecpy(buf + 5, this->name, lastof(buf));
237
238 Drivers::iterator it = GetDrivers().find(buf);
239 assert(it != GetDrivers().end());
240
241 GetDrivers().erase(it);
242 if (GetDrivers().empty()) delete &GetDrivers();
243 }
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:102
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:231
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::GetDriversInfo
static char * GetDriversInfo(char *p, const char *last)
Build a human readable list of available drivers, grouped by type.
Definition: driver.cpp:188
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
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:447
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:442
DriverFactoryBase::SelectDriver
static void SelectDriver(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:85
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:383
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