2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file driver.cpp Base for all driver handling. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
#include "stdafx.h"
|
2005-07-27 19:57:12 +00:00
|
|
|
#include "debug.h"
|
2021-03-08 14:42:39 +00:00
|
|
|
#include "error.h"
|
2023-04-19 20:47:36 +00:00
|
|
|
#include "error_func.h"
|
2007-07-05 12:23:54 +00:00
|
|
|
#include "sound/sound_driver.hpp"
|
|
|
|
#include "music/music_driver.hpp"
|
|
|
|
#include "video/video_driver.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "string_func.h"
|
2021-03-08 14:42:39 +00:00
|
|
|
#include "table/strings.h"
|
2023-06-04 14:40:17 +00:00
|
|
|
#include "fileio_func.h"
|
2020-05-17 21:32:08 +00:00
|
|
|
#include <sstream>
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2023-06-04 14:40:17 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
std::string _ini_videodriver; ///< The video driver a stored in the configuration file.
|
2019-04-12 16:46:49 +00:00
|
|
|
std::vector<Dimension> _resolutions; ///< List of resolutions.
|
|
|
|
Dimension _cur_resolution; ///< The current resolution.
|
|
|
|
bool _rightclick_emulate; ///< Whether right clicking is emulated.
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
std::string _ini_sounddriver; ///< The sound driver a stored in the configuration file.
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
std::string _ini_musicdriver; ///< The music driver a stored in the configuration file.
|
2008-01-13 21:41:24 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
std::string _ini_blitter; ///< The blitter as stored in the configuration file.
|
2019-04-12 16:46:49 +00:00
|
|
|
bool _blitter_autodetected; ///< Was the blitter autodetected or specified by the user?
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2023-06-04 14:40:17 +00:00
|
|
|
static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat"; ///< Filename to test if we crashed last time we tried to use hardware acceleration.
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get a string parameter the list of parameters.
|
|
|
|
* @param parm The parameters.
|
|
|
|
* @param name The parameter name we're looking for.
|
|
|
|
* @return The parameter value.
|
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
const char *GetDriverParam(const StringList &parm, const char *name)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2020-05-17 21:32:08 +00:00
|
|
|
if (parm.empty()) return nullptr;
|
2005-07-29 06:20:28 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
size_t len = strlen(name);
|
|
|
|
for (auto &p : parm) {
|
|
|
|
if (p.compare(0, len, name) == 0) {
|
|
|
|
if (p.length() == len) return "";
|
|
|
|
if (p[len] == '=') return p.c_str() + len + 1;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get a boolean parameter the list of parameters.
|
|
|
|
* @param parm The parameters.
|
|
|
|
* @param name The parameter name we're looking for.
|
|
|
|
* @return The parameter value.
|
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
bool GetDriverParamBool(const StringList &parm, const char *name)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
return GetDriverParam(parm, name) != nullptr;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get an integer parameter the list of parameters.
|
|
|
|
* @param parm The parameters.
|
|
|
|
* @param name The parameter name we're looking for.
|
|
|
|
* @param def The default value if the parameter doesn't exist.
|
|
|
|
* @return The parameter value.
|
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
int GetDriverParamInt(const StringList &parm, const char *name, int def)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *p = GetDriverParam(parm, name);
|
2019-04-10 21:07:06 +00:00
|
|
|
return p != nullptr ? atoi(p) : def;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the requested driver and return its class.
|
|
|
|
* @param name the driver to select.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param type the type of driver to select
|
2008-04-06 16:49:02 +00:00
|
|
|
* @post Sets the driver so GetCurrentDriver() returns it too.
|
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
void DriverFactoryBase::SelectDriver(const std::string &name, Driver::Type type)
|
2008-04-06 16:49:02 +00:00
|
|
|
{
|
2014-04-28 21:06:51 +00:00
|
|
|
if (!DriverFactoryBase::SelectDriverImpl(name, type)) {
|
2020-05-17 21:32:08 +00:00
|
|
|
name.empty() ?
|
2023-04-19 20:47:36 +00:00
|
|
|
UserError("Failed to autoprobe {} driver", GetDriverTypeName(type)) :
|
2023-06-15 17:15:08 +00:00
|
|
|
UserError("Failed to select requested {} driver '{}'", GetDriverTypeName(type), name);
|
2014-04-28 21:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the requested driver and return its class.
|
|
|
|
* @param name the driver to select.
|
|
|
|
* @param type the type of driver to select
|
|
|
|
* @post Sets the driver so GetCurrentDriver() returns it too.
|
|
|
|
* @return True upon success, otherwise false.
|
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
|
2014-04-28 21:06:51 +00:00
|
|
|
{
|
2023-10-20 18:09:58 +00:00
|
|
|
if (GetDrivers().empty()) return false;
|
2008-04-06 16:49:02 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
if (name.empty()) {
|
2009-06-30 16:20:16 +00:00
|
|
|
/* Probe for this driver, but do not fall back to dedicated/null! */
|
|
|
|
for (int priority = 10; priority > 0; priority--) {
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : GetDrivers()) {
|
|
|
|
DriverFactoryBase *d = it.second;
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
/* Check driver type */
|
|
|
|
if (d->type != type) continue;
|
|
|
|
if (d->priority != priority) continue;
|
|
|
|
|
2021-03-08 14:42:39 +00:00
|
|
|
if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
|
|
|
|
|
2023-06-04 14:40:17 +00:00
|
|
|
if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
|
|
|
|
/* Check if we have already tried this driver in last run.
|
|
|
|
* If it is here, it most likely means we crashed. So skip
|
|
|
|
* hardware acceleration. */
|
|
|
|
auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
unlink(filename.c_str());
|
|
|
|
|
|
|
|
Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name);
|
|
|
|
|
|
|
|
_video_hw_accel = false;
|
|
|
|
ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_HARDWARE_ACCELERATION_CRASH, true);
|
|
|
|
ScheduleErrorMessage(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write empty file to note we are attempting hardware acceleration. */
|
|
|
|
auto f = FioFOpenFile(HWACCELERATION_TEST_FILE, "w", BASE_DIR);
|
|
|
|
FioFCloseFile(f);
|
|
|
|
}
|
|
|
|
|
2014-04-28 21:09:19 +00:00
|
|
|
Driver *oldd = *GetActiveDriver(type);
|
2008-04-06 16:49:02 +00:00
|
|
|
Driver *newd = d->CreateInstance();
|
2014-04-28 21:09:19 +00:00
|
|
|
*GetActiveDriver(type) = newd;
|
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
const char *err = newd->Start({});
|
2019-04-10 21:07:06 +00:00
|
|
|
if (err == nullptr) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
|
2014-04-28 21:09:19 +00:00
|
|
|
delete oldd;
|
2014-04-28 21:06:51 +00:00
|
|
|
return true;
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 21:09:19 +00:00
|
|
|
*GetActiveDriver(type) = oldd;
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, err);
|
2008-04-06 16:49:02 +00:00
|
|
|
delete newd;
|
2021-03-08 14:42:39 +00:00
|
|
|
|
|
|
|
if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
|
|
|
|
_video_hw_accel = false;
|
2023-06-04 14:40:17 +00:00
|
|
|
ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION, true);
|
2021-03-08 14:42:39 +00:00
|
|
|
ScheduleErrorMessage(msg);
|
|
|
|
}
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-19 20:47:36 +00:00
|
|
|
UserError("Couldn't find any suitable {} driver", GetDriverTypeName(type));
|
2008-04-06 16:49:02 +00:00
|
|
|
} else {
|
|
|
|
/* Extract the driver name and put parameter list in parm */
|
2020-05-17 21:32:08 +00:00
|
|
|
std::istringstream buffer(name);
|
|
|
|
std::string dname;
|
|
|
|
std::getline(buffer, dname, ':');
|
|
|
|
|
|
|
|
std::string param;
|
|
|
|
std::vector<std::string> parms;
|
|
|
|
while (std::getline(buffer, param, ',')) {
|
|
|
|
parms.push_back(param);
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find this driver */
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : GetDrivers()) {
|
|
|
|
DriverFactoryBase *d = it.second;
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
/* Check driver type */
|
|
|
|
if (d->type != type) continue;
|
|
|
|
|
|
|
|
/* Check driver name */
|
2023-04-27 09:47:08 +00:00
|
|
|
if (!StrEqualsIgnoreCase(dname, d->name)) continue;
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
/* Found our driver, let's try it */
|
|
|
|
Driver *newd = d->CreateInstance();
|
|
|
|
|
|
|
|
const char *err = newd->Start(parms);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (err != nullptr) {
|
2008-04-06 16:49:02 +00:00
|
|
|
delete newd;
|
2023-04-19 20:47:36 +00:00
|
|
|
UserError("Unable to load driver '{}'. The error was: {}", d->name, err);
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(driver, 1, "Successfully loaded {} driver '{}'", GetDriverTypeName(type), d->name);
|
2008-04-06 16:49:02 +00:00
|
|
|
delete *GetActiveDriver(type);
|
|
|
|
*GetActiveDriver(type) = newd;
|
2014-04-28 21:06:51 +00:00
|
|
|
return true;
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
2023-04-19 20:47:36 +00:00
|
|
|
UserError("No such {} driver: {}\n", GetDriverTypeName(type), dname);
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 14:40:17 +00:00
|
|
|
/**
|
|
|
|
* Mark the current video driver as operational.
|
|
|
|
*/
|
|
|
|
void DriverFactoryBase::MarkVideoDriverOperational()
|
|
|
|
{
|
|
|
|
/* As part of the detection whether the GPU driver crashes the game,
|
|
|
|
* and as we are operational now, remove the hardware acceleration
|
|
|
|
* test-file. */
|
|
|
|
auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE);
|
|
|
|
if (!filename.empty()) unlink(filename.c_str());
|
|
|
|
}
|
|
|
|
|
2008-04-06 16:49:02 +00:00
|
|
|
/**
|
|
|
|
* Build a human readable list of available drivers, grouped by type.
|
2023-05-19 21:22:30 +00:00
|
|
|
* @param output_iterator The iterator to write the string to.
|
2008-04-06 16:49:02 +00:00
|
|
|
*/
|
2023-05-19 21:22:30 +00:00
|
|
|
void DriverFactoryBase::GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator)
|
2008-04-06 16:49:02 +00:00
|
|
|
{
|
|
|
|
for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
|
2023-05-19 21:22:30 +00:00
|
|
|
fmt::format_to(output_iterator, "List of {} drivers:\n", GetDriverTypeName(type));
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
for (int priority = 10; priority >= 0; priority--) {
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : GetDrivers()) {
|
|
|
|
DriverFactoryBase *d = it.second;
|
2008-04-06 16:49:02 +00:00
|
|
|
if (d->type != type) continue;
|
|
|
|
if (d->priority != priority) continue;
|
2023-05-19 21:22:30 +00:00
|
|
|
fmt::format_to(output_iterator, "{:>18}: {}\n", d->name, d->GetDescription());
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 21:22:30 +00:00
|
|
|
fmt::format_to(output_iterator, "\n");
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-11 12:46:28 +00:00
|
|
|
|
2013-11-25 14:26:46 +00:00
|
|
|
/**
|
|
|
|
* Construct a new DriverFactory.
|
|
|
|
* @param type The type of driver.
|
|
|
|
* @param priority The priority within the driver class.
|
|
|
|
* @param name The name of the driver.
|
|
|
|
* @param description A long-ish description of the driver.
|
|
|
|
*/
|
|
|
|
DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
|
|
|
|
type(type), priority(priority), name(name), description(description)
|
|
|
|
{
|
|
|
|
/* Prefix the name with driver type to make it unique */
|
2023-05-31 19:38:18 +00:00
|
|
|
std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
|
2013-11-25 14:26:46 +00:00
|
|
|
|
2021-04-01 09:16:19 +00:00
|
|
|
Drivers &drivers = GetDrivers();
|
2023-05-31 19:38:18 +00:00
|
|
|
assert(drivers.find(typed_name) == drivers.end());
|
|
|
|
drivers.insert(Drivers::value_type(typed_name, this));
|
2013-11-25 14:26:46 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Frees memory used for this->name
|
2008-06-11 12:46:28 +00:00
|
|
|
*/
|
2009-10-04 14:49:39 +00:00
|
|
|
DriverFactoryBase::~DriverFactoryBase()
|
|
|
|
{
|
2008-06-11 12:46:28 +00:00
|
|
|
/* Prefix the name with driver type to make it unique */
|
2023-05-31 19:38:18 +00:00
|
|
|
std::string typed_name = fmt::format("{}{}", GetDriverTypeName(type), name);
|
2008-06-11 12:46:28 +00:00
|
|
|
|
2023-05-31 19:38:18 +00:00
|
|
|
Drivers::iterator it = GetDrivers().find(typed_name);
|
2008-06-24 09:15:45 +00:00
|
|
|
assert(it != GetDrivers().end());
|
|
|
|
|
|
|
|
GetDrivers().erase(it);
|
2008-06-11 12:46:28 +00:00
|
|
|
if (GetDrivers().empty()) delete &GetDrivers();
|
|
|
|
}
|