OpenTTD Source: src/driver.cpp Source File

OpenTTD Source 15.0-beta1

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 "error_func.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#include "table/strings.h"
19#include "fileio_func.h"
20#include <sstream>
21
22#include "safeguards.h"
23
24std::string _ini_videodriver;
25std::vector<Dimension> _resolutions;
26 Dimension _cur_resolution;
27 bool _rightclick_emulate;
28
29std::string _ini_sounddriver;
30
31std::string _ini_musicdriver;
32
33std::string _ini_blitter;
34 bool _blitter_autodetected;
35
36 static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat";
37
44 const char *GetDriverParam(const StringList &parm, const char *name)
45{
46 if (parm.empty()) return nullptr;
47
48 size_t len = strlen(name);
49 for (auto &p : parm) {
50 if (p.compare(0, len, name) == 0) {
51 if (p.length() == len) return "";
52 if (p[len] == '=') return p.c_str() + len + 1;
53 }
54 }
55 return nullptr;
56}
57
64 bool GetDriverParamBool(const StringList &parm, const char *name)
65{
66 return GetDriverParam(parm, name) != nullptr;
67}
68
76 int GetDriverParamInt(const StringList &parm, const char *name, int def)
77{
78 const char *p = GetDriverParam(parm, name);
79 return p != nullptr ? atoi(p) : def;
80}
81
88 void DriverFactoryBase::SelectDriver(const std::string &name, Driver::Type type)
89{
90 if (!DriverFactoryBase::SelectDriverImpl(name, type)) {
91 name.empty() ?
92 UserError("Failed to autoprobe {} driver", GetDriverTypeName(type)) :
93 UserError("Failed to select requested {} driver '{}'", GetDriverTypeName(type), name);
94 }
95}
96
104 bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
105{
106 if (GetDrivers().empty()) return false;
107
108 if (name.empty()) {
109 /* Probe for this driver, but do not fall back to dedicated/null! */
110 for (int priority = 10; priority > 0; priority--) {
111 for (auto &it : GetDrivers()) {
112 DriverFactoryBase *d = it.second;
113
114 /* Check driver type */
115 if (d->type != type) continue;
116 if (d->priority != priority) continue;
117
118 if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
119
120 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
121 /* Check if we have already tried this driver in last run.
122 * If it is here, it most likely means we crashed. So skip
123 * hardware acceleration. */
124 auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE);
125 if (!filename.empty()) {
126 FioRemove(filename);
127
128 Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name);
129
130 _video_hw_accel = false;
131 ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_HARDWARE_ACCELERATION_CRASH, true);
132 ScheduleErrorMessage(msg);
133 continue;
134 }
135
136 /* Write empty file to note we are attempting hardware acceleration. */
137 FioFOpenFile(HWACCELERATION_TEST_FILE, "w", BASE_DIR);
138 }
139
140 Driver *oldd = *GetActiveDriver(type);
141 Driver *newd = d->CreateInstance();
142 *GetActiveDriver(type) = newd;
143
144 auto err = newd->Start({});
145 if (!err) {
146 Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
147 delete oldd;
148 return true;
149 }
150
151 *GetActiveDriver(type) = oldd;
152 Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, *err);
153 delete newd;
154
155 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
156 _video_hw_accel = false;
157 ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION, true);
158 ScheduleErrorMessage(msg);
159 }
160 }
161 }
162 UserError("Couldn't find any suitable {} driver", GetDriverTypeName(type));
163 } else {
164 /* Extract the driver name and put parameter list in parm */
165 std::istringstream buffer(name);
166 std::string dname;
167 std::getline(buffer, dname, ':');
168
169 std::string param;
170 std::vector<std::string> parms;
171 while (std::getline(buffer, param, ',')) {
172 parms.push_back(param);
173 }
174
175 /* Find this driver */
176 for (auto &it : GetDrivers()) {
177 DriverFactoryBase *d = it.second;
178
179 /* Check driver type */
180 if (d->type != type) continue;
181
182 /* Check driver name */
183 if (!StrEqualsIgnoreCase(dname, d->name)) continue;
184
185 /* Found our driver, let's try it */
186 Driver *newd = d->CreateInstance();
187
188 auto err = newd->Start(parms);
189 if (err) {
190 delete newd;
191 UserError("Unable to load driver '{}'. The error was: {}", d->name, *err);
192 }
193
194 Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name);
195 delete *GetActiveDriver(type);
196 *GetActiveDriver(type) = newd;
197 return true;
198 }
199 UserError("No such {} driver: {}\n", GetDriverTypeName(type), dname);
200 }
201}
202
206 void DriverFactoryBase::MarkVideoDriverOperational()
207{
208 /* As part of the detection whether the GPU driver crashes the game,
209 * and as we are operational now, remove the hardware acceleration
210 * test-file. */
211 auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE);
212 if (!filename.empty()) FioRemove(filename);
213}
214
219 void DriverFactoryBase::GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator)
220{
221 for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
222 fmt::format_to(output_iterator, "List of {} drivers:\n", GetDriverTypeName(type));
223
224 for (int priority = 10; priority >= 0; priority--) {
225 for (auto &it : GetDrivers()) {
226 DriverFactoryBase *d = it.second;
227 if (d->type != type) continue;
228 if (d->priority != priority) continue;
229 fmt::format_to(output_iterator, "{:>18}: {}\n", d->name, d->GetDescription());
230 }
231 }
232
233 fmt::format_to(output_iterator, "\n");
234 }
235}
236
244DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
245 type(type), priority(priority), name(name), description(description)
246{
247 /* Prefix the name with driver type to make it unique */
248 std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
249
250 Drivers &drivers = GetDrivers();
251 assert(drivers.find(typed_name) == drivers.end());
252 drivers.insert(Drivers::value_type(typed_name, this));
253}
254
258DriverFactoryBase::~DriverFactoryBase()
259{
260 /* Prefix the name with driver type to make it unique */
261 std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
262
263 Drivers::iterator it = GetDrivers().find(typed_name);
264 assert(it != GetDrivers().end());
265
266 GetDrivers().erase(it);
267 if (GetDrivers().empty()) delete &GetDrivers();
268}
DriverFactoryBase
Base for all driver factories.
Definition driver.h:57
DriverFactoryBase::MarkVideoDriverOperational
static void MarkVideoDriverOperational()
Mark the current video driver as operational.
Definition driver.cpp:206
DriverFactoryBase::name
std::string_view name
The name of the drivers of this factory.
Definition driver.h:65
DriverFactoryBase::GetDescription
std::string_view GetDescription() const
Get a nice description of the driver-class.
Definition driver.h:138
DriverFactoryBase::Drivers
std::map< std::string, DriverFactoryBase * > Drivers
Type for a map of drivers.
Definition driver.h:68
DriverFactoryBase::GetDrivers
static Drivers & GetDrivers()
Get the map with drivers.
Definition driver.h:73
DriverFactoryBase::UsesHardwareAcceleration
virtual bool UsesHardwareAcceleration() const
Does the driver use hardware acceleration (video-drivers only).
Definition driver.h:114
DriverFactoryBase::SelectDriver
static void SelectDriver(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition driver.cpp:88
DriverFactoryBase::GetDriverTypeName
static std::string_view GetDriverTypeName(Driver::Type type)
Get the driver type name.
Definition driver.h:95
DriverFactoryBase::SelectDriverImpl
static bool SelectDriverImpl(const std::string &name, Driver::Type type)
Find the requested driver and return its class.
Definition driver.cpp:104
DriverFactoryBase::CreateInstance
virtual Driver * CreateInstance() const =0
Create an instance of this driver-class.
DriverFactoryBase::DriverFactoryBase
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description)
Construct a new DriverFactory.
Definition driver.cpp:244
DriverFactoryBase::GetDriversInfo
static void GetDriversInfo(std::back_insert_iterator< std::string > &output_iterator)
Build a human readable list of available drivers, grouped by type.
Definition driver.cpp:219
DriverFactoryBase::type
Driver::Type type
The type of driver.
Definition driver.h:63
DriverFactoryBase::~DriverFactoryBase
virtual ~DriverFactoryBase()
Frees memory used for this->name.
Definition driver.cpp:258
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition driver.h:84
DriverFactoryBase::priority
int priority
The priority of this factory.
Definition driver.h:64
Driver
A driver for communicating with the user.
Definition driver.h:21
Driver::Type
Type
The type of driver.
Definition driver.h:38
Driver::DT_END
@ DT_END
Helper for iteration.
Definition driver.h:43
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition driver.h:42
Driver::DT_BEGIN
@ DT_BEGIN
Helper for iteration.
Definition driver.h:39
Driver::Start
virtual std::optional< std::string_view > Start(const StringList &parm)=0
Start this driver.
ErrorMessageData
The data of the error message.
Definition error.h:31
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition driver.cpp:44
_blitter_autodetected
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
Definition driver.cpp:34
_ini_videodriver
std::string _ini_videodriver
The video driver a stored in the configuration file.
Definition driver.cpp:24
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:76
_ini_musicdriver
std::string _ini_musicdriver
The music driver a stored in the configuration file.
Definition driver.cpp:31
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:25
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:26
_rightclick_emulate
bool _rightclick_emulate
Whether right clicking is emulated.
Definition driver.cpp:27
_ini_blitter
std::string _ini_blitter
The blitter as stored in the configuration file.
Definition driver.cpp:33
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:64
_ini_sounddriver
std::string _ini_sounddriver
The sound driver a stored in the configuration file.
Definition driver.cpp:29
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:36
error.h
Functions related to errors.
ScheduleErrorMessage
void ScheduleErrorMessage(ErrorList &datas)
Schedule a list of errors.
Definition error_gui.cpp:452
error_func.h
Error reporting related functions.
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const std::string &filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
FioRemove
bool FioRemove(const std::string &filename)
Remove a file.
Definition fileio.cpp:328
FioFOpenFile
std::optional< FileHandle > 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:242
fileio_func.h
Functions for Standard In/Out file operations.
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:116
music_driver.hpp
Base for all music playback.
safeguards.h
A number of safeguards to prevent using unsafe methods.
sound_driver.hpp
Base for all sound drivers.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
StrEqualsIgnoreCase
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:347
string_func.h
Functions related to low-level strings.
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:30
_video_hw_accel
bool _video_hw_accel
Whether to consider hardware accelerated video drivers on startup.
Definition video_driver.cpp:25
video_driver.hpp
Base of all video drivers.