| 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 "qfeedbackplugininterfaces.h" |
| 42 | |
| 43 | #include <QtCore/QVariant> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | /*! |
| 48 | \class QFeedbackActuator |
| 49 | \brief The QFeedbackActuator class describes actuators for tactile feedback. |
| 50 | \inmodule QtFeedback |
| 51 | |
| 52 | An actuator knows how to play a \l{QFeedbackEffect}{tactile |
| 53 | effect}. The class gives access to a specified actuator. |
| 54 | |
| 55 | An actuator can be used to play \l{QFeedbackHapticsEffect}s using |
| 56 | \l{QFeedbackHapticsEffect::}{setActuator()}. Usually, you will not |
| 57 | have to set an actuator directly on a QFeedbackHapticsEffect. |
| 58 | QFeedbackHapticsEffect and QFeedbackFileEffect uses an appropriate |
| 59 | actuator by default. However, you can query which actuators |
| 60 | are available with actuators(). |
| 61 | |
| 62 | \code |
| 63 | QFeedbackActuator actuator; // default system actuator |
| 64 | QList<QFeedbackActuator> actuators = QFeedbackActuator::actuators(); |
| 65 | foreach (const QFeedbackActuator& temp, actuators) { |
| 66 | if (temp.name() == "ExampleActuatorName") { |
| 67 | actuator = temp; |
| 68 | } |
| 69 | } |
| 70 | \endcode |
| 71 | |
| 72 | The QFeedbackActuator class gives access to information about the |
| 73 | actuator it represents. You can query if the actuator \l{isEnabled()}{is enabled} |
| 74 | and if it \l{isValid()}{is valid }. Whether an actuator is ready to play an |
| 75 | effect can be queried by checking the actuator's state(). The |
| 76 | \l{QFeedbackActuator::}{State} enum describes the states and |
| 77 | actuator can have. You can also get a human readable name for the actuator with the |
| 78 | name() function. |
| 79 | |
| 80 | \sa QFeedbackHapticsEffect, QFeedbackFileEffect, QFeedbackEffect |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | \enum QFeedbackActuator::Capability |
| 85 | |
| 86 | \value Envelope Capability defining the wave type with attack/fade times and levels. |
| 87 | \value Period Capability defining that the device can play periodic effects. |
| 88 | */ |
| 89 | |
| 90 | /*! |
| 91 | \enum QFeedbackActuator::State |
| 92 | |
| 93 | \value Busy The actuator is busy. |
| 94 | \value Ready The actuator is ready to play an effect. |
| 95 | \value Unknown The actuator is in an unknown state. |
| 96 | */ |
| 97 | |
| 98 | |
| 99 | /*! |
| 100 | Constructs a QFeedbackActuator, passing \a parent to the QObject constructor. |
| 101 | |
| 102 | The object will represent the default actuator on the system. |
| 103 | If there are no actuators attached to the system, isValid() will return false. |
| 104 | |
| 105 | \sa isValid() |
| 106 | */ |
| 107 | QFeedbackActuator::QFeedbackActuator(QObject *parent) : QObject(parent), m_id(-1) |
| 108 | { |
| 109 | QList<QFeedbackActuator*> list = actuators(); |
| 110 | if (!list.isEmpty()) { |
| 111 | QFeedbackActuator* defaultActuator = list.first(); |
| 112 | m_id = defaultActuator->id(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | Constructs a QFeedbackActuator with id \a id, passing \a parent to the QObject constructor. |
| 118 | |
| 119 | This is used by plugins to represents their actuators. |
| 120 | |
| 121 | \sa isValid() |
| 122 | */ |
| 123 | QFeedbackActuator::QFeedbackActuator(QObject *parent, int id) : QObject(parent), m_id(id) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | /*! |
| 128 | \property QFeedbackActuator::id |
| 129 | \brief id of the feedback actuator. |
| 130 | */ |
| 131 | |
| 132 | /*! |
| 133 | Returns the id of the actuator. |
| 134 | \since 1.1 |
| 135 | */ |
| 136 | int QFeedbackActuator::id() const |
| 137 | { |
| 138 | return m_id; |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | \property QFeedbackActuator::valid |
| 143 | \brief validity of the feedback actuator |
| 144 | */ |
| 145 | |
| 146 | /*! |
| 147 | Returns true if the actuator is valid. |
| 148 | */ |
| 149 | bool QFeedbackActuator::isValid() const |
| 150 | { |
| 151 | return m_id >= 0; |
| 152 | } |
| 153 | |
| 154 | /*! |
| 155 | \property QFeedbackActuator::name |
| 156 | \brief name of the feedback actuator |
| 157 | */ |
| 158 | |
| 159 | /*! |
| 160 | Returns the name of the actuator. |
| 161 | */ |
| 162 | QString QFeedbackActuator::name() const |
| 163 | { |
| 164 | return QFeedbackHapticsInterface::instance()->actuatorProperty(*this, QFeedbackHapticsInterface::Name).toString(); |
| 165 | } |
| 166 | |
| 167 | /*! |
| 168 | \property QFeedbackActuator::state |
| 169 | \brief state of the feedback actuator |
| 170 | */ |
| 171 | |
| 172 | /*! |
| 173 | Returns the state of the actuator. |
| 174 | */ |
| 175 | QFeedbackActuator::State QFeedbackActuator::state() const |
| 176 | { |
| 177 | return QFeedbackActuator::State(QFeedbackHapticsInterface::instance()->actuatorProperty(*this, QFeedbackHapticsInterface::State).toInt()); |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Returns if the actuator supports the supplied \a capability. |
| 182 | */ |
| 183 | bool QFeedbackActuator::isCapabilitySupported(Capability capability) const |
| 184 | { |
| 185 | return QFeedbackHapticsInterface::instance()->isActuatorCapabilitySupported(*this, capability); |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | \property QFeedbackActuator::enabled |
| 190 | \brief whether the feedback actuator is enabled |
| 191 | */ |
| 192 | |
| 193 | /*! |
| 194 | Returns true if you can use this actuator to start effects. |
| 195 | */ |
| 196 | bool QFeedbackActuator::isEnabled() const |
| 197 | { |
| 198 | return QFeedbackHapticsInterface::instance()->actuatorProperty(*this, QFeedbackHapticsInterface::Enabled).toBool(); |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | Allows you to enable or disable an actuator. If \a enabled is true, the actuator will be enabled, |
| 203 | and otherwise it will be disabled. |
| 204 | |
| 205 | \note Some systems may not allow you to change whether an actuator is enabled. |
| 206 | */ |
| 207 | void QFeedbackActuator::setEnabled(bool enabled) |
| 208 | { |
| 209 | if (isEnabled() != enabled) { |
| 210 | QFeedbackHapticsInterface::instance()->setActuatorProperty(*this, QFeedbackHapticsInterface::Enabled, enabled); |
| 211 | emit enabledChanged(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | \fn void QFeedbackActuator::enabledChanged() |
| 217 | |
| 218 | This signal is emitted when the actuator is requested to enable or disable itself. |
| 219 | |
| 220 | \sa isEnabled() |
| 221 | */ |
| 222 | |
| 223 | /*! |
| 224 | \fn QFeedbackActuator::actuators() |
| 225 | |
| 226 | Returns the list of actuators available on the system. |
| 227 | */ |
| 228 | QList<QFeedbackActuator*> QFeedbackActuator::actuators() |
| 229 | { |
| 230 | return QFeedbackHapticsInterface::instance()->actuators(); |
| 231 | } |
| 232 | |
| 233 | /*! |
| 234 | \fn QFeedbackActuator::operator==(const QFeedbackActuator &other) const |
| 235 | |
| 236 | Returns true if this actuator is equal to \a other. |
| 237 | */ |
| 238 | bool QFeedbackActuator::operator==(const QFeedbackActuator &other) const |
| 239 | { |
| 240 | return m_id == other.m_id; |
| 241 | } |
| 242 | |
| 243 | QT_END_NAMESPACE |