1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtFeedback module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qfeedbackplugininterfaces.h"
#include "qfeedbackplugin_p.h"
#include "qfeedbackeffect_p.h"
#include "qfeedbackpluginsearch.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QStringList>
#include <QtCore/QDir>
#include <QtCore/QPluginLoader>
#include <QtCore/QHash>
#include <QDebug>
QT_BEGIN_NAMESPACE
/*!
\class QFeedbackInterface
\ingroup feedback
\inmodule QtFeedback
\brief The QFeedbackInterface class is the base class for plugins providing feedback.
This interface gives the possibility to report errors from within a backend plugin.
*/
/*!
\fn QFeedbackInterface::reportError(const QFeedbackEffect *effect, QFeedbackEffect::ErrorType error)
Allows a plugin to report the specified \a error whenever necessary. Errors most likely can happen
trying to play or pause an effect, which should be supplied as the parameter \a effect.
*/
/*!
\enum QFeedbackInterface::PluginPriority
This enum describes the priority that the plugin should have in case more than one of the same type (Haptics or Theme) is found.
If more than one plugin has the same priority, the first one that has been loaded will be used. However, multiple
file effect plugins can be loaded at the same time.
\value PluginLowPriority The plugin will have a low priority. This is usually the case for
platform specific-APIs.
\value PluginNormalPriority The plugin will have a normal priority.
This is usually the case for advanced technologies.
\value PluginHighPriority The plugin will have higher priority. Use this priority if you
want your own plugin to be used.
*/
void QFeedbackInterface::reportError(const QFeedbackEffect *effect, QFeedbackEffect::ErrorType error)
{
if (effect)
emit effect->error(error);
}
// These are really useless docs, so I've marked them as internal
/*!
\internal
\fn QFeedbackThemeInterface::~QFeedbackThemeInterface()
Destroys any resources used by this interface.
*/
/*!
\internal
\fn QFeedbackFileInterface::~QFeedbackFileInterface()
Destroys any resources used by this interface.
*/
/*!
\internal
\fn QFeedbackHapticsInterface::~QFeedbackHapticsInterface()
Destroys any resources used by this interface.
*/
template <class T>
class BackendLoader
{
public:
BackendLoader() : inst(0) { }
~BackendLoader() {
#if QT_CONFIG(library)
pl.unload();
#endif
}
void setInstance(T *newInst) { inst = newInst; }
T * instance() { return inst; }
void tryLoad(QPluginLoader &loader)
{
#if QT_CONFIG(library)
if (T *newInst = qobject_cast<T*>(loader.instance())) {
if (!inst || inst->pluginPriority() < newInst->pluginPriority()) {
inst = newInst;
pl.unload(); //release any reference to a previous plugin instance
pl.setFileName(loader.fileName());
pl.load(); //Adds a ref to the library
}
}
#else
Q_UNUSED(loader)
#endif
}
private:
QPluginLoader pl;
T *inst;
};
class FileBackend : public QFeedbackFileInterface
{
public:
FileBackend()
{
}
//this class is used to redirect the calls to all the file backends available
virtual void setLoaded(QFeedbackFileEffect *effect, bool load)
{
if (load) {
//start loading
tryBackendLoad(effect);
} else {
//unload
if (QFeedbackFileInterface *subBackend = getBackend(effect))
subBackend->setLoaded(effect, load);
QFeedbackFileEffectPrivate::get(effect)->loadFinished(false); // make sure it's marked unloaded [XXX this isn't allowed to fail!]
}
}
virtual void setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state)
{
if (QFeedbackFileInterface *subBackend = getBackend(effect))
subBackend->setEffectState(effect, state);
else
QFeedbackInterface::reportError(effect, QFeedbackEffect::UnknownError);
}
virtual QFeedbackEffect::State effectState(const QFeedbackFileEffect *effect)
{
if (QFeedbackFileInterface *subBackend = getBackend(effect))
return subBackend->effectState(effect);
return QFeedbackEffect::Stopped;
}
virtual int effectDuration(const QFeedbackFileEffect *effect)
{
if (QFeedbackFileInterface *subBackend = getBackend(effect))
return subBackend->effectDuration(effect);
return 0;
}
virtual QStringList supportedMimeTypes()
{
QStringList ret;
for (int i = 0; i < subBackends.count(); ++i) {
ret += subBackends.at(i)->supportedMimeTypes();
}
return ret;
}
void addFileBackend(QFeedbackFileInterface *backend)
{
subBackends.append(backend);
}
void reportLoadFinished(QFeedbackFileEffect *effect, bool success)
{
if (success) {
//the file was loaded by the current backend
QFeedbackFileEffectPrivate::get(effect)->loadFinished(true);
return;
}
//let's try the next backend
tryBackendLoad(effect);
}
private:
QList<QFeedbackFileInterface*> subBackends;
QFeedbackFileInterface *getBackend(const QFeedbackFileEffect *effect)
{
const QFeedbackFileEffectPrivate *priv = QFeedbackFileEffectPrivate::get(effect);
if (priv->backendUsed >= 0 && priv->backendUsed < subBackends.count())
return subBackends.at(priv->backendUsed);
return 0;
}
void tryBackendLoad(QFeedbackFileEffect *effect)
{
QFeedbackFileEffectPrivate *p = QFeedbackFileEffectPrivate::get(effect);
p->backendUsed++;
//let's try to load the file
if (p->backendUsed >= subBackends.count()) {
//the file couldn't be loaded
p->loadFinished(false);
reportError(effect, QFeedbackEffect::UnknownError);
// Do a state change as well, (to stopped)
QMetaObject::invokeMethod(effect, "stateChanged");
return;
}
subBackends.at(p->backendUsed)->setLoaded(effect, true);
//now we're waiting for the reply (call to asyncLoadFinished)
}
};
class BackendManager
{
public:
BackendManager()
{
#if QT_CONFIG(library)
QStringList pluginPaths = getPluginPaths(QLatin1String("feedback"));
foreach (const QString& pluginPath, pluginPaths) {
QPluginLoader loader(pluginPath);
hapticsBackend.tryLoad(loader);
themeBackend.tryLoad(loader);
if (QFeedbackFileInterface *newFile = qobject_cast<QFeedbackFileInterface*>(loader.instance())) {
fileBackend.addFileBackend(newFile);
} else {
loader.unload();
}
}
#endif
if (!hapticsBackend.instance())
hapticsBackend.setInstance(new QDummyBackend);
}
QFeedbackHapticsInterface* hapticsBackendInstance()
{
return hapticsBackend.instance();
}
QFeedbackThemeInterface* themeBackendInstance()
{
return themeBackend.instance();
}
FileBackend *fileBackendInstance()
{
return &fileBackend;
}
private:
BackendLoader<QFeedbackHapticsInterface> hapticsBackend;
BackendLoader<QFeedbackThemeInterface> themeBackend;
FileBackend fileBackend;
};
Q_GLOBAL_STATIC(BackendManager, backendManager)
/*!
\class QFeedbackHapticsInterface
\ingroup feedback
\brief The QFeedbackHapticsInterface class is the base class for plugins providing custom haptics effects.
This interface will be used to try to play custom effects with specific duration, intensity, envelope and period.
An effect is always played on a specified actuator.
*/
/*!
\enum QFeedbackHapticsInterface::EffectProperty
This enum describes all effect properties for haptics effects.
\value Duration The effect duration (in milliseconds)
\value Intensity The effect intensity
\value AttackTime The effect attack time (in milliseconds)
\value AttackIntensity The effect attack intensity
\value FadeTime The effect fade time (in milliseconds)
\value FadeIntensity The effect fade intensity
\value Period The effect period, this is an optional effect property.
*/
/*!
\enum QFeedbackHapticsInterface::ActuatorProperty
This enum describes all actuator properties.
\value Name The actuator name.
\value State The actuator state.
\value Enabled The actuator enabled state.
*/
/*!
\fn QFeedbackHapticsInterface::actuators()
Return the available actuators provided by this plugin. The ownership of the actuator objects stays with the plugin.
*/
/*!
\fn QFeedbackHapticsInterface::pluginPriority()
Returns the priority for the plugin.
\sa QFeedbackInterface::PluginPriority
*/
// XXX TODO.. these should have been pointers to QFA :/
/*!
\fn QFeedbackHapticsInterface::setActuatorProperty(const QFeedbackActuator& actuator, ActuatorProperty property, const QVariant & value)
Sets a \a value for \a property on the \a actuator.
\sa ActuatorProperty
*/
/*!
\fn QFeedbackHapticsInterface::actuatorProperty(const QFeedbackActuator & actuator, ActuatorProperty property)
Returns the value for the \a property for an \a actuator.
\sa ActuatorProperty
*/
/*!
\fn QFeedbackHapticsInterface::isActuatorCapabilitySupported(const QFeedbackActuator &actuator, QFeedbackActuator::Capability capability)
Returns true if the \a actuator supports the \a capability.
*/
/*!
\fn QFeedbackHapticsInterface::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty property)
Tells the backend that the \a property has been updated for the supplied \a effect.
*/
/*!
\fn QFeedbackHapticsInterface::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State state)
Sets the state to \a state for the effect \a effect. If that fails the backend should report an error by
calling reportError and \a effect will in turn emit an error signal.
*/
/*!
\fn QFeedbackHapticsInterface::effectState(const QFeedbackHapticsEffect *effect)
Get the current state for the effect \a effect.
*/
/*!
\internal
\fn QFeedbackHapticsInterface::instance()
Returns the instance of the object managing haptics custom effects.
If no backend has been loaded, this will return a null pointer.
*/
QFeedbackHapticsInterface *QFeedbackHapticsInterface::instance()
{
return backendManager()->hapticsBackendInstance();
}
/*!
\fn QFeedbackHapticsInterface::createFeedbackActuator(QObject *parent, int id)
Creates an instance of QFeedbackActuator with the identifier \a id and parent \a parent. This allows
backends to create instances of actuators. It is then up to the each backend to manage
the identifiers according to its needs.
*/
QFeedbackActuator* QFeedbackHapticsInterface::createFeedbackActuator(QObject* parent, int id)
{
return new QFeedbackActuator(parent, id);
}
/*!
\class QFeedbackThemeInterface
\ingroup feedback
\brief The QFeedbackThemeInterface class is the base class for plugins providing themed effects.
They can be of any nature (tactile, audio...).
This simple interface will be used to play those themed effects by a simple call to the play method.
*/
/*!
\fn QFeedbackThemeInterface::pluginPriority()
Returns the priority for the plugin.
*/
/*!
\fn QFeedbackThemeInterface::play(QFeedbackEffect::Effect effect)
Plays the theme effect \a effect. Returns false in case of an error.
*/
/*!
\internal
\fn QFeedbackThemeInterface::instance()
Returns the instance of the object managing theme effects.
If no backend has been loaded, this will return a null pointer.
*/
QFeedbackThemeInterface *QFeedbackThemeInterface::instance()
{
return backendManager()->themeBackendInstance();
}
/*!
\internal
\class QFeedbackFileInterface
\ingroup feedback
\brief The QFeedbackFileInterface class is the base class for plugins providing support for effects stored in files.
They can be of any nature (tactile, audio...). As it is possible to load many different file types using
different technologies, all the backend plugins exposing this interface will be loaded at the same time.
When loading a file all the backend will be tried in order until one can load the file. It is thus very important
that the backends return a load status as soon as possible to not take a too long time to load a file.
*/
/*!
\internal
\fn QFeedbackFileInterface::setLoaded(QFeedbackFileEffect* effect, bool value)
Sets the state of the effect \a effect to be loaded if \a value is true, otherwise unloaded.
Loading a file is asynchronous. Once the backend knows if it has loaded or can't load the file, it must
call the reportLoadFinished function.
*/
/*!
\internal
\fn QFeedbackFileInterface::setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state)
Sets the state of \a effect to \a state.
*/
/*!
\internal
\fn QFeedbackFileInterface::effectState(const QFeedbackFileEffect *effect)
Returns the current state of the effect \a effect.
*/
/*!
\internal
\fn QFeedbackFileInterface::effectDuration(const QFeedbackFileEffect *effect)
Return the duration of \a effect, in milliseconds.
It should return \l QFeedbackEffect::Infinite in case the duration is infinite, or 0 if undefined or unknown.
*/
/*!
\internal
\fn QFeedbackFileInterface::supportedMimeTypes()
Returns a list of the MIME types supported by this plugin.
*/
/*!
\internal
\fn QFeedbackFileInterface::instance()
Returns the instance of the object managing theme effects.
Even if no backend has been loaded, this will never return a null pointer.
*/
QFeedbackFileInterface *QFeedbackFileInterface::instance()
{
return backendManager()->fileBackendInstance();
}
/*!
\internal
\fn QFeedbackFileInterface::reportLoadFinished(QFeedbackFileEffect *effect, bool success)
This is the function the backend should call when it has finished trying to load the effect \a effect.
As loading a file is asynchronous and multiple plugins are attempted after each other, the
backend has to call this function in order for the process to perform smoothly.
The success of the operation is indicated by the \a success parameter.
*/
void QFeedbackFileInterface::reportLoadFinished(QFeedbackFileEffect *effect, bool success)
{
backendManager()->fileBackendInstance()->reportLoadFinished(effect, success);
}
QT_END_NAMESPACE
|