Debian Codegraph @

src/plugins/feedback/immersion/qfeedback.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 <qfeedbackactuator.h>
41 #include "qfeedback.h"
42 #include <QtCore/QtPlugin>
43 #include <QtCore/QDebug>
44 #include <QtCore/QStringList>
45 #include <QtCore/QCoreApplication>
46 #include <QtCore/QFile>
47 #include <QtCore/QVariant>
48 #include <QtCore/QTimer>
49 #include <QDebug>
50
51 #define MAX_FILE_SIZE (1 << 14) //16KB
52
53 QFeedbackImmersion::QFeedbackImmersion() : QObject(qApp)
54 {
55 if (VIBE_FAILED(ImmVibeInitialize(VIBE_CURRENT_VERSION_NUMBER))) {
56 //that should be done once
57 //error management
58 qWarning() << "the Immersion library could not be initialized";
59 } else {
60 const int nbDev = ImmVibeGetDeviceCount();
61 for (int i = 0; i < nbDev; ++i) {
62 actuatorList << createFeedbackActuator(this, i);
63 }
64 }
65 }
66
67 QFeedbackImmersion::~QFeedbackImmersion()
68 {
69 //cleanup the devices when terminating
70 for (int i = 0 ; i < actuatorHandles.size(); ++i)
71 ImmVibeCloseDevice(actuatorHandles.at(i));
72
73 ImmVibeTerminate();
74 }
75
76 QFeedbackInterface::PluginPriority QFeedbackImmersion::pluginPriority()
77 {
78 return PluginNormalPriority;
79 }
80
81 QList<QFeedbackActuator*> QFeedbackImmersion::actuators()
82 {
83 return actuatorList;
84 }
85
86 void QFeedbackImmersion::setActuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop, const QVariant &value)
87 {
88 switch (prop)
89 {
90 case Enabled:
91 ImmVibeSetDevicePropertyBool(handleForActuator(actuator), VIBE_DEVPROPTYPE_DISABLE_EFFECTS, !value.toBool());
92 break;
93 default:
94 break;
95 }
96 }
97
98 QVariant QFeedbackImmersion::actuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop)
99 {
100 switch (prop)
101 {
102 case Name:
103 {
104 char szDeviceName[VIBE_MAX_DEVICE_NAME_LENGTH] = { 0 };
105 if (VIBE_FAILED(ImmVibeGetDeviceCapabilityString(actuator.id(), VIBE_DEVCAPTYPE_DEVICE_NAME,
106 VIBE_MAX_CAPABILITY_STRING_LENGTH, szDeviceName)))
107 return QString();
108
109 return QString::fromLocal8Bit(szDeviceName);
110 }
111
112 case State:
113 {
114 QFeedbackActuator::State ret = QFeedbackActuator::Unknown;
115 VibeInt32 s = 0;
116 if (actuator.isValid() && VIBE_SUCCEEDED(ImmVibeGetDeviceState(actuator.id(), &s))) {
117 if (s == VIBE_DEVICESTATE_ATTACHED)
118 ret = QFeedbackActuator::Ready;
119 else if (s == VIBE_DEVICESTATE_BUSY)
120 ret = QFeedbackActuator:: Busy;
121 }
122
123 return ret;
124 }
125 case Enabled:
126 {
127 VibeBool disabled = true;
128 if (VIBE_FAILED(ImmVibeGetDevicePropertyBool(handleForActuator(actuator), VIBE_DEVPROPTYPE_DISABLE_EFFECTS, &disabled)))
129 return false;
130 return !disabled;
131 }
132 default:
133 return QVariant();
134 }
135 }
136
137 bool QFeedbackImmersion::isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability cap)
138 {
139 switch(cap)
140 {
141 case QFeedbackActuator::Envelope:
142 case QFeedbackActuator::Period:
143 return true;
144 default:
145 return false;
146 }
147 }
148
149
150 VibeInt32 QFeedbackImmersion::convertedDuration(int duration)
151 {
152 //this allows to manage the infinite durations
153 if (duration == -1)
154 return VIBE_TIME_INFINITE;
155 return duration;
156 }
157
158
159 VibeInt32 QFeedbackImmersion::handleForActuator(const QFeedbackActuator &actuator)
160 {
161 return handleForActuator(actuator.id());
162 }
163
164 VibeInt32 QFeedbackImmersion::handleForActuator(int actId)
165 {
166 if (actId < 0)
167 return VIBE_INVALID_DEVICE_HANDLE_VALUE;
168
169 //we avoid locking too much (it will only lock if the device is not yet open
170 if (actuatorHandles.size() <= actId) {
171 QMutexLocker locker(&mutex);
172 while (actuatorHandles.size() <= actId)
173 actuatorHandles.append(VIBE_INVALID_DEVICE_HANDLE_VALUE);
174 }
175
176 if (VIBE_IS_INVALID_DEVICE_HANDLE(actuatorHandles.at(actId))) {
177 QMutexLocker locker(&mutex);
178 if (VIBE_IS_INVALID_DEVICE_HANDLE(actuatorHandles.at(actId))) {
179 ImmVibeOpenDevice(actId, &actuatorHandles[actId] );
180
181 //temporary solution: provide a proto dev license key
182 ImmVibeSetDevicePropertyString(actuatorHandles.at(actId), VIBE_DEVPROPTYPE_LICENSE_KEY, "IMWPROTOSJZF4EH6KWVUK8HAP5WACT6Q");
183 }
184 }
185 return actuatorHandles.at(actId);
186 }
187
188 void QFeedbackImmersion::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty)
189 {
190 VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
191 if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
192 return; // the effect is simply not running
193
194 VibeStatus status = VIBE_S_SUCCESS;
195 if (effect->period() > 0) {
196 status = ImmVibeModifyPlayingPeriodicEffect(handleForActuator(effect->actuator()), effectHandle,
197 convertedDuration(effect->duration()),
198 effect->intensity() * qreal(VIBE_MAX_MAGNITUDE), effect->period(),
199 VIBE_DEFAULT_STYLE,
200 effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
201 effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
202 } else {
203 status = ImmVibeModifyPlayingMagSweepEffect(handleForActuator(effect->actuator()), effectHandle,
204 convertedDuration(effect->duration()),
205 effect->intensity() * qreal(VIBE_MAX_MAGNITUDE),
206 VIBE_DEFAULT_STYLE,
207 effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
208 effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE));
209 }
210
211 // Hmm. Not sure if the duration needs to be updated or not
212 if (VIBE_SUCCEEDED(status)) {
213 startTimerForHandle(effectHandle, effect); // this will kill old one, if necessary
214 }
215
216 if (VIBE_FAILED(status))
217 reportError(effect, QFeedbackEffect::UnknownError);
218
219 }
220
221 void QFeedbackImmersion::killTimerForHandle(VibeInt32 handle)
222 {
223 QTimer* t = effectTimers.take(handle);
224 if (t) {
225 t->stop();
226 delete t;
227 }
228 }
229
230 void QFeedbackImmersion::startTimerForHandle(VibeInt32 handle, const QFeedbackHapticsEffect *effect)
231 {
232 // Make sure we don't have one already
233 killTimerForHandle(handle);
234
235 // And create a timer for a non infinite, non periodic effect
236 if (effect->period() <= 0 && effect->duration() > 0) {
237 QTimer* t = new QTimer();
238 t->setSingleShot(true);
239 t->setInterval(effect->duration() + effect->attackTime() + effect->fadeTime());
240 connect(t, SIGNAL(timeout()), const_cast<QFeedbackHapticsEffect*>(effect), SIGNAL(stateChanged()));
241 effectTimers.insert(handle, t);
242 t->start();
243 }
244 }
245
246 void QFeedbackImmersion::startTimerForHandle(VibeInt32 handle, QFeedbackFileEffect *effect)
247 {
248 // Make sure we don't have one already
249 killTimerForHandle(handle);
250
251 // And create a timer for a non infinite effect
252 if (effect->duration() > 0) {
253 QTimer* t = new QTimer();
254 t->setSingleShot(true);
255 t->setInterval(effect->duration());
256 connect(t, SIGNAL(timeout()), effect, SIGNAL(stateChanged()));
257 effectTimers.insert(handle, t);
258 t->start();
259 }
260 }
261
262 void QFeedbackImmersion::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State state)
263 {
264 VibeStatus status = VIBE_S_SUCCESS;
265 VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
266
267 switch (state)
268 {
269 case QFeedbackEffect::Stopped:
270 if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
271 status = ImmVibeStopPlayingEffect(handleForActuator(effect->actuator()), effectHandle);
272 effectHandles.remove(effect);
273 killTimerForHandle(effectHandle);
274 }
275 break;
276 case QFeedbackEffect::Paused:
277 Q_ASSERT(VIBE_IS_VALID_EFFECT_HANDLE(effectHandle));
278 status = ImmVibePausePlayingEffect(handleForActuator(effect->actuator()), effectHandle);
279 killTimerForHandle(effectHandle);
280 break;
281 case QFeedbackEffect::Running:
282 //if the effect handle exists, the feedback must be paused
283 if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
284 status = ImmVibeResumePausedEffect(handleForActuator(effect->actuator()), effectHandle);
285 // Restart a timer if needed (only aperiodic ones)
286 if (VIBE_SUCCEEDED(status)) {
287 startTimerForHandle(effectHandle, effect);
288 }
289 } else {
290 //we need to start the effect and create the handle
291 VibeInt32 effectHandle = VIBE_INVALID_EFFECT_HANDLE_VALUE;
292 if (effect->period() > 0) {
293 status = ImmVibePlayPeriodicEffect(handleForActuator(effect->actuator()), convertedDuration(effect->duration()),
294 qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE)), effect->period(),
295 VIBE_DEFAULT_STYLE, effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
296 effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE), &effectHandle);
297 } else {
298 status = ImmVibePlayMagSweepEffect(handleForActuator(effect->actuator()), convertedDuration(effect->duration()),
299 qRound(effect->intensity() * qreal(VIBE_MAX_MAGNITUDE)),
300 VIBE_DEFAULT_STYLE, effect->attackTime(), effect->attackIntensity() * qreal(VIBE_MAX_MAGNITUDE),
301 effect->fadeTime(), effect->fadeIntensity() * qreal(VIBE_MAX_MAGNITUDE), &effectHandle);
302 }
303 if (VIBE_SUCCEEDED(status)) {
304 effectHandles.insert(effect, effectHandle);
305 startTimerForHandle(effectHandle, effect);
306 }
307 }
308 break;
309 default:
310 break;
311 }
312 }
313
314 QFeedbackEffect::State QFeedbackImmersion::effectState(const QFeedbackHapticsEffect *effect)
315 {
316 VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
317 if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
318 return QFeedbackEffect::Stopped; // the effect is simply not running
319
320 VibeInt32 effectState = VIBE_EFFECT_STATE_NOT_PLAYING;
321 ImmVibeGetEffectState(handleForActuator(effect->actuator()), effectHandle, &effectState);
322
323 return updateImmState(effect, effectHandle, effectState);
324 }
325
326 // **************************
327 // !!! File based stuff below
328 // **************************
329
330
331 void QFeedbackImmersion::setLoaded(QFeedbackFileEffect *effect, bool load)
332 {
333 const QUrl url = effect->source();
334
335 // This doesn't handle qrc urls..
336 const QString fileName = url.toLocalFile();
337 if (fileName.isEmpty())
338 return;
339
340 if (!load && !fileData.contains(fileName))
341 return;
342
343 FileContent &fc = fileData[fileName];
344 if (load) {
345 bool success = true;
346 if (fc.refCount == 0) {
347 //we need to load the file
348 QFile file(fileName);
349 success = false;
350 if (file.size() < MAX_FILE_SIZE && file.open(QIODevice::ReadOnly)) {
351 fc.ba = file.readAll();
352 //now let's try to check the file content with immersion
353 if (VIBE_FAILED(ImmVibeGetIVTEffectCount(fc.constData()))) {
354 fileData.remove(fileName);
355 } else {
356 fc.refCount++;
357 success = true;
358 }
359 }
360 }
361 reportLoadFinished(effect, success);
362 } else {
363 //unload
364 if (fc.refCount > 0)
365 fc.refCount--;
366 if (fc.refCount == 0)
367 fileData.remove(fileName);
368 }
369 }
370
371 void QFeedbackImmersion::setEffectState(QFeedbackFileEffect *effect, QFeedbackEffect::State state)
372 {
373 VibeStatus status = VIBE_S_SUCCESS;
374 VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
375
376 VibeInt32 dev = handleForActuator(0); //we always use the default (first) device
377
378 switch (state)
379 {
380 case QFeedbackEffect::Stopped:
381 if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
382 status = ImmVibeStopPlayingEffect(dev, effectHandle);
383 effectHandles.remove(effect);
384 killTimerForHandle(effectHandle);
385 }
386 break;
387 case QFeedbackEffect::Paused:
388 Q_ASSERT(VIBE_IS_VALID_EFFECT_HANDLE(effectHandle));
389 status = ImmVibePausePlayingEffect(dev, effectHandle);
390 killTimerForHandle(effectHandle);
391 break;
392 case QFeedbackEffect::Running:
393 //if the effect handle exists, the feedback must be paused
394 if (VIBE_IS_VALID_EFFECT_HANDLE(effectHandle)) {
395 status = ImmVibeResumePausedEffect(dev, effectHandle);
396 if (VIBE_SUCCEEDED(status)) {
397 startTimerForHandle(effectHandle, effect);
398 }
399 } else {
400 //we need to start the effect and create the handle
401 QString fileName = effect->source().toLocalFile();
402 Q_ASSERT(fileData.contains(fileName));
403 status = ImmVibePlayIVTEffect(dev, fileData[fileName].constData(), 0, &effectHandle);
404 if (VIBE_SUCCEEDED(status)) {
405 effectHandles.insert(effect, effectHandle);
406 startTimerForHandle(effectHandle, effect);
407 }
408 }
409 break;
410 default:
411 break;
412 }
413
414 if (VIBE_FAILED(status))
415 reportError(effect, QFeedbackEffect::UnknownError);
416 }
417
418 QFeedbackEffect::State QFeedbackImmersion::updateImmState(const QFeedbackEffect *effect, VibeInt32 effectHandle, VibeInt32 effectState)
419 {
420 // here we detect changes in the state of the effect
421 // and make sure any timers are updated
422 switch(effectState)
423 {
424 case VIBE_EFFECT_STATE_PAUSED:
425 killTimerForHandle(effectHandle);
426 return QFeedbackEffect::Paused;
427 case VIBE_EFFECT_STATE_PLAYING: {
428 // If the timer existed and is not running, we probably need
429 // to reschedule it so the effect state can be up to date
430 QTimer *t = effectTimers.value(effectHandle);
431 if (t && !t->isActive()) {
432 // Well, perhaps the timer fired early. Trigger it again later
433 t->setInterval(50); // XXX arbitrary choice
434 t->start();
435 }
436 return QFeedbackEffect::Running;
437 }
438 case VIBE_EFFECT_STATE_NOT_PLAYING:
439 default:
440 killTimerForHandle(effectHandle);
441 if (effectHandles.contains(effect)) {
442 effectHandles.remove(effect);
443 // because we kill the timer before it fires stateChanged, we need to emit stateChanged ourself.
444 QMetaObject::invokeMethod(const_cast<QFeedbackEffect*>(effect), "stateChanged");
445 }
446 return QFeedbackEffect::Stopped;
447 }
448 }
449
450 QFeedbackEffect::State QFeedbackImmersion::effectState(const QFeedbackFileEffect *effect)
451 {
452 VibeInt32 effectHandle = effectHandles.value(effect, VIBE_INVALID_EFFECT_HANDLE_VALUE);
453 if (VIBE_IS_INVALID_EFFECT_HANDLE(effectHandle))
454 return QFeedbackEffect::Stopped; // the effect is simply not running
455
456 VibeInt32 effectState = VIBE_EFFECT_STATE_NOT_PLAYING;
457 ImmVibeGetEffectState(handleForActuator(0), effectHandle, &effectState);
458
459 return updateImmState(effect, effectHandle, effectState);
460 }
461
462 int QFeedbackImmersion::effectDuration(const QFeedbackFileEffect *effect)
463 {
464 VibeInt32 ret = 0;
465 QString fileName = effect->source().toLocalFile();
466 if (fileData.contains(fileName))
467 ImmVibeGetIVTEffectDuration(fileData[fileName].constData(), 0, &ret);
468
469 return ret;
470 }
471
472 QStringList QFeedbackImmersion::supportedMimeTypes()
473 {
474 return QStringList() << QLatin1String("vibra/ivt");
475 }