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
qlibinputpointer.cpp
Go to the documentation of this file.
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
5#include <libinput.h>
6#include <QtCore/QEvent>
7#include <QtGui/QGuiApplication>
8#include <QtGui/QScreen>
9#include <QtGui/private/qguiapplication_p.h>
10#include <QtGui/private/qinputdevicemanager_p.h>
11#include <qpa/qwindowsysteminterface.h>
12#include <private/qhighdpiscaling_p.h>
13
15
16Q_STATIC_LOGGING_CATEGORY(qLcLibInputMouse, "qt.qpa.input.mouse")
17
18QLibInputPointer::QLibInputPointer()
19 : m_buttons(Qt::NoButton)
20{
21}
22
23void QLibInputPointer::processButton(libinput_event_pointer *e)
24{
25 const uint32_t b = libinput_event_pointer_get_button(e);
26 const bool pressed = libinput_event_pointer_get_button_state(e) == LIBINPUT_BUTTON_STATE_PRESSED;
27
28 Qt::MouseButton button = Qt::NoButton;
29 switch (b) {
30 case 0x110: button = Qt::LeftButton; break; // BTN_LEFT
31 case 0x111: button = Qt::RightButton; break;
32 case 0x112: button = Qt::MiddleButton; break;
33 case 0x113: button = Qt::ExtraButton1; break; // AKA Qt::BackButton
34 case 0x114: button = Qt::ExtraButton2; break; // AKA Qt::ForwardButton
35 case 0x115: button = Qt::ExtraButton3; break; // AKA Qt::TaskButton
36 case 0x116: button = Qt::ExtraButton4; break;
37 case 0x117: button = Qt::ExtraButton5; break;
38 case 0x118: button = Qt::ExtraButton6; break;
39 case 0x119: button = Qt::ExtraButton7; break;
40 case 0x11a: button = Qt::ExtraButton8; break;
41 case 0x11b: button = Qt::ExtraButton9; break;
42 case 0x11c: button = Qt::ExtraButton10; break;
43 case 0x11d: button = Qt::ExtraButton11; break;
44 case 0x11e: button = Qt::ExtraButton12; break;
45 case 0x11f: button = Qt::ExtraButton13; break;
46 }
47
48 m_buttons.setFlag(button, pressed);
49
50 QEvent::Type type = pressed ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease;
51 Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
52
53 qCDebug(qLcLibInputMouse) << "processButton" << type << button << m_buttons << m_pos << mods;
54 QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons, button, type, mods);
55}
56
57void QLibInputPointer::processMotion(libinput_event_pointer *e)
58{
59 const double dx = libinput_event_pointer_get_dx(e);
60 const double dy = libinput_event_pointer_get_dy(e);
61 QScreen * const primaryScreen = QGuiApplication::primaryScreen();
62 const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
63
64 m_pos.setX(qBound(g.left(), qRound(m_pos.x() + dx), g.right()));
65 m_pos.setY(qBound(g.top(), qRound(m_pos.y() + dy), g.bottom()));
66
67 Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
68
69 qCDebug(qLcLibInputMouse) << "processMotion" << m_pos << mods;
70 QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons,
71 Qt::NoButton, QEvent::MouseMove, mods);
72}
73
74void QLibInputPointer::processAbsMotion(libinput_event_pointer *e)
75{
76 QScreen * const primaryScreen = QGuiApplication::primaryScreen();
77 const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
78
79 const double x = libinput_event_pointer_get_absolute_x_transformed(e, g.width());
80 const double y = libinput_event_pointer_get_absolute_y_transformed(e, g.height());
81
82 m_pos.setX(qBound(g.left(), qRound(g.left() + x), g.right()));
83 m_pos.setY(qBound(g.top(), qRound(g.top() + y), g.bottom()));
84
85 Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
86
87 qCDebug(qLcLibInputMouse) << "processAbsMotion" << m_pos << mods;
88 QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons,
89 Qt::NoButton, QEvent::MouseMove, mods);
90
91}
92
93void QLibInputPointer::processAxis(libinput_event_pointer *e)
94{
95 double value; // default axis value is 15 degrees per wheel click
96 QPoint angleDelta;
97#if !QT_CONFIG(libinput_axis_api)
98 value = libinput_event_pointer_get_axis_value(e);
99 if (libinput_event_pointer_get_axis(e) == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)
100 angleDelta.setY(qRound(value));
101 else
102 angleDelta.setX(qRound(value));
103#else
104 if (libinput_event_pointer_has_axis(e, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
105#if QT_CONFIG(libinput_hires_wheel_support)
106 value = libinput_event_pointer_get_scroll_value_v120(e, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
107#else
108 value = libinput_event_pointer_get_axis_value(e, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
109#endif
110 angleDelta.setY(qRound(value));
111 }
112 if (libinput_event_pointer_has_axis(e, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
113#if QT_CONFIG(libinput_hires_wheel_support)
114 value = libinput_event_pointer_get_scroll_value_v120(e, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
115#else
116 value = libinput_event_pointer_get_axis_value(e, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
117#endif
118 angleDelta.setX(qRound(value));
119 }
120#endif
121#if QT_CONFIG(libinput_hires_wheel_support)
122 const int factor = -1;
123#else
124 const int factor = -8;
125#endif
126 angleDelta *= factor;
127 Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
128 qCDebug(qLcLibInputMouse) << "processAxis" << angleDelta << m_pos << mods;
129 QWindowSystemInterface::handleWheelEvent(nullptr, m_pos, m_pos, QPoint(), angleDelta, mods);
130}
131
132void QLibInputPointer::setPos(const QPoint &pos)
133{
134 QScreen * const primaryScreen = QGuiApplication::primaryScreen();
135 const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
136
137 m_pos.setX(qBound(g.left(), pos.x(), g.right()));
138 m_pos.setY(qBound(g.top(), pos.y(), g.bottom()));
139 qCDebug(qLcLibInputMouse) << "setPos" << pos << "bounded:" << m_pos;
140}
141
142QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")