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