| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 Canonical, Ltd. and/or its subsidiary(-ies). |
| 4 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qinputinfo.h" |
| 42 | |
| 43 | #include "qinputinfomanager_p.h" |
| 44 | |
| 45 | Q_GLOBAL_STATIC(QInputInfoManagerPrivate, inputDeviceManagerPrivate) |
| 46 | |
| 47 | #if defined(Q_OS_LINUX) |
| 48 | #if !defined(QT_NO_UDEV) |
| 49 | #include "linux/qinputinfomanagerudev_p.h" |
| 50 | #endif |
| 51 | |
| 52 | #if !defined(QT_NO_MIR) |
| 53 | #include <QGuiApplication> |
| 54 | #include <qpa/qplatformnativeinterface.h> |
| 55 | #include "linux/qinputinfomanagermir_p.h" |
| 56 | #endif |
| 57 | |
| 58 | #if !defined(QT_NO_UDEV) |
| 59 | Q_GLOBAL_STATIC(QInputInfoManagerUdev, inputDeviceManagerUdev) |
| 60 | #endif |
| 61 | #if !defined(QT_NO_MIR) |
| 62 | static void not_owning_connection(MirConnection*) {} |
| 63 | QInputInfoManagerMir::MirConnectionPtr attempt_to_connect_to_mir() |
| 64 | { |
| 65 | auto nativeInterface = QGuiApplication::platformNativeInterface(); |
| 66 | |
| 67 | if (nativeInterface) { |
| 68 | auto connection = static_cast<MirConnection*>(nativeInterface->nativeResourceForIntegration("mirconnection")); |
| 69 | return QInputInfoManagerMir::MirConnectionPtr{connection, ¬_owning_connection}; |
| 70 | } else { |
| 71 | auto connection = mir_connect_sync(NULL, "qtsystems-plugin"); |
| 72 | if (mir_connection_is_valid(connection)) |
| 73 | return QInputInfoManagerMir::MirConnectionPtr{connection, &mir_connection_release}; |
| 74 | else |
| 75 | return QInputInfoManagerMir::MirConnectionPtr{connection, ¬_owning_connection}; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Q_GLOBAL_STATIC_WITH_ARGS(QInputInfoManagerMir, inputDeviceManagerMir, (attempt_to_connect_to_mir())) |
| 80 | #endif |
| 81 | #endif |
| 82 | |
| 83 | QT_BEGIN_NAMESPACE |
| 84 | |
| 85 | /*! |
| 86 | \internal |
| 87 | */ |
| 88 | QInputInfoManagerPrivate * QInputInfoManagerPrivate::instance() |
| 89 | { |
| 90 | #ifndef QT_NO_MIR |
| 91 | if (inputDeviceManagerMir.exists()) { |
| 92 | return inputDeviceManagerMir(); |
| 93 | } else { |
| 94 | auto connection_attempt = attempt_to_connect_to_mir(); |
| 95 | if (connection_attempt && mir_connection_is_valid(connection_attempt.get())) { |
| 96 | connection_attempt.reset(); |
| 97 | return inputDeviceManagerMir(); |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | #ifndef QT_NO_UDEV |
| 102 | return inputDeviceManagerUdev(); |
| 103 | #endif |
| 104 | return inputDeviceManagerPrivate(); |
| 105 | } |
| 106 | |
| 107 | /*! |
| 108 | \internal |
| 109 | */ |
| 110 | QInputDevicePrivate::QInputDevicePrivate(QObject *parent) : |
| 111 | QObject(parent), |
| 112 | type(QInputDevice::UnknownType) |
| 113 | { |
| 114 | qRegisterMetaType<QInputDevice::InputType>(); |
| 115 | qRegisterMetaType<QInputDevice::InputTypeFlags>(); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /*! |
| 120 | \class QInputDevice |
| 121 | \inmodule QtSystemInfo |
| 122 | \brief The QInputDevice class provides various information about one input device. |
| 123 | \ingroup systeminfo |
| 124 | |
| 125 | */ |
| 126 | /*! |
| 127 | \enum InputType::InputType |
| 128 | This enum describes the type of input. |
| 129 | |
| 130 | \value UnknownType The input type is unknown. |
| 131 | \value Button Input is a button. |
| 132 | \value Mouse Input is a mouse. |
| 133 | \value TouchPad Input is a touch pad. |
| 134 | \value TouchScreen Input is a touch screen. |
| 135 | \value Keyboard Input is a keyboard |
| 136 | \value Switch Input is a switch |
| 137 | */ |
| 138 | |
| 139 | /*! |
| 140 | Constructs a \l QInputDevice object with the given \a parent. |
| 141 | */ |
| 142 | QInputDevice::QInputDevice(QObject *parent) : |
| 143 | QObject(parent), |
| 144 | d_ptr(new QInputDevicePrivate(this)) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | /*! |
| 149 | \property QInputDevice::properties |
| 150 | \brief The properties of this input device. |
| 151 | |
| 152 | This property contains a variant map of all properties known for this input device. |
| 153 | Name and identifier properties are required. |
| 154 | |
| 155 | \sa name(), identifier(), buttons(), switches(), relativeAxes(), absoluteAxes(), type() |
| 156 | |
| 157 | */ |
| 158 | QVariantMap QInputDevice::properties() |
| 159 | { |
| 160 | return deviceProperties; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | Returns the name of this input device. |
| 165 | */ |
| 166 | QString QInputDevice::name() const |
| 167 | { |
| 168 | return d_ptr->name; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | Sets the name of this input device to \b name. |
| 173 | */ |
| 174 | void QInputDevice::setName(const QString &name) |
| 175 | { |
| 176 | deviceProperties.insert(QStringLiteral("name"),name); |
| 177 | d_ptr->name = name; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | Returns the identifier of this device. |
| 182 | */ |
| 183 | QString QInputDevice::identifier() const |
| 184 | { |
| 185 | return d_ptr->identifier; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | Sets the identifier of this device to /b id. |
| 190 | */ |
| 191 | void QInputDevice::setIdentifier(const QString &id) |
| 192 | { |
| 193 | d_ptr->identifier = id; |
| 194 | deviceProperties.insert(QStringLiteral("identifier"),id); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | Returns the number of buttons this device has. |
| 199 | */ |
| 200 | QList <int> QInputDevice::buttons() const |
| 201 | { |
| 202 | return d_ptr->buttons; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | \internal |
| 207 | */ |
| 208 | void QInputDevice::addButton(int buttonCode) |
| 209 | { |
| 210 | d_ptr->buttons.append(buttonCode); |
| 211 | deviceProperties.insert(QStringLiteral("buttons"),QVariant::fromValue(d_ptr->buttons)); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | Returns the number of switches of this device. |
| 216 | */ |
| 217 | QList <int> QInputDevice::switches() const |
| 218 | { |
| 219 | return d_ptr->switches; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | \internal |
| 224 | */ |
| 225 | void QInputDevice::addSwitch(int switchCode) |
| 226 | { |
| 227 | d_ptr->switches.append(switchCode); |
| 228 | deviceProperties.insert(QStringLiteral("switches"),QVariant::fromValue(d_ptr->switches)); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | Returns a list of the relative axes of this device |
| 233 | */ |
| 234 | QList <int> QInputDevice::relativeAxes() const |
| 235 | { |
| 236 | return d_ptr->relativeAxes; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | \internal |
| 241 | */ |
| 242 | void QInputDevice::addRelativeAxis(int axisCode) |
| 243 | { |
| 244 | d_ptr->relativeAxes.append(axisCode); |
| 245 | deviceProperties.insert(QStringLiteral("rAxis"),QVariant::fromValue(d_ptr->relativeAxes)); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | Returns a list of the absolute axes of this device |
| 250 | */ |
| 251 | QList <int> QInputDevice::absoluteAxes() const |
| 252 | { |
| 253 | return d_ptr->absoluteAxes; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | \internal |
| 258 | */ |
| 259 | void QInputDevice::addAbsoluteAxis(int axisCode) |
| 260 | { |
| 261 | d_ptr->absoluteAxes.append(axisCode); |
| 262 | deviceProperties.insert(QStringLiteral("aAxis"),QVariant::fromValue(d_ptr->absoluteAxes)); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | Returns a QInputDevice::InputTypeFlags of all the types of types. |
| 267 | */ |
| 268 | QInputDevice::InputTypeFlags QInputDevice::types() const |
| 269 | { |
| 270 | return d_ptr->type; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | \internal |
| 275 | */ |
| 276 | void QInputDevice::setTypes(QInputDevice::InputTypeFlags type) |
| 277 | { |
| 278 | d_ptr->type = type; |
| 279 | deviceProperties.insert(QStringLiteral("types"),QVariant::fromValue(type)); |
| 280 | } |
| 281 | |
| 282 | /*! |
| 283 | \class QInputInfoManager |
| 284 | \inmodule QtSystemInfo |
| 285 | |
| 286 | \brief The QInputInfoManager class manages input devices. |
| 287 | |
| 288 | */ |
| 289 | QInputInfoManager::QInputInfoManager(QObject *parent) : |
| 290 | QObject(parent), |
| 291 | d_ptr(QInputInfoManagerPrivate::instance()), |
| 292 | currentFilter(QInputDevice::Button | QInputDevice::Mouse |
| 293 | | QInputDevice::TouchPad | QInputDevice::TouchScreen |
| 294 | | QInputDevice::Keyboard |
| 295 | | QInputDevice::Switch), |
| 296 | filteredCount(0) |
| 297 | { |
| 298 | |
| 299 | connect(d_ptr, &QInputInfoManagerPrivate::deviceAdded,this,&QInputInfoManager::addedDevice); |
| 300 | connect(d_ptr, &QInputInfoManagerPrivate::deviceRemoved,this,&QInputInfoManager::removedDevice); |
| 301 | |
| 302 | connect(d_ptr,SIGNAL(ready()),this,SLOT(privateReady())); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | /*! |
| 307 | Destroy the QSensorGestureManager |
| 308 | */ |
| 309 | QInputInfoManager::~QInputInfoManager() |
| 310 | { |
| 311 | } |
| 312 | /*! |
| 313 | \fn QInputInfoManager::ready() |
| 314 | |
| 315 | This signal is emitted when input device map and associated properties are ready to use. |
| 316 | */ |
| 317 | /*! |
| 318 | \fn QInputInfoManager::deviceAdded(QInputDevice *inputDevice) |
| 319 | |
| 320 | This signal is emitted when a new input device is added. |
| 321 | */ |
| 322 | /*! |
| 323 | \fn QInputInfoManager::deviceRemoved(const QString &deviceId) |
| 324 | |
| 325 | This signal is emitted when a new input device is removed. |
| 326 | */ |
| 327 | |
| 328 | /* |
| 329 | \internal |
| 330 | */ |
| 331 | void QInputInfoManager::privateReady() |
| 332 | { |
| 333 | setFilter(currentFilter); |
| 334 | Q_EMIT ready(); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | Returns a QMap of input devices using the currently set QInputDevice::InputTypeFlags filter. |
| 339 | */ |
| 340 | QMap <QString, QInputDevice *> QInputInfoManager::deviceMap() |
| 341 | { |
| 342 | return currentFilteredMap; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | \internal |
| 347 | */ |
| 348 | void QInputInfoManager::addedDevice(QInputDevice *deviceInfo) |
| 349 | { |
| 350 | Q_UNUSED(deviceInfo); |
| 351 | int oldFilteredCount = filteredCount; |
| 352 | |
| 353 | setFilter(currentFilter); |
| 354 | |
| 355 | if (oldFilteredCount < filteredCount) { |
| 356 | Q_EMIT deviceAdded(deviceInfo); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | \internal |
| 362 | */ |
| 363 | void QInputInfoManager::removedDevice(const QString &deviceId) |
| 364 | { |
| 365 | bool ok = false; |
| 366 | if (currentFilteredMap.contains(deviceId)) |
| 367 | ok = true; |
| 368 | setFilter(currentFilter); |
| 369 | if (ok) { |
| 370 | Q_EMIT deviceRemoved(deviceId); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | Returns the number of input devices with the currently set QInputDevice::InputTypeFlags filter, |
| 376 | if no filter is set, this returns number of all available input devices. |
| 377 | */ |
| 378 | int QInputInfoManager::count() const |
| 379 | { |
| 380 | int deviceCount = 0; |
| 381 | if (currentFilter.testFlag(QInputDevice::Button)) { |
| 382 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::Button)); |
| 383 | } |
| 384 | if (currentFilter.testFlag(QInputDevice::Mouse)) { |
| 385 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::Mouse)); |
| 386 | } |
| 387 | if (currentFilter.testFlag(QInputDevice::TouchPad)) { |
| 388 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::TouchPad)); |
| 389 | } |
| 390 | if (currentFilter.testFlag(QInputDevice::TouchScreen)) { |
| 391 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::TouchScreen)); |
| 392 | } |
| 393 | if (currentFilter.testFlag(QInputDevice::Keyboard)) { |
| 394 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::Keyboard)); |
| 395 | } |
| 396 | if (currentFilter.testFlag(QInputDevice::Switch)) { |
| 397 | deviceCount += count(static_cast< QInputDevice::InputType >(QInputDevice::Switch)); |
| 398 | } |
| 399 | return deviceCount; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | \property QInputInfoManager::count |
| 404 | \brief The number of input devices of the type filter. |
| 405 | |
| 406 | Returns the number of input devices of the type \a filter, regardless of the filter |
| 407 | that is currently set. |
| 408 | */ |
| 409 | /*! |
| 410 | \fn QInputInfoManager::countChanged(int count) |
| 411 | |
| 412 | This signal is emitted when the count of devices in regards to the current filter changes. |
| 413 | */ |
| 414 | |
| 415 | int QInputInfoManager::count(const QInputDevice::InputType filter) const |
| 416 | { |
| 417 | int dList = 0; |
| 418 | QMapIterator<QString, QInputDevice *> i(d_ptr->deviceMap); |
| 419 | while (i.hasNext()) { |
| 420 | i.next(); |
| 421 | if (i.value()->types().testFlag(filter)) { |
| 422 | dList++; |
| 423 | } |
| 424 | } |
| 425 | return dList; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | \property QInputInfoManager::filter |
| 430 | \brief The currently set filter. |
| 431 | |
| 432 | Returns the currently set device filter. |
| 433 | If none is set, default filter includes all QInputDevice::InputType |
| 434 | */ |
| 435 | |
| 436 | /*! |
| 437 | \fn QInputInfoManager::filterChanged(QInputDevice::InputTypeFlags filterFlags) |
| 438 | |
| 439 | This signal is emitted when the filter property changes. |
| 440 | */ |
| 441 | |
| 442 | QInputDevice::InputTypeFlags QInputInfoManager::filter() |
| 443 | { |
| 444 | return currentFilter; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Sets the current input device filter to \a filter, and updates device map. |
| 449 | * |
| 450 | * \sa deviceMap() |
| 451 | */ |
| 452 | void QInputInfoManager::setFilter(QInputDevice::InputTypeFlags filter) |
| 453 | { |
| 454 | currentFilteredMap.clear(); |
| 455 | if (!filter) { |
| 456 | filter = filter & (QInputDevice::Button | QInputDevice::Mouse |
| 457 | | QInputDevice::TouchPad | QInputDevice::TouchScreen |
| 458 | | QInputDevice::Keyboard | QInputDevice::Switch); |
| 459 | } |
| 460 | QMapIterator<QString, QInputDevice *> i(d_ptr->deviceMap); |
| 461 | while (i.hasNext()) { |
| 462 | i.next(); |
| 463 | |
| 464 | if (filter.testFlag(QInputDevice::Button) |
| 465 | && i.value()->types().testFlag(QInputDevice::Button)) { |
| 466 | currentFilteredMap.insert(i.key(),i.value()); |
| 467 | } |
| 468 | if (filter.testFlag(QInputDevice::Mouse) |
| 469 | && i.value()->types().testFlag(QInputDevice::Mouse)) { |
| 470 | currentFilteredMap.insert(i.key(),i.value()); |
| 471 | } |
| 472 | if (filter.testFlag(QInputDevice::TouchPad) |
| 473 | && i.value()->types().testFlag(QInputDevice::TouchPad)) { |
| 474 | currentFilteredMap.insert(i.key(),i.value()); |
| 475 | } |
| 476 | if (filter.testFlag(QInputDevice::TouchScreen) |
| 477 | && i.value()->types().testFlag(QInputDevice::TouchScreen)) { |
| 478 | currentFilteredMap.insert(i.key(),i.value()); |
| 479 | } |
| 480 | if (filter.testFlag(QInputDevice::Keyboard) |
| 481 | && i.value()->types().testFlag(QInputDevice::Keyboard)) { |
| 482 | currentFilteredMap.insert(i.key(),i.value()); |
| 483 | } |
| 484 | if (filter.testFlag(QInputDevice::Switch) |
| 485 | && i.value()->types().testFlag(QInputDevice::Switch)) { |
| 486 | currentFilteredMap.insert(i.key(),i.value()); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (filter != currentFilter) { |
| 491 | currentFilter = filter; |
| 492 | Q_EMIT filterChanged(filter); |
| 493 | } |
| 494 | |
| 495 | if (currentFilteredMap.count() != filteredCount) { |
| 496 | filteredCount = currentFilteredMap.count(); |
| 497 | Q_EMIT countChanged(filteredCount); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | \property QInputInfoManager::lastAdded |
| 503 | \brief The last added input device. |
| 504 | |
| 505 | Returns the last added input device. |
| 506 | */ |
| 507 | QInputDevice *QInputInfoManager::lastAdded() |
| 508 | { |
| 509 | return currentFilteredMap.last(); |
| 510 | } |
| 511 | |
| 512 | QT_END_NAMESPACE |