OpenTTD Source: src/driver.cpp Source File

OpenTTD Source 13.2.1

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 "fileio_func.h"
19 #include <string>
20 #include <sstream>
21
22 #ifdef _WIN32
23 # include <windows.h>
24 #else
25 # include <unistd.h>
26 #endif /* _WIN32 */
27
28 #include "safeguards.h"
29
30 std::string _ini_videodriver;
31 std::vector<Dimension> _resolutions;
32 Dimension _cur_resolution;
33 bool _rightclick_emulate;
34
35 std::string _ini_sounddriver;
36
37 std::string _ini_musicdriver;
38
39 std::string _ini_blitter;
40 bool _blitter_autodetected;
41
42 static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat";
43
50 const char *GetDriverParam(const StringList &parm, const char *name)
51 {
52 if (parm.empty()) return nullptr;
53
54 size_t len = strlen(name);
55 for (auto &p : parm) {
56 if (p.compare(0, len, name) == 0) {
57 if (p.length() == len) return "";
58 if (p[len] == '=') return p.c_str() + len + 1;
59 }
60 }
61 return nullptr;
62 }
63
70 bool GetDriverParamBool(const StringList &parm, const char *name)
71 {
72 return GetDriverParam(parm, name) != nullptr;
73 }
74
82 int GetDriverParamInt(const StringList &parm, const char *name, int def)
83 {
84 const char *p = GetDriverParam(parm, name);
85 return p != nullptr ? atoi(p) : def;
86 }
87
94 void DriverFactoryBase::SelectDriver(const std::string &name, Driver::Type type)
95 {
96 if (!DriverFactoryBase::SelectDriverImpl(name, type)) {
97 name.empty() ?
98 usererror("Failed to autoprobe %s driver", GetDriverTypeName(type)) :
99 usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type), name.c_str());
100 }
101 }
102
110 bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
111 {
112 if (GetDrivers().size() == 0) return false;
113
114 if (name.empty()) {
115 /* Probe for this driver, but do not fall back to dedicated/null! */
116 for (int priority = 10; priority > 0; priority--) {
117 Drivers::iterator it = GetDrivers().begin();
118 for (; it != GetDrivers().end(); ++it) {
119 DriverFactoryBase *d = (*it).second;
120
121 /* Check driver type */
122 if (d->type != type) continue;
123 if (d->priority != priority) continue;
124
125 if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
126
127 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
128 /* Check if we have already tried this driver in last run.
129 * If it is here, it most likely means we crashed. So skip
130 * hardware acceleration. */
131 auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE.c_str());
132 if (!filename.empty()) {
133 unlink(filename.c_str());
134
135 Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name);
136
137 _video_hw_accel = false;
138 ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_HARDWARE_ACCELERATION_CRASH, true);
139 ScheduleErrorMessage(msg);
140 continue;
141 }
142
143 /* Write empty file to note we are attempting hardware acceleration. */
144 auto f = FioFOpenFile(HWACCELERATION_TEST_FILE.c_str(), "w", BASE_DIR);
145 FioFCloseFile(f);
146 }
147
148 Driver *oldd = *GetActiveDriver(type);
149 Driver *newd = d->CreateInstance();
150 *GetActiveDriver(type) = newd;
151
152 const char *err = newd->Start({});
153 if (err == nullptr) {
154 Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
155 delete oldd;
156 return true;
157 }
158
159 *GetActiveDriver(type) = oldd;
160 Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, err);
161 delete newd;
162
163 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
164 _video_hw_accel = false;
165 ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION, true);
166 ScheduleErrorMessage(msg);
167 }
168 }
169 }
170 usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type));
171 } else {
172 /* Extract the driver name and put parameter list in parm */
173 std::istringstream buffer(name);
174 std::string dname;
175 std::getline(buffer, dname, ':');
176
177 std::string param;
178 std::vector<std::string> parms;
179 while (std::getline(buffer, param, ',')) {
180 parms.push_back(param);
181 }
182
183 /* Find this driver */
184 Drivers::iterator it = GetDrivers().begin();
185 for (; it != GetDrivers().end(); ++it) {
186 DriverFactoryBase *d = (*it).second;
187
188 /* Check driver type */
189 if (d->type != type) continue;
190
191 /* Check driver name */
192 if (strcasecmp(dname.c_str(), d->name) != 0) continue;
193
194 /* Found our driver, let's try it */
195 Driver *newd = d->CreateInstance();
196
197 const char *err = newd->Start(parms);
198 if (err != nullptr) {
199 delete newd;
200 usererror("Unable to load driver '%s'. The error was: %s", d->name, err);
201 }
202
203 Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name);
204 delete *GetActiveDriver(type);
205 *GetActiveDriver(type) = newd;
206 return true;
207 }
208 usererror("No such %s driver: %s\n", GetDriverTypeName(type), dname.c_str());
209 }
210 }
211
215 void DriverFactoryBase::MarkVideoDriverOperational()
216 {
217 /* As part of the detection whether the GPU driver crashes the game,
218 * and as we are operational now, remove the hardware acceleration
219 * test-file. */
220 auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE.c_str());
221 if (!filename.empty()) unlink(filename.c_str());
222 }
223
230 char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
231 {
232 for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
233 p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
234
235 for (int priority = 10; priority >= 0; priority--) {
236 Drivers::iterator it = GetDrivers().begin();
237 for (; it != GetDrivers().end(); it++) {
238 DriverFactoryBase *d = (*it).second;
239 if (d->type != type) continue;
240 if (d->priority != priority) continue;
241 p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
242 }
243 }
244
245 p += seprintf(p, last, "\n");
246 }
247
248 return p;
249 }
250
258 DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
259 type(type), priority(priority), name(name), description(description)
260 {
261 /* Prefix the name with driver type to make it unique */
262 char buf[32];
263 strecpy(buf, GetDriverTypeName(type), lastof(buf));
264 strecpy(buf + 5, name, lastof(buf));
265
266 Drivers &drivers = GetDrivers();
267 assert(drivers.find(buf) == drivers.end());
268 drivers.insert(Drivers::value_type(buf, this));
269 }
270
274 DriverFactoryBase::~DriverFactoryBase()
275 {
276 /* Prefix the name with driver type to make it unique */
277 char buf[32];
278 strecpy(buf, GetDriverTypeName(type), lastof(buf));
279 strecpy(buf + 5, this->name, lastof(buf));
280
281 Drivers::iterator it = GetDrivers().find(buf);
282 assert(it != GetDrivers().end());
283
284 GetDrivers().erase(it);
285 if (GetDrivers().empty()) delete &GetDrivers();
286 }
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:110
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:105
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:39
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition: driver.cpp:258
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:274
fileio_func.h
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:50
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:37
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition: fileio_type.h:109
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:230
FioFOpenFile
FILE * FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:245
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:59
safeguards.h
music_driver.hpp
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:31
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:82
sound_driver.hpp
_rightclick_emulate
bool _rightclick_emulate
Whether right clicking is emulated.
Definition: driver.cpp:33
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:40
_ini_sounddriver
std::string _ini_sounddriver
The sound driver a stored in the configuration file.
Definition: driver.cpp:35
_video_hw_accel
bool _video_hw_accel
Whether to consider hardware accelerated video drivers.
Definition: video_driver.cpp:24
ScheduleErrorMessage
void ScheduleErrorMessage(const ErrorMessageData &data)
Schedule an error.
Definition: error_gui.cpp:451
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition: driver.h:116
DriverFactoryBase::GetDescription
const char * GetDescription() const
Get a nice description of the driver-class.
Definition: driver.h:140
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
DriverFactoryBase::SelectDriver
static void SelectDriver(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition: driver.cpp:94
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
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:141
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:113
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:402
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:70
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:32
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:30
DriverFactoryBase::MarkVideoDriverOperational
static void MarkVideoDriverOperational()
Mark the current video driver as operational.
Definition: driver.cpp:215
FioFCloseFile
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:130
debug.h
HWACCELERATION_TEST_FILE
static const std::string HWACCELERATION_TEST_FILE
Filename to test if we crashed last time we tried to use hardware acceleration.
Definition: driver.cpp:42