Debian Codegraph @

src/feedback/qfeedbackplugin.cpp

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