| 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 <QtCore/QtPlugin> |
| 41 | #include <QtCore/QCoreApplication> |
| 42 | #include <QtCore/QFile> |
| 43 | |
| 44 | #include <QDebug> |
| 45 | #include "qfeedback.h" |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | QFeedbackMMK::QFeedbackMMK() : QObject(qApp) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | QFeedbackMMK::~QFeedbackMMK() |
| 54 | { |
| 55 | foreach(FeedbackInfo fi, mEffects.values()) { |
| 56 | delete fi.soundEffect; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void QFeedbackMMK::setLoaded(QFeedbackFileEffect *effect, bool load) |
| 61 | { |
| 62 | if (!effect) |
| 63 | return; |
| 64 | |
| 65 | // See if we have seen this effect before |
| 66 | FeedbackInfo fi = mEffects.value(effect); |
| 67 | |
| 68 | if (load) { |
| 69 | // Well.. we might already have an effect, since we don't create them until |
| 70 | // we load... |
| 71 | if (fi.loaded) { |
| 72 | // We've already loaded? |
| 73 | return; |
| 74 | } else { |
| 75 | if (fi.soundEffect) { |
| 76 | // We've started a load, they must just be impatient |
| 77 | // Pushing this elevator button does nothing.. |
| 78 | return; |
| 79 | } else { |
| 80 | // New sound effect! |
| 81 | QUrl url = effect->source(); |
| 82 | if (QFile::exists(url.toLocalFile())) { |
| 83 | fi.soundEffect = new QSoundEffect(this); |
| 84 | mEffects.insert(effect, fi); |
| 85 | mEffectMap.insert(fi.soundEffect, effect); |
| 86 | |
| 87 | connect(fi.soundEffect, SIGNAL(statusChanged()), this, SLOT(soundEffectStatusChanged())); |
| 88 | connect(fi.soundEffect, SIGNAL(playingChanged()), this, SLOT(soundEffectPlayingChanged())); |
| 89 | fi.soundEffect->setSource(url); |
| 90 | |
| 91 | // conceptually we're now loading, so we have to do this manually?? |
| 92 | QMetaObject::invokeMethod(effect, "stateChanged"); |
| 93 | } else { |
| 94 | reportLoadFinished(effect, false); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | // Time to unload. |
| 100 | if (fi.soundEffect) { |
| 101 | mEffectMap.remove(fi.soundEffect); |
| 102 | fi.soundEffect->deleteLater(); |
| 103 | } |
| 104 | mEffects.remove(effect); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void QFeedbackMMK::setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state) |
| 109 | { |
| 110 | FeedbackInfo fi = mEffects.value(effect); |
| 111 | switch (state) |
| 112 | { |
| 113 | case QFeedbackEffect::Stopped: |
| 114 | if (fi.playing) { |
| 115 | Q_ASSERT(fi.soundEffect); |
| 116 | fi.playing = false; |
| 117 | mEffects.insert(effect, fi); // overwrite previous version |
| 118 | fi.soundEffect->stop(); |
| 119 | } |
| 120 | break; |
| 121 | |
| 122 | case QFeedbackEffect::Paused: |
| 123 | // Well, we can't pause, really |
| 124 | reportError(effect, QFeedbackEffect::UnknownError); |
| 125 | break; |
| 126 | |
| 127 | case QFeedbackEffect::Running: |
| 128 | if (fi.playing) { |
| 129 | // We're already playing. |
| 130 | } else if (fi.soundEffect) { |
| 131 | fi.playing = true; |
| 132 | mEffects.insert(effect, fi); // overwrite previous version |
| 133 | fi.soundEffect->play(); |
| 134 | } |
| 135 | break; |
| 136 | default: |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | QFeedbackEffect::State QFeedbackMMK::effectState(const QFeedbackFileEffect *effect) |
| 142 | { |
| 143 | FeedbackInfo fi = mEffects.value(effect); |
| 144 | if (fi.soundEffect) { |
| 145 | if (fi.playing) // we might not be loaded, however |
| 146 | return QFeedbackEffect::Running; |
| 147 | if (fi.loaded) |
| 148 | return QFeedbackEffect::Stopped; // No idle? |
| 149 | return QFeedbackEffect::Loading; |
| 150 | } |
| 151 | return QFeedbackEffect::Stopped; |
| 152 | } |
| 153 | |
| 154 | int QFeedbackMMK::effectDuration(const QFeedbackFileEffect *effect) |
| 155 | { |
| 156 | Q_UNUSED(effect); |
| 157 | // XXX This isn't supported by MMK currently |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | QStringList QFeedbackMMK::supportedMimeTypes() |
| 162 | { |
| 163 | return QSoundEffect::supportedMimeTypes(); |
| 164 | } |
| 165 | |
| 166 | void QFeedbackMMK::soundEffectStatusChanged() |
| 167 | { |
| 168 | QSoundEffect* se = qobject_cast<QSoundEffect*>(sender()); |
| 169 | if (se) { |
| 170 | // Hmm, now look up the right sound effect |
| 171 | QFeedbackFileEffect* fe = mEffectMap.value(se); |
| 172 | if (!fe) |
| 173 | return; |
| 174 | |
| 175 | FeedbackInfo fi = mEffects.value(fe); |
| 176 | |
| 177 | switch(se->status()) { |
| 178 | case QSoundEffect::Error: |
| 179 | if (!fi.soundEffect || !fi.loaded) { |
| 180 | // Error before we got loaded, so fail the load |
| 181 | mEffectMap.remove(se); |
| 182 | mEffects.remove(fe); |
| 183 | se->deleteLater(); |
| 184 | reportLoadFinished(fe, false); // this ends our involvement |
| 185 | } else { |
| 186 | reportError(fe, QFeedbackEffect::UnknownError); // this doesn't do much |
| 187 | } |
| 188 | break; |
| 189 | |
| 190 | case QSoundEffect::Ready: |
| 191 | if (fe->state() == QFeedbackEffect::Loading) { |
| 192 | reportLoadFinished(fe, true); |
| 193 | |
| 194 | FeedbackInfo fi = mEffects.value(fe); |
| 195 | fi.loaded = true; |
| 196 | mEffects.insert(fe, fi); |
| 197 | |
| 198 | QMetaObject::invokeMethod(fe, "stateChanged"); |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | // Nothing to handle here? |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void QFeedbackMMK::soundEffectPlayingChanged() |
| 210 | { |
| 211 | QSoundEffect* se = qobject_cast<QSoundEffect*>(sender()); |
| 212 | if (se) { |
| 213 | QFeedbackFileEffect* fileEffect = mEffectMap.value(se); |
| 214 | FeedbackInfo fi = mEffects.value(fileEffect); |
| 215 | |
| 216 | if (fi.soundEffect == se) { |
| 217 | if (fi.playing != se->isPlaying()) { |
| 218 | fi.playing = se->isPlaying(); |
| 219 | mEffects.insert(fileEffect, fi); // overwrite previous version |
| 220 | |
| 221 | QFeedbackFileEffect* fe = mEffectMap.value(se); |
| 222 | // Emit the stateChanged() signal |
| 223 | if (fe) { |
| 224 | QMetaObject::invokeMethod(fe, "stateChanged"); |
| 225 | } |
| 226 | } else { |
| 227 | // Do nothing, internal state is already ok |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | QT_END_NAMESPACE |