File indexing completed on 2024-05-12 15:56:41

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
0003  *
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoInputDevice.h"
0008 
0009 class Q_DECL_HIDDEN KoInputDevice::Private
0010 {
0011 public:
0012     Private(QTabletEvent::TabletDevice d, QTabletEvent::PointerType p, qint64 id, bool m)
0013             : device(d),
0014             pointer(p),
0015             uniqueTabletId(id),
0016             mouse(m) {
0017     }
0018     QTabletEvent::TabletDevice device;
0019     QTabletEvent::PointerType pointer;
0020     qint64 uniqueTabletId;
0021     bool mouse;
0022 };
0023 
0024 KoInputDevice::KoInputDevice(QTabletEvent::TabletDevice device, QTabletEvent::PointerType pointer, qint64 uniqueTabletId)
0025         : d(new Private(device, pointer, uniqueTabletId, false))
0026 {
0027 }
0028 
0029 KoInputDevice::KoInputDevice()
0030         : d(new Private(QTabletEvent::NoDevice, QTabletEvent::UnknownPointer, -1, true))
0031 {
0032 }
0033 
0034 KoInputDevice::KoInputDevice(const KoInputDevice &other)
0035         : d(new Private(other.d->device, other.d->pointer, other.d->uniqueTabletId, other.d->mouse))
0036 {
0037 }
0038 
0039 
0040 KoInputDevice::~KoInputDevice()
0041 {
0042     delete d;
0043 }
0044 
0045 QTabletEvent::TabletDevice KoInputDevice::device() const
0046 {
0047     return d->device;
0048 }
0049 
0050 QTabletEvent::PointerType KoInputDevice::pointer() const
0051 {
0052     return d->pointer;
0053 }
0054 
0055 qint64 KoInputDevice::uniqueTabletId() const
0056 {
0057     return d->uniqueTabletId;
0058 }
0059 
0060 bool KoInputDevice::isMouse() const
0061 {
0062     // sometimes, the system gives us tablet events with NoDevice or UnknownPointer. This is
0063     // likely an XInput2 bug. However, assuming that if cannot identify the tablet device we've
0064     // actually got a mouse is reasonable. See https://bugs.kde.org/show_bug.cgi?id=283130.
0065     return d->mouse || d->device == QTabletEvent::NoDevice || d->pointer == QTabletEvent::UnknownPointer;
0066 }
0067 
0068 
0069 bool KoInputDevice::operator==(const KoInputDevice &other) const
0070 {
0071     return d->device == other.d->device && d->pointer == other.d->pointer &&
0072            d->uniqueTabletId == other.d->uniqueTabletId && d->mouse == other.d->mouse;
0073 }
0074 
0075 bool KoInputDevice::operator!=(const KoInputDevice &other) const
0076 {
0077     return !(operator==(other));
0078 }
0079 
0080 KoInputDevice & KoInputDevice::operator=(const KoInputDevice & other)
0081 {
0082     d->device = other.d->device;
0083     d->pointer = other.d->pointer;
0084     d->uniqueTabletId = other.d->uniqueTabletId;
0085     d->mouse = other.d->mouse;
0086     return *this;
0087 }
0088 
0089 // static
0090 KoInputDevice KoInputDevice::invalid()
0091 {
0092     KoInputDevice id(QTabletEvent::NoDevice, QTabletEvent::UnknownPointer);
0093     return id;
0094 }
0095 
0096 
0097 
0098 KoInputDevice KoInputDevice::mouse()
0099 {
0100     KoInputDevice id;
0101     return id;
0102 }
0103 
0104 // static
0105 KoInputDevice KoInputDevice::stylus()
0106 {
0107     KoInputDevice id(QTabletEvent::Stylus, QTabletEvent::Pen);
0108     return id;
0109 }
0110 
0111 // static
0112 KoInputDevice KoInputDevice::eraser()
0113 {
0114     KoInputDevice id(QTabletEvent::Stylus, QTabletEvent::Eraser);
0115     return id;
0116 }
0117 
0118 QDebug operator<<(QDebug dbg, const KoInputDevice &device)
0119 {
0120     if (device.isMouse())
0121         dbg.nospace() << "mouse";
0122     else {
0123         switch (device.pointer()) {
0124         case QTabletEvent::UnknownPointer:
0125             dbg.nospace() << "unknown pointer";
0126             break;
0127         case QTabletEvent::Pen:
0128             dbg.nospace() << "pen";
0129             break;
0130         case QTabletEvent::Cursor:
0131             dbg.nospace() << "cursor";
0132             break;
0133         case QTabletEvent::Eraser:
0134             dbg.nospace() << "eraser";
0135             break;
0136         }
0137         switch(device.device()) {
0138         case QTabletEvent::NoDevice:
0139             dbg.space() << "no device";
0140             break;
0141         case QTabletEvent::Puck:
0142             dbg.space() << "puck";
0143             break;
0144         case QTabletEvent::Stylus:
0145             dbg.space() << "stylus";
0146             break;
0147         case QTabletEvent::Airbrush:
0148             dbg.space() << "airbrush";
0149             break;
0150         case QTabletEvent::FourDMouse:
0151             dbg.space() << "four2mouse";
0152             break;
0153         case QTabletEvent::RotationStylus:
0154             dbg.space() << "rotationstylus";
0155             break;
0156         case QTabletEvent::XFreeEraser:
0157             dbg.space() << "XFreeEraser";
0158             break;
0159         }
0160         dbg.space() << "(id: " << device.uniqueTabletId() << ")";
0161     }
0162     return dbg.space();
0163 }