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/>.
8 /** @file driver.cpp Base for all driver handling. */
12 #include "sound/sound_driver.hpp"
13 #include "music/music_driver.hpp"
14 #include "video/video_driver.hpp"
15 #include "string_func.h"
17 #include "safeguards.h"
19 char *_ini_videodriver
; ///< The video driver a stored in the configuration file.
20 std::vector
<Dimension
> _resolutions
; ///< List of resolutions.
21 Dimension _cur_resolution
; ///< The current resolution.
22 bool _rightclick_emulate
; ///< Whether right clicking is emulated.
24 char *_ini_sounddriver
; ///< The sound driver a stored in the configuration file.
26 char *_ini_musicdriver
; ///< The music driver a stored in the configuration file.
28 char *_ini_blitter
; ///< The blitter as stored in the configuration file.
29 bool _blitter_autodetected
; ///< Was the blitter autodetected or specified by the user?
32 * Get a string parameter the list of parameters.
33 * @param parm The parameters.
34 * @param name The parameter name we're looking for.
35 * @return The parameter value.
37 const char *GetDriverParam(const char * const *parm
, const char *name
)
41 if (parm
== nullptr) return nullptr;
44 for (; *parm
!= nullptr; parm
++) {
45 const char *p
= *parm
;
47 if (strncmp(p
, name
, len
) == 0) {
48 if (p
[len
] == '=') return p
+ len
+ 1;
49 if (p
[len
] == '\0') return p
+ len
;
56 * Get a boolean parameter the list of parameters.
57 * @param parm The parameters.
58 * @param name The parameter name we're looking for.
59 * @return The parameter value.
61 bool GetDriverParamBool(const char * const *parm
, const char *name
)
63 return GetDriverParam(parm
, name
) != nullptr;
67 * Get an integer parameter the list of parameters.
68 * @param parm The parameters.
69 * @param name The parameter name we're looking for.
70 * @param def The default value if the parameter doesn't exist.
71 * @return The parameter value.
73 int GetDriverParamInt(const char * const *parm
, const char *name
, int def
)
75 const char *p
= GetDriverParam(parm
, name
);
76 return p
!= nullptr ? atoi(p
) : def
;
80 * Find the requested driver and return its class.
81 * @param name the driver to select.
82 * @param type the type of driver to select
83 * @post Sets the driver so GetCurrentDriver() returns it too.
85 void DriverFactoryBase::SelectDriver(const char *name
, Driver::Type type
)
87 if (!DriverFactoryBase::SelectDriverImpl(name
, type
)) {
89 usererror("Failed to autoprobe %s driver", GetDriverTypeName(type
)) :
90 usererror("Failed to select requested %s driver '%s'", GetDriverTypeName(type
), name
);
95 * Find the requested driver and return its class.
96 * @param name the driver to select.
97 * @param type the type of driver to select
98 * @post Sets the driver so GetCurrentDriver() returns it too.
99 * @return True upon success, otherwise false.
101 bool DriverFactoryBase::SelectDriverImpl(const char *name
, Driver::Type type
)
103 if (GetDrivers().size() == 0) return false;
105 if (StrEmpty(name
)) {
106 /* Probe for this driver, but do not fall back to dedicated/null! */
107 for (int priority
= 10; priority
> 0; priority
--) {
108 Drivers::iterator it
= GetDrivers().begin();
109 for (; it
!= GetDrivers().end(); ++it
) {
110 DriverFactoryBase
*d
= (*it
).second
;
112 /* Check driver type */
113 if (d
->type
!= type
) continue;
114 if (d
->priority
!= priority
) continue;
116 Driver
*oldd
= *GetActiveDriver(type
);
117 Driver
*newd
= d
->CreateInstance();
118 *GetActiveDriver(type
) = newd
;
120 const char *err
= newd
->Start(nullptr);
121 if (err
== nullptr) {
122 DEBUG(driver
, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type
), d
->name
);
127 *GetActiveDriver(type
) = oldd
;
128 DEBUG(driver
, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type
), d
->name
, err
);
132 usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type
));
136 const char *parms
[32];
138 /* Extract the driver name and put parameter list in parm */
139 strecpy(buffer
, name
, lastof(buffer
));
140 parm
= strchr(buffer
, ':');
142 if (parm
!= nullptr) {
144 /* Tokenize the parm. */
147 if (np
< lengthof(parms
) - 1) parms
[np
++] = parm
;
148 while (*parm
!= '\0' && *parm
!= ',') parm
++;
149 } while (*parm
== ',');
153 /* Find this driver */
154 Drivers::iterator it
= GetDrivers().begin();
155 for (; it
!= GetDrivers().end(); ++it
) {
156 DriverFactoryBase
*d
= (*it
).second
;
158 /* Check driver type */
159 if (d
->type
!= type
) continue;
161 /* Check driver name */
162 if (strcasecmp(buffer
, d
->name
) != 0) continue;
164 /* Found our driver, let's try it */
165 Driver
*newd
= d
->CreateInstance();
167 const char *err
= newd
->Start(parms
);
168 if (err
!= nullptr) {
170 usererror("Unable to load driver '%s'. The error was: %s", d
->name
, err
);
173 DEBUG(driver
, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type
), d
->name
);
174 delete *GetActiveDriver(type
);
175 *GetActiveDriver(type
) = newd
;
178 usererror("No such %s driver: %s\n", GetDriverTypeName(type
), buffer
);
183 * Build a human readable list of available drivers, grouped by type.
184 * @param p The buffer to write to.
185 * @param last The last element in the buffer.
186 * @return The end of the written buffer.
188 char *DriverFactoryBase::GetDriversInfo(char *p
, const char *last
)
190 for (Driver::Type type
= Driver::DT_BEGIN
; type
!= Driver::DT_END
; type
++) {
191 p
+= seprintf(p
, last
, "List of %s drivers:\n", GetDriverTypeName(type
));
193 for (int priority
= 10; priority
>= 0; priority
--) {
194 Drivers::iterator it
= GetDrivers().begin();
195 for (; it
!= GetDrivers().end(); it
++) {
196 DriverFactoryBase
*d
= (*it
).second
;
197 if (d
->type
!= type
) continue;
198 if (d
->priority
!= priority
) continue;
199 p
+= seprintf(p
, last
, "%18s: %s\n", d
->name
, d
->GetDescription());
203 p
+= seprintf(p
, last
, "\n");
210 * Construct a new DriverFactory.
211 * @param type The type of driver.
212 * @param priority The priority within the driver class.
213 * @param name The name of the driver.
214 * @param description A long-ish description of the driver.
216 DriverFactoryBase::DriverFactoryBase(Driver::Type type
, int priority
, const char *name
, const char *description
) :
217 type(type
), priority(priority
), name(name
), description(description
)
219 /* Prefix the name with driver type to make it unique */
221 strecpy(buf
, GetDriverTypeName(type
), lastof(buf
));
222 strecpy(buf
+ 5, name
, lastof(buf
));
224 const char *longname
= stredup(buf
);
226 std::pair
<Drivers::iterator
, bool> P
= GetDrivers().insert(Drivers::value_type(longname
, this));
231 * Frees memory used for this->name
233 DriverFactoryBase::~DriverFactoryBase()
235 /* Prefix the name with driver type to make it unique */
237 strecpy(buf
, GetDriverTypeName(type
), lastof(buf
));
238 strecpy(buf
+ 5, this->name
, lastof(buf
));
240 Drivers::iterator it
= GetDrivers().find(buf
);
241 assert(it
!= GetDrivers().end());
243 const char *longname
= (*it
).first
;
245 GetDrivers().erase(it
);
248 if (GetDrivers().empty()) delete &GetDrivers();