1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qlibinputpointer_p.h"
41 #include <libinput.h>
42 #include <QtCore/QEvent>
43 #include <QtGui/QGuiApplication>
44 #include <QtGui/QScreen>
45 #include <QtGui/private/qguiapplication_p.h>
46 #include <QtGui/private/qinputdevicemanager_p.h>
47 #include <qpa/qwindowsysteminterface.h>
48 #include <private/qhighdpiscaling_p.h>
49 
50 QT_BEGIN_NAMESPACE
51 
QLibInputPointer()52 QLibInputPointer::QLibInputPointer()
53     : m_buttons(Qt::NoButton)
54 {
55 }
56 
processButton(libinput_event_pointer * e)57 void QLibInputPointer::processButton(libinput_event_pointer *e)
58 {
59     const uint32_t b = libinput_event_pointer_get_button(e);
60     const bool pressed = libinput_event_pointer_get_button_state(e) == LIBINPUT_BUTTON_STATE_PRESSED;
61 
62     Qt::MouseButton button = Qt::NoButton;
63     switch (b) {
64     case 0x110: button = Qt::LeftButton; break;    // BTN_LEFT
65     case 0x111: button = Qt::RightButton; break;
66     case 0x112: button = Qt::MiddleButton; break;
67     case 0x113: button = Qt::ExtraButton1; break;  // AKA Qt::BackButton
68     case 0x114: button = Qt::ExtraButton2; break;  // AKA Qt::ForwardButton
69     case 0x115: button = Qt::ExtraButton3; break;  // AKA Qt::TaskButton
70     case 0x116: button = Qt::ExtraButton4; break;
71     case 0x117: button = Qt::ExtraButton5; break;
72     case 0x118: button = Qt::ExtraButton6; break;
73     case 0x119: button = Qt::ExtraButton7; break;
74     case 0x11a: button = Qt::ExtraButton8; break;
75     case 0x11b: button = Qt::ExtraButton9; break;
76     case 0x11c: button = Qt::ExtraButton10; break;
77     case 0x11d: button = Qt::ExtraButton11; break;
78     case 0x11e: button = Qt::ExtraButton12; break;
79     case 0x11f: button = Qt::ExtraButton13; break;
80     }
81 
82     m_buttons.setFlag(button, pressed);
83 
84     QEvent::Type type = pressed ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease;
85     Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
86 
87     QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons, button, type, mods);
88 }
89 
processMotion(libinput_event_pointer * e)90 void QLibInputPointer::processMotion(libinput_event_pointer *e)
91 {
92     const double dx = libinput_event_pointer_get_dx(e);
93     const double dy = libinput_event_pointer_get_dy(e);
94     QScreen * const primaryScreen = QGuiApplication::primaryScreen();
95     const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
96 
97     m_pos.setX(qBound(g.left(), qRound(m_pos.x() + dx), g.right()));
98     m_pos.setY(qBound(g.top(), qRound(m_pos.y() + dy), g.bottom()));
99 
100     Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
101 
102     QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons,
103                                              Qt::NoButton, QEvent::MouseMove, mods);
104 }
105 
processAbsMotion(libinput_event_pointer * e)106 void QLibInputPointer::processAbsMotion(libinput_event_pointer *e)
107 {
108     QScreen * const primaryScreen = QGuiApplication::primaryScreen();
109     const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
110 
111     const double x = libinput_event_pointer_get_absolute_x_transformed(e, g.width());
112     const double y = libinput_event_pointer_get_absolute_y_transformed(e, g.height());
113 
114     m_pos.setX(qBound(g.left(), qRound(g.left() + x), g.right()));
115     m_pos.setY(qBound(g.top(), qRound(g.top() + y), g.bottom()));
116 
117     Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
118 
119     QWindowSystemInterface::handleMouseEvent(nullptr, m_pos, m_pos, m_buttons,
120                                              Qt::NoButton, QEvent::MouseMove, mods);
121 
122 }
123 
processAxis(libinput_event_pointer * e)124 void QLibInputPointer::processAxis(libinput_event_pointer *e)
125 {
126     double value; // default axis value is 15 degrees per wheel click
127     QPoint angleDelta;
128 #if !QT_CONFIG(libinput_axis_api)
129     value = libinput_event_pointer_get_axis_value(e);
130     if (libinput_event_pointer_get_axis(e) == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)
131         angleDelta.setY(qRound(value));
132     else
133         angleDelta.setX(qRound(value));
134 #else
135     if (libinput_event_pointer_has_axis(e, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
136         value = libinput_event_pointer_get_axis_value(e, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
137         angleDelta.setY(qRound(value));
138     }
139     if (libinput_event_pointer_has_axis(e, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
140         value = libinput_event_pointer_get_axis_value(e, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
141         angleDelta.setX(qRound(value));
142     }
143 #endif
144     const int factor = 8;
145     angleDelta *= -factor;
146     Qt::KeyboardModifiers mods = QGuiApplicationPrivate::inputDeviceManager()->keyboardModifiers();
147     QWindowSystemInterface::handleWheelEvent(nullptr, m_pos, m_pos, QPoint(), angleDelta, mods);
148 }
149 
setPos(const QPoint & pos)150 void QLibInputPointer::setPos(const QPoint &pos)
151 {
152     QScreen * const primaryScreen = QGuiApplication::primaryScreen();
153     const QRect g = QHighDpi::toNativePixels(primaryScreen->virtualGeometry(), primaryScreen);
154 
155     m_pos.setX(qBound(g.left(), pos.x(), g.right()));
156     m_pos.setY(qBound(g.top(), pos.y(), g.bottom()));
157 }
158 
159 QT_END_NAMESPACE
160