| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qevdevmousehandler_p.h" |
| 5 | |
| 6 | #include <QSocketNotifier> |
| 7 | #include <QStringList> |
| 8 | #include <QPoint> |
| 9 | #include <QGuiApplication> |
| 10 | #include <QScreen> |
| 11 | #include <QLoggingCategory> |
| 12 | #include <qpa/qwindowsysteminterface.h> |
| 13 | |
| 14 | #include <qplatformdefs.h> |
| 15 | #include <private/qcore_unix_p.h> // overrides QT_OPEN |
| 16 | #include <private/qhighdpiscaling_p.h> |
| 17 | |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #ifdef Q_OS_FREEBSD |
| 21 | #include <dev/evdev/input.h> |
| 22 | #else |
| 23 | #include <linux/kd.h> |
| 24 | #include <linux/input.h> |
| 25 | #endif |
| 26 | |
| 27 | #define TEST_BIT(array, bit) (array[bit/8] & (1<<(bit%8))) |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | using namespace Qt::StringLiterals; |
| 32 | |
| 33 | Q_LOGGING_CATEGORY(qLcEvdevMouse, "qt.qpa.input" ) |
| 34 | |
| 35 | std::unique_ptr<QEvdevMouseHandler> QEvdevMouseHandler::create(const QString &device, const QString &specification) |
| 36 | { |
| 37 | qCDebug(qLcEvdevMouse) << "create mouse handler for" << device << specification; |
| 38 | |
| 39 | bool compression = true; |
| 40 | int jitterLimit = 0; |
| 41 | int grab = 0; |
| 42 | bool abs = false; |
| 43 | |
| 44 | const auto args = QStringView{specification}.split(sep: u':'); |
| 45 | for (const auto &arg : args) { |
| 46 | if (arg == "nocompress"_L1 ) |
| 47 | compression = false; |
| 48 | else if (arg.startsWith(s: "dejitter="_L1 )) |
| 49 | jitterLimit = arg.mid(pos: 9).toInt(); |
| 50 | else if (arg.startsWith(s: "grab="_L1 )) |
| 51 | grab = arg.mid(pos: 5).toInt(); |
| 52 | else if (arg == "abs"_L1 ) |
| 53 | abs = true; |
| 54 | } |
| 55 | |
| 56 | int fd; |
| 57 | fd = qt_safe_open(pathname: device.toLocal8Bit().constData(), O_RDONLY | O_NDELAY, mode: 0); |
| 58 | if (fd >= 0) { |
| 59 | ::ioctl(fd: fd, EVIOCGRAB, grab); |
| 60 | return std::unique_ptr<QEvdevMouseHandler>(new QEvdevMouseHandler(device, fd, abs, compression, jitterLimit)); |
| 61 | } else { |
| 62 | qErrnoWarning(errno, msg: "Cannot open mouse input device %s" , qPrintable(device)); |
| 63 | return nullptr; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | QEvdevMouseHandler::QEvdevMouseHandler(const QString &device, int fd, bool abs, bool compression, int jitterLimit) |
| 68 | : m_device(device), m_fd(fd), m_abs(abs), m_compression(compression) |
| 69 | { |
| 70 | setObjectName("Evdev Mouse Handler"_L1 ); |
| 71 | |
| 72 | m_jitterLimitSquared = jitterLimit * jitterLimit; |
| 73 | |
| 74 | // Some touch screens present as mice with absolute coordinates. |
| 75 | // These can not be differentiated from touchpads, so supplying abs to QT_QPA_EVDEV_MOUSE_PARAMETERS |
| 76 | // will force qevdevmousehandler to treat the coordinates as absolute, scaled to the hardware maximums. |
| 77 | // Turning this on will not affect mice as these do not report in absolute coordinates |
| 78 | // but will make touchpads act like touch screens |
| 79 | if (m_abs) |
| 80 | m_abs = getHardwareMaximum(); |
| 81 | |
| 82 | detectHiResWheelSupport(); |
| 83 | |
| 84 | // socket notifier for events on the mouse device |
| 85 | m_notify = new QSocketNotifier(m_fd, QSocketNotifier::Read, this); |
| 86 | connect(sender: m_notify, signal: &QSocketNotifier::activated, |
| 87 | context: this, slot: &QEvdevMouseHandler::readMouseData); |
| 88 | } |
| 89 | |
| 90 | QEvdevMouseHandler::~QEvdevMouseHandler() |
| 91 | { |
| 92 | if (m_fd >= 0) |
| 93 | qt_safe_close(fd: m_fd); |
| 94 | } |
| 95 | |
| 96 | void QEvdevMouseHandler::detectHiResWheelSupport() |
| 97 | { |
| 98 | #if defined(REL_WHEEL_HI_RES) || defined(REL_HWHEEL_HI_RES) |
| 99 | // Check if we can expect hires events as we will get both |
| 100 | // legacy and hires event and needs to know if we should |
| 101 | // ignore the legacy events. |
| 102 | unsigned char relFeatures[(REL_MAX / 8) + 1]{}; |
| 103 | if (ioctl(fd: m_fd, EVIOCGBIT(EV_REL, sizeof (relFeatures)), relFeatures) == -1) |
| 104 | return; |
| 105 | |
| 106 | #if defined(REL_WHEEL_HI_RES) |
| 107 | m_hiResWheel = TEST_BIT(relFeatures, REL_WHEEL_HI_RES); |
| 108 | #endif |
| 109 | #if defined(REL_HWHEEL_HI_RES) |
| 110 | m_hiResHWheel = TEST_BIT(relFeatures, REL_HWHEEL_HI_RES); |
| 111 | #endif |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | // Ask touch screen hardware for information on coordinate maximums |
| 116 | // If any ioctls fail, revert to non abs mode |
| 117 | bool QEvdevMouseHandler::getHardwareMaximum() |
| 118 | { |
| 119 | unsigned char absFeatures[(ABS_MAX / 8) + 1]; |
| 120 | memset(s: absFeatures, c: '\0', n: sizeof (absFeatures)); |
| 121 | |
| 122 | // test if ABS_X, ABS_Y are available |
| 123 | if (ioctl(fd: m_fd, EVIOCGBIT(EV_ABS, sizeof (absFeatures)), absFeatures) == -1) |
| 124 | return false; |
| 125 | |
| 126 | if ((!TEST_BIT(absFeatures, ABS_X)) || (!TEST_BIT(absFeatures, ABS_Y))) |
| 127 | return false; |
| 128 | |
| 129 | // ask hardware for minimum and maximum values |
| 130 | struct input_absinfo absInfo; |
| 131 | if (ioctl(fd: m_fd, EVIOCGABS(ABS_X), &absInfo) == -1) |
| 132 | return false; |
| 133 | |
| 134 | m_hardwareWidth = absInfo.maximum - absInfo.minimum; |
| 135 | |
| 136 | if (ioctl(fd: m_fd, EVIOCGABS(ABS_Y), &absInfo) == -1) |
| 137 | return false; |
| 138 | |
| 139 | m_hardwareHeight = absInfo.maximum - absInfo.minimum; |
| 140 | |
| 141 | QScreen *primaryScreen = QGuiApplication::primaryScreen(); |
| 142 | QRect g = QHighDpi::toNativePixels(value: primaryScreen->virtualGeometry(), context: primaryScreen); |
| 143 | m_hardwareScalerX = static_cast<qreal>(m_hardwareWidth) / (g.right() - g.left()); |
| 144 | m_hardwareScalerY = static_cast<qreal>(m_hardwareHeight) / (g.bottom() - g.top()); |
| 145 | |
| 146 | qCDebug(qLcEvdevMouse) << "Absolute pointing device" |
| 147 | << "hardware max x" << m_hardwareWidth |
| 148 | << "hardware max y" << m_hardwareHeight |
| 149 | << "hardware scalers x" << m_hardwareScalerX << 'y' << m_hardwareScalerY; |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void QEvdevMouseHandler::sendMouseEvent() |
| 155 | { |
| 156 | int x; |
| 157 | int y; |
| 158 | |
| 159 | if (!m_abs) { |
| 160 | x = m_x - m_prevx; |
| 161 | y = m_y - m_prevy; |
| 162 | if (m_prevInvalid) { |
| 163 | x = y = 0; |
| 164 | m_prevInvalid = false; |
| 165 | } |
| 166 | } else { |
| 167 | x = m_x / m_hardwareScalerX; |
| 168 | y = m_y / m_hardwareScalerY; |
| 169 | } |
| 170 | |
| 171 | if (m_eventType == QEvent::MouseMove) |
| 172 | emit handleMouseEvent(x, y, abs: m_abs, buttons: m_buttons, button: Qt::NoButton, type: m_eventType); |
| 173 | else |
| 174 | emit handleMouseEvent(x, y, abs: m_abs, buttons: m_buttons, button: m_button, type: m_eventType); |
| 175 | |
| 176 | m_prevx = m_x; |
| 177 | m_prevy = m_y; |
| 178 | } |
| 179 | |
| 180 | void QEvdevMouseHandler::readMouseData() |
| 181 | { |
| 182 | struct ::input_event buffer[32]; |
| 183 | int n = 0; |
| 184 | bool posChanged = false, btnChanged = false; |
| 185 | bool pendingMouseEvent = false; |
| 186 | forever { |
| 187 | int result = QT_READ(fd: m_fd, data: reinterpret_cast<char *>(buffer) + n, maxlen: sizeof(buffer) - n); |
| 188 | |
| 189 | if (result == 0) { |
| 190 | qWarning(msg: "evdevmouse: Got EOF from the input device" ); |
| 191 | return; |
| 192 | } else if (result < 0) { |
| 193 | if (errno != EINTR && errno != EAGAIN) { |
| 194 | qErrnoWarning(errno, msg: "evdevmouse: Could not read from input device" ); |
| 195 | // If the device got disconnected, stop reading, otherwise we get flooded |
| 196 | // by the above error over and over again. |
| 197 | if (errno == ENODEV) { |
| 198 | delete m_notify; |
| 199 | m_notify = nullptr; |
| 200 | qt_safe_close(fd: m_fd); |
| 201 | m_fd = -1; |
| 202 | } |
| 203 | return; |
| 204 | } |
| 205 | } else { |
| 206 | n += result; |
| 207 | if (n % sizeof(buffer[0]) == 0) |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | n /= sizeof(buffer[0]); |
| 213 | |
| 214 | for (int i = 0; i < n; ++i) { |
| 215 | struct ::input_event *data = &buffer[i]; |
| 216 | if (data->type == EV_ABS) { |
| 217 | // Touchpads: store the absolute position for now, will calculate a relative one later. |
| 218 | if (data->code == ABS_X && m_x != data->value) { |
| 219 | m_x = data->value; |
| 220 | posChanged = true; |
| 221 | } else if (data->code == ABS_Y && m_y != data->value) { |
| 222 | m_y = data->value; |
| 223 | posChanged = true; |
| 224 | } |
| 225 | } else if (data->type == EV_REL) { |
| 226 | QPoint delta; |
| 227 | if (data->code == REL_X) { |
| 228 | m_x += data->value; |
| 229 | posChanged = true; |
| 230 | } else if (data->code == REL_Y) { |
| 231 | m_y += data->value; |
| 232 | posChanged = true; |
| 233 | } else if (!m_hiResWheel && data->code == REL_WHEEL) { |
| 234 | // data->value: positive == up, negative == down |
| 235 | delta.setY(120 * data->value); |
| 236 | emit handleWheelEvent(delta); |
| 237 | #ifdef REL_WHEEL_HI_RES |
| 238 | } else if (data->code == REL_WHEEL_HI_RES) { |
| 239 | delta.setY(data->value); |
| 240 | emit handleWheelEvent(delta); |
| 241 | #endif |
| 242 | } else if (!m_hiResHWheel && data->code == REL_HWHEEL) { |
| 243 | // data->value: positive == right, negative == left |
| 244 | delta.setX(-120 * data->value); |
| 245 | emit handleWheelEvent(delta); |
| 246 | #ifdef REL_HWHEEL_HI_RES |
| 247 | } else if (data->code == REL_HWHEEL_HI_RES) { |
| 248 | delta.setX(-data->value); |
| 249 | emit handleWheelEvent(delta); |
| 250 | #endif |
| 251 | } |
| 252 | } else if (data->type == EV_KEY && data->code == BTN_TOUCH) { |
| 253 | // We care about touchpads only, not touchscreens -> don't map to button press. |
| 254 | // Need to invalidate prevx/y however to get proper relative pos. |
| 255 | m_prevInvalid = true; |
| 256 | } else if (data->type == EV_KEY && data->code >= BTN_LEFT && data->code <= BTN_JOYSTICK) { |
| 257 | Qt::MouseButton button = Qt::NoButton; |
| 258 | // BTN_LEFT == 0x110 in kernel's input.h |
| 259 | // The range of possible mouse buttons ends just before BTN_JOYSTICK, value 0x120. |
| 260 | switch (data->code) { |
| 261 | case 0x110: button = Qt::LeftButton; break; // BTN_LEFT |
| 262 | case 0x111: button = Qt::RightButton; break; |
| 263 | case 0x112: button = Qt::MiddleButton; break; |
| 264 | case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton |
| 265 | case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton |
| 266 | case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton |
| 267 | case 0x116: button = Qt::ExtraButton4; break; |
| 268 | case 0x117: button = Qt::ExtraButton5; break; |
| 269 | case 0x118: button = Qt::ExtraButton6; break; |
| 270 | case 0x119: button = Qt::ExtraButton7; break; |
| 271 | case 0x11a: button = Qt::ExtraButton8; break; |
| 272 | case 0x11b: button = Qt::ExtraButton9; break; |
| 273 | case 0x11c: button = Qt::ExtraButton10; break; |
| 274 | case 0x11d: button = Qt::ExtraButton11; break; |
| 275 | case 0x11e: button = Qt::ExtraButton12; break; |
| 276 | case 0x11f: button = Qt::ExtraButton13; break; |
| 277 | } |
| 278 | m_buttons.setFlag(flag: button, on: data->value); |
| 279 | m_button = button; |
| 280 | m_eventType = data->value == 0 ? QEvent::MouseButtonRelease : QEvent::MouseButtonPress; |
| 281 | btnChanged = true; |
| 282 | } else if (data->type == EV_SYN && data->code == SYN_REPORT) { |
| 283 | if (btnChanged) { |
| 284 | btnChanged = posChanged = false; |
| 285 | sendMouseEvent(); |
| 286 | pendingMouseEvent = false; |
| 287 | } else if (posChanged) { |
| 288 | m_eventType = QEvent::MouseMove; |
| 289 | posChanged = false; |
| 290 | if (m_compression) { |
| 291 | pendingMouseEvent = true; |
| 292 | } else { |
| 293 | sendMouseEvent(); |
| 294 | } |
| 295 | } |
| 296 | } else if (data->type == EV_MSC && data->code == MSC_SCAN) { |
| 297 | // kernel encountered an unmapped key - just ignore it |
| 298 | continue; |
| 299 | } |
| 300 | } |
| 301 | if (m_compression && pendingMouseEvent) { |
| 302 | int distanceSquared = (m_x - m_prevx)*(m_x - m_prevx) + (m_y - m_prevy)*(m_y - m_prevy); |
| 303 | if (distanceSquared > m_jitterLimitSquared) |
| 304 | sendMouseEvent(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | QT_END_NAMESPACE |
| 309 | |
| 310 | #include "moc_qevdevmousehandler_p.cpp" |
| 311 | |