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"
|
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"
|
2020-05-17 21:32:08 +00:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2005-07-23 15:16:57 +00:00
|
|
|
|
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
|
|
|
|
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() ?
|
2014-04-28 21:06:51 +00:00
|
|
|
usererror("Failed to autoprobe %s driver", GetDriverTypeName(type)) :
|
2020-05-17 21:32:08 +00:00
|
|
|
usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type), name.c_str());
|
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
|
|
|
{
|
|
|
|
if (GetDrivers().size() == 0) 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--) {
|
2008-04-06 16:49:02 +00:00
|
|
|
Drivers::iterator it = GetDrivers().begin();
|
|
|
|
for (; it != GetDrivers().end(); ++it) {
|
|
|
|
DriverFactoryBase *d = (*it).second;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
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;
|
|
|
|
ErrorMessageData msg(STR_VIDEO_DRIVER_ERROR, STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION);
|
|
|
|
ScheduleErrorMessage(msg);
|
|
|
|
}
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-05 20:54:52 +00:00
|
|
|
usererror("Couldn't find any suitable %s 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 */
|
|
|
|
Drivers::iterator it = GetDrivers().begin();
|
|
|
|
for (; it != GetDrivers().end(); ++it) {
|
|
|
|
DriverFactoryBase *d = (*it).second;
|
|
|
|
|
|
|
|
/* Check driver type */
|
|
|
|
if (d->type != type) continue;
|
|
|
|
|
|
|
|
/* Check driver name */
|
2020-05-17 21:32:08 +00:00
|
|
|
if (strcasecmp(dname.c_str(), d->name) != 0) 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;
|
2008-06-05 20:54:52 +00:00
|
|
|
usererror("Unable to load driver '%s'. The error was: %s", 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
|
|
|
}
|
2020-05-17 21:32:08 +00:00
|
|
|
usererror("No such %s driver: %s\n", GetDriverTypeName(type), dname.c_str());
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a human readable list of available drivers, grouped by type.
|
2011-05-01 20:04:09 +00:00
|
|
|
* @param p The buffer to write to.
|
|
|
|
* @param last The last element in the buffer.
|
|
|
|
* @return The end of the written buffer.
|
2008-04-06 16:49:02 +00:00
|
|
|
*/
|
|
|
|
char *DriverFactoryBase::GetDriversInfo(char *p, const char *last)
|
|
|
|
{
|
|
|
|
for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type));
|
2008-04-06 16:49:02 +00:00
|
|
|
|
|
|
|
for (int priority = 10; priority >= 0; priority--) {
|
|
|
|
Drivers::iterator it = GetDrivers().begin();
|
|
|
|
for (; it != GetDrivers().end(); it++) {
|
|
|
|
DriverFactoryBase *d = (*it).second;
|
|
|
|
if (d->type != type) continue;
|
|
|
|
if (d->priority != priority) continue;
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription());
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 14:42:31 +00:00
|
|
|
p += seprintf(p, last, "\n");
|
2008-04-06 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
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 */
|
|
|
|
char buf[32];
|
|
|
|
strecpy(buf, GetDriverTypeName(type), lastof(buf));
|
|
|
|
strecpy(buf + 5, name, lastof(buf));
|
|
|
|
|
2021-04-01 09:16:19 +00:00
|
|
|
Drivers &drivers = GetDrivers();
|
|
|
|
assert(drivers.find(buf) == drivers.end());
|
|
|
|
drivers.insert(Drivers::value_type(buf, 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 */
|
|
|
|
char buf[32];
|
|
|
|
strecpy(buf, GetDriverTypeName(type), lastof(buf));
|
|
|
|
strecpy(buf + 5, this->name, lastof(buf));
|
|
|
|
|
2008-06-24 09:15:45 +00:00
|
|
|
Drivers::iterator it = GetDrivers().find(buf);
|
|
|
|
assert(it != GetDrivers().end());
|
|
|
|
|
|
|
|
GetDrivers().erase(it);
|
2008-06-11 12:46:28 +00:00
|
|
|
if (GetDrivers().empty()) delete &GetDrivers();
|
|
|
|
}
|