Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbsdmouse.cpp
Go to the documentation of this file.
1// Copyright (C) 2015-2016 Oleksandr Tymoshenko <gonzo@bluezbox.com>
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 "qbsdmouse.h"
5
6#include <QFile>
7#include <QSocketNotifier>
8#include <QStringList>
9#include <QPoint>
10#include <QGuiApplication>
11#include <qpa/qwindowsysteminterface.h>
12#include <private/qguiapplication_p.h>
13#include <private/qinputdevicemanager_p_p.h>
14
15#include <private/qcore_unix_p.h>
16#include <qdebug.h>
17
18#include <errno.h>
19#include <fcntl.h>
20#include <sys/mouse.h>
21#include <unistd.h>
22
23QT_BEGIN_NAMESPACE
24
25enum {
26 PsmLevelBasic = 0,
27 PsmLevelExtended = 1,
28 PsmLevelNative = 2
29};
30
31QBsdMouseHandler::QBsdMouseHandler(const QString &key, const QString &specification)
32{
33 Q_UNUSED(key);
34
35 setObjectName(QLatin1String("BSD Sysmouse Handler"));
36
37 QByteArray device;
38 if (specification.startsWith("/dev/"))
39 device = QFile::encodeName(specification);
40
41 if (device.isEmpty())
42 device = QByteArrayLiteral("/dev/sysmouse");
43
44 m_devFd = QT_OPEN(device.constData(), O_RDONLY);
45 if (m_devFd < 0) {
46 qErrnoWarning(errno, "open(%s) failed", device.constData());
47 return;
48 }
49
50 int level = 0;
51 if (ioctl(m_devFd, MOUSE_GETLEVEL, &level)) {
52 qErrnoWarning(errno, "ioctl(%s, MOUSE_GETLEVEL) failed", device.constData());
53 close(m_devFd);
54 m_devFd = -1;
55 return;
56 }
57
58 switch (level) {
59 case PsmLevelBasic:
60 m_packetSize = 5;
61 break;
62 case PsmLevelExtended:
63 m_packetSize = 8;
64 break;
65 default:
66 qWarning("Unsupported mouse device operation level: %d", level);
67 close(m_devFd);
68 m_devFd = -1;
69 return;
70 }
71
72 if (fcntl(m_devFd, F_SETFL, O_NONBLOCK)) {
73 qErrnoWarning(errno, "fcntl(%s, F_SETFL, O_NONBLOCK) failed", device.constData());
74 close(m_devFd);
75 m_devFd = -1;
76 return;
77 }
78
79 m_notifier.reset(new QSocketNotifier(m_devFd, QSocketNotifier::Read, this));
80 connect(m_notifier.data(), &QSocketNotifier::activated, this, &QBsdMouseHandler::readMouseData);
81 QInputDeviceManagerPrivate::get(QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount(
82 QInputDeviceManager::DeviceTypePointer, 1);
83}
84
86{
87 if (m_devFd != -1)
88 close(m_devFd);
89}
90
91void QBsdMouseHandler::readMouseData()
92{
93 if (m_devFd < 0)
94 return;
95
96 if (m_packetSize == 0)
97 return;
98
99 // packet format described in mouse(4)
100 qint8 packet[MOUSE_SYS_PACKETSIZE];
101 quint8 status = 0;
102 while (read(m_devFd, packet, m_packetSize) == m_packetSize) {
103 const qint16 relx = packet[1] + packet[3];
104 const qint16 rely = -(packet[2] + packet[4]);
105
106 m_x += relx;
107 m_y += rely;
108
109 status = packet[0] & MOUSE_SYS_STDBUTTONS;
110 }
111
112 // clamp to screen geometry
113 const QRect g = QGuiApplication::primaryScreen()->virtualGeometry();
114 if (m_x + m_xOffset < g.left())
115 m_x = g.left() - m_xOffset;
116 else if (m_x + m_xOffset > g.right())
117 m_x = g.right() - m_xOffset;
118
119 if (m_y + m_yOffset < g.top())
120 m_y = g.top() - m_yOffset;
121 else if (m_y + m_yOffset > g.bottom())
122 m_y = g.bottom() - m_yOffset;
123
124 const QPoint pos(m_x + m_xOffset, m_y + m_yOffset);
125 m_buttons = Qt::NoButton;
126 if (!(status & MOUSE_SYS_BUTTON1UP))
127 m_buttons |= Qt::LeftButton;
128 if (!(status & MOUSE_SYS_BUTTON2UP))
129 m_buttons |= Qt::MiddleButton;
130 if (!(status & MOUSE_SYS_BUTTON3UP))
131 m_buttons |= Qt::RightButton;
132
133 QWindowSystemInterface::handleMouseEvent(0, pos, pos, m_buttons);
134}
135
136QT_END_NAMESPACE
~QBsdMouseHandler() override
Definition qbsdmouse.cpp:85