Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
24
25enum {
29};
30
32{
34
35 setObjectName(QLatin1String("BSD Sysmouse Handler"));
36
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;
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);
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
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
134}
135
IOBluetoothDevice * device
~QBsdMouseHandler() override
Definition qbsdmouse.cpp:85
QBsdMouseHandler(const QString &key, const QString &specification)
Definition qbsdmouse.cpp:31
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
Definition qfile.h:158
static QInputDeviceManager * inputDeviceManager()
QScreen * primaryScreen
the primary (or default) screen of the application.
static QInputDeviceManagerPrivate * get(QInputDeviceManager *mgr)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
Q_WEAK_OVERLOAD void setObjectName(const QString &name)
Sets the object's name to name.
Definition qobject.h:127
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
T * data() const noexcept
Returns the value of the pointer referenced by this object.
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
QRect virtualGeometry
the pixel geometry of the virtual desktop to which this screen belongs
Definition qscreen.h:47
\inmodule QtCore
void activated(QSocketDescriptor socket, QSocketNotifier::Type activationEvent, QPrivateSignal)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static bool handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global, Qt::MouseButtons state, Qt::MouseButton button, QEvent::Type type, Qt::KeyboardModifiers mods=Qt::NoModifier, Qt::MouseEventSource source=Qt::MouseEventNotSynthesized)
void qErrnoWarning(const char *msg,...)
Combined button and popup list for selecting options.
@ LeftButton
Definition qnamespace.h:58
@ RightButton
Definition qnamespace.h:59
@ MiddleButton
Definition qnamespace.h:60
@ NoButton
Definition qnamespace.h:57
@ PsmLevelNative
Definition qbsdmouse.cpp:28
@ PsmLevelExtended
Definition qbsdmouse.cpp:27
@ PsmLevelBasic
Definition qbsdmouse.cpp:26
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
#define QT_OPEN
#define qWarning
Definition qlogging.h:166
GLenum GLuint GLint level
GLuint64 key
GLboolean GLboolean g
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define Q_UNUSED(x)
short qint16
Definition qtypes.h:47
QT_BEGIN_NAMESPACE typedef signed char qint8
Definition qtypes.h:45
unsigned char quint8
Definition qtypes.h:46
ReturnedValue read(const char *data)