| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 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:GPL-EXCEPT$ | 
| 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 General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <qfeedbackactuator.h> | 
| 30 | #include "qfeedbacktestplugin.h" | 
| 31 | #include <QtCore/QtPlugin> | 
| 32 | #include <QtCore/QDebug> | 
| 33 | #include <QtCore/QStringList> | 
| 34 | #include <QtCore/QCoreApplication> | 
| 35 | #include <QtCore/QFile> | 
| 36 | #include <QtCore/QVariant> | 
| 37 | #include <QDebug> | 
| 38 |  | 
| 39 | QFeedbackTestPlugin::QFeedbackTestPlugin() | 
| 40 |     : QObject(qApp), mHapticState(QFeedbackEffect::Stopped), mFileState(QFeedbackEffect::Stopped) | 
| 41 | { | 
| 42 |     actuators_ << createFeedbackActuator(parent: this, id: 0) << createFeedbackActuator(parent: this, id: 1); | 
| 43 | } | 
| 44 |  | 
| 45 | QFeedbackTestPlugin::~QFeedbackTestPlugin() | 
| 46 | { | 
| 47 | } | 
| 48 |  | 
| 49 | QFeedbackInterface::PluginPriority QFeedbackTestPlugin::pluginPriority() | 
| 50 | { | 
| 51 |     return PluginHighPriority; // to make sure we get used | 
| 52 | } | 
| 53 |  | 
| 54 | QList<QFeedbackActuator*> QFeedbackTestPlugin::actuators() | 
| 55 | { | 
| 56 |     return actuators_; | 
| 57 | } | 
| 58 |  | 
| 59 | void QFeedbackTestPlugin::setActuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop, const QVariant &value) | 
| 60 | { | 
| 61 |     Q_UNUSED(actuator) | 
| 62 |     Q_UNUSED(prop) | 
| 63 |     Q_UNUSED(value) | 
| 64 | } | 
| 65 |  | 
| 66 | QVariant QFeedbackTestPlugin::actuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop) | 
| 67 | { | 
| 68 |     Q_UNUSED(actuator) | 
| 69 |  | 
| 70 |     switch (prop) { | 
| 71 |         case Name: | 
| 72 |         if (actuator.id() == 0) | 
| 73 |             return QString(QLatin1String("test plugin" )); | 
| 74 |         else | 
| 75 |             return QString(QLatin1String("5555" )); | 
| 76 |  | 
| 77 |         case State: | 
| 78 |             return static_cast<int>(QFeedbackActuator::Unknown); | 
| 79 |  | 
| 80 |         case Enabled: | 
| 81 |             return false; | 
| 82 |         default: | 
| 83 |             break; | 
| 84 |     } | 
| 85 |  | 
| 86 |     return QVariant(); | 
| 87 | } | 
| 88 |  | 
| 89 | bool QFeedbackTestPlugin::isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability cap) | 
| 90 | { | 
| 91 |     switch (cap) { | 
| 92 |         case QFeedbackActuator::Envelope: | 
| 93 |         case QFeedbackActuator::Period: | 
| 94 |             return true; | 
| 95 |         default: | 
| 96 |             break; | 
| 97 |     } | 
| 98 |  | 
| 99 |     return false; | 
| 100 | } | 
| 101 |  | 
| 102 | QTimer* QFeedbackTestPlugin::ensureTimer(const QFeedbackHapticsEffect* effect) | 
| 103 | { | 
| 104 |     // Yes, this is slow | 
| 105 |     QTimer *t = mHapticEffects.key(avalue: effect); | 
| 106 |     if (!t) { | 
| 107 |         t = new QTimer(); | 
| 108 |         t->setSingleShot(true); | 
| 109 |         t->setInterval(effect->duration()); | 
| 110 |         connect(sender: t, SIGNAL(timeout()), receiver: this, SLOT(timerExpired())); | 
| 111 |         mHapticEffects.insert(akey: t, avalue: effect); | 
| 112 |     } | 
| 113 |     return t; | 
| 114 | } | 
| 115 |  | 
| 116 |  | 
| 117 | void QFeedbackTestPlugin::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty ep) | 
| 118 | { | 
| 119 |     if (ep == QFeedbackHapticsInterface::Duration) { | 
| 120 |         QTimer* t = ensureTimer(effect); | 
| 121 |         t->setInterval(effect->duration()); | 
| 122 |     } | 
| 123 | } | 
| 124 |  | 
| 125 | void QFeedbackTestPlugin::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State state) | 
| 126 | { | 
| 127 |     Q_UNUSED(effect) | 
| 128 |     if (mHapticState != state) { | 
| 129 |         mHapticState = state; | 
| 130 |         QTimer* t = ensureTimer(effect); | 
| 131 |         if (mHapticState == QFeedbackEffect::Running) { | 
| 132 |             t->start(); | 
| 133 |         } else if (mHapticState == QFeedbackEffect::Stopped) { | 
| 134 |             t->stop(); | 
| 135 |         } else if (mHapticState == QFeedbackEffect::Paused) { | 
| 136 |             // In theory should set the duration to the remainder... | 
| 137 |             t->stop(); | 
| 138 |         } | 
| 139 |     } | 
| 140 | } | 
| 141 |  | 
| 142 | QFeedbackEffect::State QFeedbackTestPlugin::effectState(const QFeedbackHapticsEffect *effect) | 
| 143 | { | 
| 144 |     Q_UNUSED(effect) | 
| 145 |     return mHapticState; | 
| 146 | } | 
| 147 |  | 
| 148 | void QFeedbackTestPlugin::timerExpired() | 
| 149 | { | 
| 150 |     mHapticState = QFeedbackEffect::Stopped; | 
| 151 |     // Emit the stateChanged signal | 
| 152 |     const QFeedbackHapticsEffect* effect = mHapticEffects.value(akey: static_cast<QTimer*>(sender())); | 
| 153 |     if (effect) { | 
| 154 |         QMetaObject::invokeMethod(obj: const_cast<QFeedbackHapticsEffect*>(effect), member: "stateChanged" ); | 
| 155 |     } | 
| 156 | } | 
| 157 |  | 
| 158 |  | 
| 159 |  | 
| 160 | void QFeedbackTestPlugin::setLoaded(QFeedbackFileEffect *effect, bool load) | 
| 161 | { | 
| 162 |     if (effect->source() == QUrl("load" )) { | 
| 163 |         // Succeed the load | 
| 164 |         if (load) { | 
| 165 |             mFileState = QFeedbackEffect::Loading; | 
| 166 |             reportLoadFinished(effect, success: true); // not strictly true | 
| 167 |         } else | 
| 168 |             mFileState = QFeedbackEffect::Stopped; | 
| 169 |     } else { | 
| 170 |         // Fail the load | 
| 171 |         if (load) | 
| 172 |             reportLoadFinished(effect, success: false); | 
| 173 |     } | 
| 174 | } | 
| 175 |  | 
| 176 | void QFeedbackTestPlugin::setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state) | 
| 177 | { | 
| 178 |     Q_UNUSED(effect) | 
| 179 |     if (effect->source() == QUrl("load" )) // we only change state for good effects | 
| 180 |         mFileState = state; | 
| 181 | } | 
| 182 |  | 
| 183 | QFeedbackEffect::State QFeedbackTestPlugin::effectState(const QFeedbackFileEffect *effect) | 
| 184 | { | 
| 185 |     Q_UNUSED(effect) | 
| 186 |     return mFileState; | 
| 187 | } | 
| 188 |  | 
| 189 | int QFeedbackTestPlugin::effectDuration(const QFeedbackFileEffect *effect) | 
| 190 | { | 
| 191 |     Q_UNUSED(effect) | 
| 192 |     return 5678; | 
| 193 | } | 
| 194 |  | 
| 195 | QStringList QFeedbackTestPlugin::supportedMimeTypes() | 
| 196 | { | 
| 197 |     return QStringList() << "x-test/this is a test" ; | 
| 198 | } | 
| 199 |  | 
| 200 | bool QFeedbackTestPlugin::play(QFeedbackEffect::Effect effect) | 
| 201 | { | 
| 202 |     if (effect == QFeedbackEffect::Press) | 
| 203 |         return true; | 
| 204 |     else { | 
| 205 |         reportError(0, QFeedbackEffect::UnknownError); | 
| 206 |         return false; | 
| 207 |     } | 
| 208 | } | 
| 209 |  |