Codechange: Use unique_ptr throughout instead of new raw pointer for company news...
[openttd-github.git] / src / driver.cpp
blobf9c68fc2661aeb2034879778a2f80b945564a925
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 */
8 /** @file driver.cpp Base for all driver handling. */
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>
22 #include "safeguards.h"
24 std::string _ini_videodriver; ///< The video driver a stored in the configuration file.
25 std::vector<Dimension> _resolutions; ///< List of resolutions.
26 Dimension _cur_resolution; ///< The current resolution.
27 bool _rightclick_emulate; ///< Whether right clicking is emulated.
29 std::string _ini_sounddriver; ///< The sound driver a stored in the configuration file.
31 std::string _ini_musicdriver; ///< The music driver a stored in the configuration file.
33 std::string _ini_blitter; ///< The blitter as stored in the configuration file.
34 bool _blitter_autodetected; ///< Was the blitter autodetected or specified by the user?
36 static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat"; ///< Filename to test if we crashed last time we tried to use hardware acceleration.
38 /**
39 * Get a string parameter the list of parameters.
40 * @param parm The parameters.
41 * @param name The parameter name we're looking for.
42 * @return The parameter value.
44 const char *GetDriverParam(const StringList &parm, const char *name)
46 if (parm.empty()) return nullptr;
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;
55 return nullptr;
58 /**
59 * Get a boolean parameter the list of parameters.
60 * @param parm The parameters.
61 * @param name The parameter name we're looking for.
62 * @return The parameter value.
64 bool GetDriverParamBool(const StringList &parm, const char *name)
66 return GetDriverParam(parm, name) != nullptr;
69 /**
70 * Get an integer parameter the list of parameters.
71 * @param parm The parameters.
72 * @param name The parameter name we're looking for.
73 * @param def The default value if the parameter doesn't exist.
74 * @return The parameter value.
76 int GetDriverParamInt(const StringList &parm, const char *name, int def)
78 const char *p = GetDriverParam(parm, name);
79 return p != nullptr ? atoi(p) : def;
82 /**
83 * Find the requested driver and return its class.
84 * @param name the driver to select.
85 * @param type the type of driver to select
86 * @post Sets the driver so GetCurrentDriver() returns it too.
88 void DriverFactoryBase::SelectDriver(const std::string &name, Driver::Type type)
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);
97 /**
98 * Find the requested driver and return its class.
99 * @param name the driver to select.
100 * @param type the type of driver to select
101 * @post Sets the driver so GetCurrentDriver() returns it too.
102 * @return True upon success, otherwise false.
104 bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
106 if (GetDrivers().empty()) return false;
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;
114 /* Check driver type */
115 if (d->type != type) continue;
116 if (d->priority != priority) continue;
118 if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
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);
128 Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name);
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;
136 /* Write empty file to note we are attempting hardware acceleration. */
137 FioFOpenFile(HWACCELERATION_TEST_FILE, "w", BASE_DIR);
140 Driver *oldd = *GetActiveDriver(type);
141 Driver *newd = d->CreateInstance();
142 *GetActiveDriver(type) = newd;
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;
151 *GetActiveDriver(type) = oldd;
152 Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, *err);
153 delete newd;
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);
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, ':');
169 std::string param;
170 std::vector<std::string> parms;
171 while (std::getline(buffer, param, ',')) {
172 parms.push_back(param);
175 /* Find this driver */
176 for (auto &it : GetDrivers()) {
177 DriverFactoryBase *d = it.second;
179 /* Check driver type */
180 if (d->type != type) continue;
182 /* Check driver name */
183 if (!StrEqualsIgnoreCase(dname, d->name)) continue;
185 /* Found our driver, let's try it */
186 Driver *newd = d->CreateInstance();
188 auto err = newd->Start(parms);
189 if (err) {
190 delete newd;
191 UserError("Unable to load driver '{}'. The error was: {}", d->name, *err);
194 Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name);
195 delete *GetActiveDriver(type);
196 *GetActiveDriver(type) = newd;
197 return true;
199 UserError("No such {} driver: {}\n", GetDriverTypeName(type), dname);
204 * Mark the current video driver as operational.
206 void DriverFactoryBase::MarkVideoDriverOperational()
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);
216 * Build a human readable list of available drivers, grouped by type.
217 * @param output_iterator The iterator to write the string to.
219 void DriverFactoryBase::GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator)
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));
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());
233 fmt::format_to(output_iterator, "\n");
238 * Construct a new DriverFactory.
239 * @param type The type of driver.
240 * @param priority The priority within the driver class.
241 * @param name The name of the driver.
242 * @param description A long-ish description of the driver.
244 DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
245 type(type), priority(priority), name(name), description(description)
247 /* Prefix the name with driver type to make it unique */
248 std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
250 Drivers &drivers = GetDrivers();
251 assert(drivers.find(typed_name) == drivers.end());
252 drivers.insert(Drivers::value_type(typed_name, this));
256 * Frees memory used for this->name
258 DriverFactoryBase::~DriverFactoryBase()
260 /* Prefix the name with driver type to make it unique */
261 std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
263 Drivers::iterator it = GetDrivers().find(typed_name);
264 assert(it != GetDrivers().end());
266 GetDrivers().erase(it);
267 if (GetDrivers().empty()) delete &GetDrivers();