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