summaryrefslogtreecommitdiffstats
path: root/plugins/feedback/symbian/qfeedback_chwrmhaptics.cpp
blob: 448845aafcd131876a5d4d9e9678aaf0f1ce054f (plain)
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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <qfeedbackactuator.h>
#include "qfeedback_symbian.h"
#include <QtCore/QVariant>
#include <QtCore/QFile>
#include <QtCore/QtPlugin>
#include <QtGui/QApplication>

#include <hwrmhaptics.h>
#include <hwrmlogicalactuators.h>

#define MAX_FILE_SIZE 16535 // XXX TODO: what should this be?
#define VIBE_MAX_MAGNITUDE 10000 // XXX TODO: what should this be?

/* ============================== Common Functions ============================== */
CHWRMHaptics *QFeedbackSymbian::haptics(int actuator)
{
    THWRMLogicalActuators logicalActuator = static_cast<THWRMLogicalActuators>(actuator);
    CHWRMHaptics* haptics = m_haptics.value(logicalActuator);
    if (!haptics) {
        QT_TRAP_THROWING(haptics = CHWRMHaptics::NewL(this, 0));
        if (haptics) {
            // no recovery is possible if this fails.
            QT_TRAP_THROWING(haptics->OpenActuatorL(logicalActuator));

            // Add our new haptics handle to our internal hash.
            m_haptics.insert(logicalActuator, haptics);
        }
    }

    return haptics;
}

/* ============================== Haptics Interface Functions ============================== */

void QFeedbackSymbian::setActuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop, const QVariant &value)
{
    // set the device property for the actuator.
    switch(prop)
    {
    case Enabled:
        TRAP_IGNORE(haptics(actuator.id())->SetDeviceProperty(CHWRMHaptics::EHWRMHapticsDisableEffects, (!(value.toBool()))));
        // fall through.
    default:
        break; // we don't allow setting the name or the state.
    }
}

QVariant QFeedbackSymbian::actuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop)
{
    // return the device property for the actuator.
    TInt value(0);
    switch(prop)
    {
        case Name:
        {
            return QString("chwrmhaptics:actuator:%1").arg(actuator.id());
        }
        case State:
        {
            TInt status = 0;
            TRAP_IGNORE(status = haptics(actuator.id())->HapticsStatus());
            switch (status)
            {
            case MHWRMHapticsObserver::EHWRMHapticsStatusAvailable:
                return QFeedbackActuator::Ready;
            case MHWRMHapticsObserver::EHWRMHapticsStatusReserved:
                return QFeedbackActuator::Busy;
            case MHWRMHapticsObserver::EHWRMHapticsStatusSuspended:
                return QFeedbackActuator::Busy; // a higher priority client is using the actuator.
            default:
                break;
            }
            return QFeedbackActuator::Unknown;
        }
        case Enabled:
        {
            TRAP_IGNORE(haptics(actuator.id())->GetDeviceProperty(CHWRMHaptics::EHWRMHapticsDisableEffects, value));
            return (!value);
        }
        default:
        {
            break; // unknown property type.  Just return 0 (false).
        }
    }

    return QVariant::fromValue(value);
}

bool QFeedbackSymbian::isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability cap)
{
    switch(cap)
    {
    case QFeedbackActuator::Envelope:
    case QFeedbackActuator::Period:
        return true;
    default:
        break;
    }
    return false;
}

void QFeedbackSymbian::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty prop)
{
    TInt errorCode(KErrNone);
    TInt effectHandle = effectHandles.value(effect);

    if (prop == Period) {
        // We cannot change the period of a playing effect.
        reportError(effect, QFeedbackEffect::DeviceBusy);
        return;
    }

    // If we get here, it was some property other than Period which needed modification.
    // In this case, we can modify the effect directly, without stopping and then recreating it.
    if (effect->period() == -1) { // magnitude sweep (non-periodic) effect
        // reconstruct the effect with the updated parameters
        CHWRMHaptics::THWRMHapticsMagSweepEffect magnitudeSweepEffect;
        magnitudeSweepEffect.iDuration = effect->duration(); // the ModifyPlayingXxxEffect() function is smart enough to modify the end time without resetting the play time.
        magnitudeSweepEffect.iMagnitude = qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE));
        magnitudeSweepEffect.iStyle = CHWRMHaptics::EHWRMHapticsStyleSmooth; // XXX TODO: support this?
        magnitudeSweepEffect.iAttackTime = effect->attackTime();
        magnitudeSweepEffect.iAttackLevel = qRound(effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE));
        magnitudeSweepEffect.iFadeTime = effect->fadeTime();
        magnitudeSweepEffect.iFadeLevel = qRound(effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));

        // update the effect
        TRAP(errorCode, haptics(effect->actuator()->id())->ModifyPlayingMagSweepEffect(effectHandle, magnitudeSweepEffect));
    } else { // periodic effect
        CHWRMHaptics::THWRMHapticsPeriodicEffect periodicEffect;
        periodicEffect.iDuration = effect->duration(); // the ModifyPlayingXxxEffect() function is smart enough to modify the end time without resetting the play time.
        periodicEffect.iMagnitude = qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE));
        periodicEffect.iPeriod = effect->period();
        periodicEffect.iStyle = CHWRMHaptics::EHWRMHapticsStyleSmooth; // XXX TODO: support this?
        periodicEffect.iAttackTime = effect->attackTime();
        periodicEffect.iAttackLevel = qRound(effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE));
        periodicEffect.iFadeTime = effect->fadeTime();
        periodicEffect.iFadeLevel = qRound(effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));

        // update the effect
        TRAP(errorCode, haptics(effect->actuator()->id())->ModifyPlayingPeriodicEffect(effectHandle, periodicEffect));
    }

    if (errorCode != KErrNone) {
        reportError(effect, QFeedbackEffect::UnknownError);
    }
}

void QFeedbackSymbian::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State newState)
{
    // retrieve the handle for this effect.
    TInt effectHandle = effectHandles.value(effect);

    TInt err(KErrNone);
    switch(newState)
    {
    case QFeedbackEffect::Stopped:
        TRAP(err, haptics(effect->actuator()->id())->StopPlayingEffect(effectHandle));
        if (err == KErrNone) {
            // we remove the effect from our hashes if it's stopped.
            effectHandles.remove(effect);
            handleEffects.remove(effectHandle);
        }
        break;
    case QFeedbackEffect::Paused:
        TRAP(err, haptics(effect->actuator()->id())->PausePlayingEffect(effectHandle));
        break;
    case QFeedbackEffect::Running:
        if (effect->state() == QFeedbackEffect::Paused) {
            // if it was paused, we can resume it by specifying the handle
            TRAP(err, haptics(effect->actuator()->id())->ResumePausedEffect(effectHandle));
        } else {
            // otherwise, we will receive a new handle from the PlayXxxEffect() functions
            if (effect->period() == -1) {
                // reconstruct the effect with the updated parameters
                CHWRMHaptics::THWRMHapticsMagSweepEffect magnitudeSweepEffect;
                magnitudeSweepEffect.iDuration = effect->duration();
                magnitudeSweepEffect.iMagnitude = qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE));
                magnitudeSweepEffect.iStyle = CHWRMHaptics::EHWRMHapticsStyleSmooth; // XXX TODO: support this?
                magnitudeSweepEffect.iAttackTime = effect->attackTime();
                magnitudeSweepEffect.iAttackLevel = qRound(effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE));
                magnitudeSweepEffect.iFadeTime = effect->fadeTime();
                magnitudeSweepEffect.iFadeLevel = qRound(effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
                TRAP(err, haptics(effect->actuator()->id())->PlayMagSweepEffect(magnitudeSweepEffect, effectHandle));
            } else {
                // find the handle for the effect
                CHWRMHaptics::THWRMHapticsPeriodicEffect periodicEffect;
                periodicEffect.iDuration = effect->duration();
                periodicEffect.iMagnitude = qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE));
                periodicEffect.iPeriod = effect->period();
                periodicEffect.iStyle = CHWRMHaptics::EHWRMHapticsStyleSmooth; // XXX TODO: support this?
                periodicEffect.iAttackTime = effect->attackTime();
                periodicEffect.iAttackLevel = qRound(effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE));
                periodicEffect.iFadeTime = effect->fadeTime();
                periodicEffect.iFadeLevel = qRound(effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
                TRAP(err, haptics(effect->actuator()->id())->PlayPeriodicEffect(periodicEffect, effectHandle));
            }
            if (err == KErrNone) {
                // replacement insert operation.
                effectHandles.insert(effect, effectHandle);
                handleEffects.insert(effectHandle, effect);
            }
	}
        // fall through
    default:
        break;
    }

    // ugly hack: after starting the effect, we must update the intensity, or it doesn't work.  Bug?
    updateEffectProperty(effect, Intensity);

    if (err != KErrNone)
        reportError(effect, QFeedbackEffect::UnknownError);
}

QFeedbackEffect::State QFeedbackSymbian::effectState(const QFeedbackHapticsEffect *effect)
{
    TInt handle = effectHandles.value(effect);
    if (handle) {
        TInt state = CHWRMHaptics::EHWRMHapticsEffectNotPlaying;
        if (haptics(effect->actuator()->id())->GetEffectState(handle, state) == KErrNone) {
            switch (state) {
                case CHWRMHaptics::EHWRMHapticsEffectNotPlaying:
                default:
                    return QFeedbackEffect::Stopped;

                case CHWRMHaptics::EHWRMHapticsEffectPlaying:
                    return QFeedbackEffect::Running;

                case CHWRMHaptics::EHWRMHapticsEffectPaused:
                    return QFeedbackEffect::Paused;
            }
        }
    }
    return QFeedbackEffect::Stopped;
}

/* ============================== File Interface Functions ============================== */

CHWRMHaptics* QFeedbackSymbian::fileHaptics()
{
    // XXX Which actuator for file effects?
    // XXX TODO: add actuator selection API to QFeedbackFileEffect.
    return haptics(EHWRMLogicalActuatorAny);
}

// MHWRMHapticsObserver functions
void QFeedbackSymbian::EffectCompletedL(TInt error, TInt handle)
{
    Q_UNUSED(error); // The observer feedback function isn't called currently, so ignoring the error should be safe for now.
    QFeedbackEffect* effect = const_cast<QFeedbackEffect*>(handleEffects.value(handle));
    if (effect) {
        QMetaObject::invokeMethod(effect, "stateChanged");
    }
}

void QFeedbackSymbian::HapticsStatusChangedL(THWRMHapticsStatus aStatus)
{
    Q_UNUSED(aStatus);
    // We don't currently care.  Perhaps an actuator property should change?

}

void QFeedbackSymbian::setLoaded(QFeedbackFileEffect* effect, bool load)
{
    const QUrl url = effect->source();

    // This doesn't handle qrc urls..
    const QString fileName = url.toLocalFile();
    if (fileName.isEmpty())
        return;

    if (!load && !fileData.contains(fileName))
        return;

    FileContent &fc = fileData[fileName];

    if (load) {
        bool success = true;
        fc.refCount++;
        if (fc.refCount == 1) {
            // We need to load the data and put it into haptics
            QFile file(fileName);
            success = false;
            if (file.size() < MAX_FILE_SIZE && file.open(QIODevice::ReadOnly)) {
                fc.ba = file.readAll();
                // XXX can we clear fc.ba after the load?
                if (fileHaptics()->LoadEffectData(fc.constData(), fc.fileHandle) == KErrNone) {
                    success = true;
                } else {
                    fileData.remove(fileName);
                }
            }
        }
        reportLoadFinished(effect, success);
    } else {
        //unload
        fc.refCount--;
        if (fc.refCount == 0) {
            fileHaptics()->DeleteEffectData(fc.fileHandle);
            fileData.remove(fileName);
        }

        // Also get rid of effectHandles
        TInt effectHandle = effectHandles.value(effect);
        if (effectHandle) {
            handleEffects.remove(effectHandle);
            effectHandles.remove(effect);
        }
    }
}

void QFeedbackSymbian::setEffectState(QFeedbackFileEffect * effect, QFeedbackEffect::State state)
{
    TInt effectHandle = effectHandles.value(effect);
    TInt status = 0;

    switch (state)
    {
    case QFeedbackEffect::Stopped:
        if (effectHandle) {
            status = fileHaptics()->StopPlayingEffect(effectHandle);
            // The handle is invalid after calling stop() on it; remove it from our hashes.
            effectHandles.remove(effect);
            handleEffects.remove(effectHandle);
        }
        break;
    case QFeedbackEffect::Paused:
        if (effectHandle)
            status = fileHaptics()->PausePlayingEffect(effectHandle);
        break;
    case QFeedbackEffect::Running:
        if (effectHandle) {
            // If the effect handle exists, we're resuming
            status = fileHaptics()->ResumePausedEffect(effectHandle);
        } else {
            // Assuming we've loaded the effect, we just need to start it
            QString fileName = effect->source().toLocalFile();
            Q_ASSERT(fileData.contains(fileName)); // false if we haven't loaded it yet (prevented by upper levels?)

            status = fileHaptics()->PlayEffect(fileData[fileName].fileHandle, 0, effectHandle);
            if (status == KErrNone) {
                effectHandles.insert(effect, effectHandle);
                handleEffects.insert(effectHandle, effect);
            }
        }
        break;
    default:
        break;
    }
}

QFeedbackEffect::State QFeedbackSymbian::effectState(const QFeedbackFileEffect * effect)
{
    TInt handle = effectHandles.value(effect);
    if (handle) {
        TInt state = CHWRMHaptics::EHWRMHapticsEffectNotPlaying;
        if (fileHaptics()->GetEffectState(handle, state) == KErrNone) {
            switch (state) {
                case CHWRMHaptics::EHWRMHapticsEffectNotPlaying:
                default:
                    return QFeedbackEffect::Stopped;

                case CHWRMHaptics::EHWRMHapticsEffectPlaying:
                    return QFeedbackEffect::Running;

                case CHWRMHaptics::EHWRMHapticsEffectPaused:
                    return QFeedbackEffect::Paused;
            }
        }
    }
    return QFeedbackEffect::Stopped;
}

int QFeedbackSymbian::effectDuration(const QFeedbackFileEffect* effect)
{
    TInt handle = effectHandles.value(effect);
    if (handle) {
        TInt duration = 0;
        if (fileHaptics()->GetEffectDuration(handle, 0, duration) == KErrNone)
            return duration;
    }
    return 0; // unknown
}

QStringList QFeedbackSymbian::supportedMimeTypes()
{
    return QStringList() << QLatin1String("vibra/ivt");
}