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
|
/****************************************************************************
**
** 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 <qfeedbackactuator.h>
#include "qfeedback.h"
#include <QtCore/QtPlugin>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QVariant>
#include <QtCore/QTimer>
#include <QDebug>
#define MAX_FILE_SIZE (1 << 14) //16KB
QFeedbackImmersion::QFeedbackImmersion() : QObject(qApp)
{
if (VIBE_FAILED(ImmVibeInitialize(VIBE_CURRENT_VERSION_NUMBER))) {
//that should be done once
//error management
qWarning() << "the Immersion library could not be initialized";
} else {
const int nbDev = ImmVibeGetDeviceCount();
for (int i = 0; i < nbDev; ++i) {
actuatorList << createFeedbackActuator(this, i);
}
}
}
QFeedbackImmersion::~QFeedbackImmersion()
{
//cleanup the devices when terminating
for (int i = 0 ; i < actuatorHandles.size(); ++i)
ImmVibeCloseDevice(actuatorHandles.at(i));
ImmVibeTerminate();
}
QFeedbackInterface::PluginPriority QFeedbackImmersion::pluginPriority()
{
return PluginNormalPriority;
}
QList<QFeedbackActuator*> QFeedbackImmersion::actuators()
{
return actuatorList;
}
void QFeedbackImmersion::setActuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop, const QVariant &value)
{
switch (prop)
{
case Enabled:
ImmVibeSetDevicePropertyBool(handleForActuator(actuator), VIBE_DEVPROPTYPE_DISABLE_EFFECTS, !value.toBool());
break;
default:
break;
}
}
QVariant QFeedbackImmersion::actuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop)
{
switch (prop)
{
case Name:
{
char szDeviceName[VIBE_MAX_DEVICE_NAME_LENGTH] = { 0 };
if (VIBE_FAILED(ImmVibeGetDeviceCapabilityString(actuator.id(), VIBE_DEVCAPTYPE_DEVICE_NAME,
VIBE_MAX_CAPABILITY_STRING_LENGTH, szDeviceName)))
return QString();
return QString::fromLocal8Bit(szDeviceName);
}
case State:
{
QFeedbackActuator::State ret = QFeedbackActuator::Unknown;
VibeInt32 s = 0;
if (actuator.isValid() && VIBE_SUCCEEDED(ImmVibeGetDeviceState(actuator.id(), &s))) {
if (s == VIBE_DEVICESTATE_ATTACHED)
ret = QFeedbackActuator::Ready;
else if (s == VIBE_DEVICESTATE_BUSY)
ret = QFeedbackActuator:: Busy;
}
return ret;
}
case Enabled:
{
VibeBool disabled = true;
if (VIBE_FAILED(ImmVibeGetDevicePropertyBool(handleForActuator(actuator), VIBE_DEVPROPTYPE_DISABLE_EFFECTS, &disabled)))
return false;
return !disabled;
}
default:
return QVariant();
}
}
bool QFeedbackImmersion::isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability cap)
{
switch(cap)
{
case QFeedbackActuator::Envelope:
case QFeedbackActuator::Period:
return true;
default:
return false;
}
}
VibeInt32 QFeedbackImmersion::convertedDuration(int duration)
{
//this allows to manage the infinite durations
if (duration == -1)
return VIBE_TIME_INFINITE;
return duration;
}
VibeInt32 QFeedbackImmersion::handleForActuator(const QFeedbackActuator &actuator)
{
return handleForActuator(actuator.id());
}
VibeInt32 QFeedbackImmersion::handleForActuator(int actId)
{
if (actId < 0)
return VIBE_INVALID_DEVICE_HANDLE_VALUE;
//we avoid locking too much (it will only lock if the device is not yet open
if (actuatorHandles.size() <= actId) {
QMutexLocker locker(&mutex);
while (actuatorHandles.size() <= actId)
actuatorHandles.append(VIBE_INVALID_DEVICE_HANDLE_VALUE);
}
if (VIBE_IS_INVALID_DEVICE_HANDLE(actuatorHandles.at(actId))) {
QMutexLocker locker(&mutex);
if (VIBE_IS_INVALID_DEVICE_HANDLE(actuatorHandles.at(actId))) {
ImmVibeOpenDevice(actId, &actuatorHandles[actId] );
//temporary solution: provide a proto dev license key
ImmVibeSetDevicePropertyString(actuatorHandles.at(actId), VIBE_DEVPROPTYPE_LICENSE_KEY, "IMWPROTOSJZF4EH6KWVUK8HAP5WACT6Q");
}
}
return actuatorHandles.at(actId);
}
void QFeedbackImmersion::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty)
{
VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
return; // the effect is simply not running
VibeStatus status = VIBE_S_SUCCESS;
if (effect->period() > 0) {
status = ImmVibeModifyPlayingPeriodicEffect(handleForActuator(effect->actuator()), effectHandle,
convertedDuration(effect->duration()),
effect->intensity() * qreal(VIBE_MAX_MAGNITUDE), effect->period(),
VIBE_DEFAULT_STYLE,
effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
} else {
status = ImmVibeModifyPlayingMagSweepEffect(handleForActuator(effect->actuator()), effectHandle,
convertedDuration(effect->duration()),
effect->intensity() * qreal(VIBE_MAX_MAGNITUDE),
VIBE_DEFAULT_STYLE,
effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
}
// Hmm. Not sure if the duration needs to be updated or not
if (VIBE_SUCCEEDED(status)) {
startTimerForHandle(effectHandle, effect); // this will kill old one, if necessary
}
if (VIBE_FAILED(status))
reportError(effect, QFeedbackEffect::UnknownError);
}
void QFeedbackImmersion::killTimerForHandle(VibeInt32 handle)
{
QTimer* t = effectTimers.take(handle);
if (t) {
t->stop();
delete t;
}
}
void QFeedbackImmersion::startTimerForHandle(VibeInt32 handle, const QFeedbackHapticsEffect *effect)
{
// Make sure we don't have one already
killTimerForHandle(handle);
// And create a timer for a non infinite, non periodic effect
if (effect->period() <= 0 && effect->duration() > 0) {
QTimer* t = new QTimer();
t->setSingleShot(true);
t->setInterval(effect->duration() + effect->attackTime() + effect->fadeTime());
connect(t, SIGNAL(timeout()), const_cast<QFeedbackHapticsEffect*>(effect), SIGNAL(stateChanged()));
effectTimers.insert(handle, t);
t->start();
}
}
void QFeedbackImmersion::startTimerForHandle(VibeInt32 handle, QFeedbackFileEffect *effect)
{
// Make sure we don't have one already
killTimerForHandle(handle);
// And create a timer for a non infinite effect
if (effect->duration() > 0) {
QTimer* t = new QTimer();
t->setSingleShot(true);
t->setInterval(effect->duration());
connect(t, SIGNAL(timeout()), effect, SIGNAL(stateChanged()));
effectTimers.insert(handle, t);
t->start();
}
}
void QFeedbackImmersion::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State state)
{
VibeStatus status = VIBE_S_SUCCESS;
VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
switch (state)
{
case QFeedbackEffect::Stopped:
if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
status = ImmVibeStopPlayingEffect(handleForActuator(effect->actuator()), effectHandle);
effectHandles.remove(effect);
killTimerForHandle(effectHandle);
}
break;
case QFeedbackEffect::Paused:
Q_ASSERT(VIBE_IS_VALID_EFFECT_HANDLE(effectHandle));
status = ImmVibePausePlayingEffect(handleForActuator(effect->actuator()), effectHandle);
killTimerForHandle(effectHandle);
break;
case QFeedbackEffect::Running:
//if the effect handle exists, the feedback must be paused
if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
status = ImmVibeResumePausedEffect(handleForActuator(effect->actuator()), effectHandle);
// Restart a timer if needed (only aperiodic ones)
if (VIBE_SUCCEEDED(status)) {
startTimerForHandle(effectHandle, effect);
}
} else {
//we need to start the effect and create the handle
VibeInt32 effectHandle = VIBE_INVALID_EFFECT_HANDLE_VALUE;
if (effect->period() > 0) {
status = ImmVibePlayPeriodicEffect(handleForActuator(effect->actuator()), convertedDuration(effect->duration()),
qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE)), effect->period(),
VIBE_DEFAULT_STYLE, effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE), &effectHandle);
} else {
status = ImmVibePlayMagSweepEffect(handleForActuator(effect->actuator()), convertedDuration(effect->duration()),
qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE)),
VIBE_DEFAULT_STYLE, effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE), &effectHandle);
}
if (VIBE_SUCCEEDED(status)) {
effectHandles.insert(effect, effectHandle);
startTimerForHandle(effectHandle, effect);
}
}
break;
default:
break;
}
}
QFeedbackEffect::State QFeedbackImmersion::effectState(const QFeedbackHapticsEffect *effect)
{
VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
return QFeedbackEffect::Stopped; // the effect is simply not running
VibeInt32 effectState = VIBE_EFFECT_STATE_NOT_PLAYING;
ImmVibeGetEffectState(handleForActuator(effect->actuator()), effectHandle, &effectState);
return updateImmState(effect, effectHandle, effectState);
}
// **************************
// !!! File based stuff below
// **************************
void QFeedbackImmersion::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;
if (fc.refCount == 0) {
//we need to load the file
QFile file(fileName);
success = false;
if (file.size() < MAX_FILE_SIZE && file.open(QIODevice::ReadOnly)) {
fc.ba = file.readAll();
//now let's try to check the file content with immersion
if (VIBE_FAILED(ImmVibeGetIVTEffectCount(fc.constData()))) {
fileData.remove(fileName);
} else {
fc.refCount++;
success = true;
}
}
}
reportLoadFinished(effect, success);
} else {
//unload
if (fc.refCount > 0)
fc.refCount--;
if (fc.refCount == 0)
fileData.remove(fileName);
}
}
void QFeedbackImmersion::setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state)
{
VibeStatus status = VIBE_S_SUCCESS;
VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
VibeInt32 dev = handleForActuator(0); //we always use the default (first) device
switch (state)
{
case QFeedbackEffect::Stopped:
if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
status = ImmVibeStopPlayingEffect(dev, effectHandle);
effectHandles.remove(effect);
killTimerForHandle(effectHandle);
}
break;
case QFeedbackEffect::Paused:
Q_ASSERT(VIBE_IS_VALID_EFFECT_HANDLE(effectHandle));
status = ImmVibePausePlayingEffect(dev, effectHandle);
killTimerForHandle(effectHandle);
break;
case QFeedbackEffect::Running:
//if the effect handle exists, the feedback must be paused
if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
status = ImmVibeResumePausedEffect(dev, effectHandle);
if (VIBE_SUCCEEDED(status)) {
startTimerForHandle(effectHandle, effect);
}
} else {
//we need to start the effect and create the handle
QString fileName = effect->source().toLocalFile();
Q_ASSERT(fileData.contains(fileName));
status = ImmVibePlayIVTEffect(dev, fileData[fileName].constData(), 0, &effectHandle);
if (VIBE_SUCCEEDED(status)) {
effectHandles.insert(effect, effectHandle);
startTimerForHandle(effectHandle, effect);
}
}
break;
default:
break;
}
if (VIBE_FAILED(status))
reportError(effect, QFeedbackEffect::UnknownError);
}
QFeedbackEffect::State QFeedbackImmersion::updateImmState(const QFeedbackEffect *effect, VibeInt32 effectHandle, VibeInt32 effectState)
{
// here we detect changes in the state of the effect
// and make sure any timers are updated
switch(effectState)
{
case VIBE_EFFECT_STATE_PAUSED:
killTimerForHandle(effectHandle);
return QFeedbackEffect::Paused;
case VIBE_EFFECT_STATE_PLAYING: {
// If the timer existed and is not running, we probably need
// to reschedule it so the effect state can be up to date
QTimer *t = effectTimers.value(effectHandle);
if (t && !t->isActive()) {
// Well, perhaps the timer fired early. Trigger it again later
t->setInterval(50); // XXX arbitrary choice
t->start();
}
return QFeedbackEffect::Running;
}
case VIBE_EFFECT_STATE_NOT_PLAYING:
default:
killTimerForHandle(effectHandle);
if (effectHandles.contains(effect)) {
effectHandles.remove(effect);
// because we kill the timer before it fires stateChanged, we need to emit stateChanged ourself.
QMetaObject::invokeMethod(const_cast<QFeedbackEffect*>(effect), "stateChanged");
}
return QFeedbackEffect::Stopped;
}
}
QFeedbackEffect::State QFeedbackImmersion::effectState(const QFeedbackFileEffect *effect)
{
VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
return QFeedbackEffect::Stopped; // the effect is simply not running
VibeInt32 effectState = VIBE_EFFECT_STATE_NOT_PLAYING;
ImmVibeGetEffectState(handleForActuator(0), effectHandle, &effectState);
return updateImmState(effect, effectHandle, effectState);
}
int QFeedbackImmersion::effectDuration(const QFeedbackFileEffect *effect)
{
VibeInt32 ret = 0;
QString fileName = effect->source().toLocalFile();
if (fileData.contains(fileName))
ImmVibeGetIVTEffectDuration(fileData[fileName].constData(), 0, &ret);
return ret;
}
QStringList QFeedbackImmersion::supportedMimeTypes()
{
return QStringList() << QLatin1String("vibra/ivt");
}
|