on xenial help sign in
Add a virtual Touchpad while external screen is connected

Approved by: Andrea Cimitan

commitdc86d944fc3683a62856af7097a0bc43966c4963 authorMichael Zanetti <michael.zanetti@canonical.com> dateFri 29 Jan 2016, 11:51 (11 years ago) parentd07b43b85148c9
21 files changed+921−20
debian/unity8-private.install+1 plugins/CMakeLists.txt+1 plugins/UInput/CMakeLists.txt+7 plugins/UInput/UInput.qmltypes+44 plugins/UInput/plugin.cpp+26 plugins/UInput/plugin.h+32 plugins/UInput/qmldir+3 plugins/UInput/uinput.cpp+182 plugins/UInput/uinput.h+61 qml/Components/VirtualTouchPad.qml+138 qml/DisabledScreenNotice.qml+68−18 tests/mocks/CMakeLists.txt+1 tests/mocks/UInput/CMakeLists.txt+7 tests/mocks/UInput/mockuinput.cpp+73 tests/mocks/UInput/mockuinput.h+63 tests/mocks/UInput/plugin.cpp+26 tests/mocks/UInput/plugin.h+32 tests/mocks/UInput/qmldir+2 tests/qmltests/CMakeLists.txt+1 tests/qmltests/Components/tst_VirtualTouchPad.qml+143 tests/qmltests/tst_DisabledScreenNotice.qml+10−2
debian/unity8-private.install+1
@@ -12,6 +12,7 @@ usr/lib/*/unity8/qml/ScreenGrabber
1212usr/lib/*/unity8/qml/SessionBroadcast
1313usr/lib/*/unity8/qml/Ubuntu
1414usr/lib/*/unity8/qml/Unity
15+usr/lib/*/unity8/qml/UInput
1516usr/lib/*/unity8/qml/Utils
1617usr/lib/*/unity8/qml/Wizard
1718usr/share/accountsservice/interfaces
plugins/CMakeLists.txt+1
@@ -22,6 +22,7 @@ add_subdirectory(Powerd)
2222add_subdirectory(SessionBroadcast)
2323add_subdirectory(ScreenGrabber)
2424add_subdirectory(Ubuntu)
25+add_subdirectory(UInput)
2526add_subdirectory(Unity)
2627add_subdirectory(Utils)
2728add_subdirectory(Wizard)
plugins/UInput/CMakeLists.txtnew file+7
@@ -0,0 +1,7 @@
1+add_library(UInput-qml MODULE
2+ plugin.cpp
3+ uinput.cpp
4+)
5+
6+qt5_use_modules(UInput-qml Qml)
7+add_unity8_plugin(UInput 0.1 UInput TARGETS UInput-qml)
plugins/UInput/UInput.qmltypesnew file+44
@@ -0,0 +1,44 @@
1+import QtQuick.tooling 1.1
2+
3+// This file describes the plugin-supplied types contained in the library.
4+// It is used for QML tooling purposes only.
5+//
6+// This file was auto-generated by:
7+// 'qmlplugindump -notrelocatable UInput 0.1 plugins'
8+
9+Module {
10+ Component {
11+ name: "UInput"
12+ prototype: "QObject"
13+ exports: ["UInput/UInput 0.1"]
14+ exportMetaObjectRevisions: [0]
15+ Enum {
16+ name: "Button"
17+ values: {
18+ "ButtonLeft": 0,
19+ "ButtonRight": 1,
20+ "ButtonMiddle": 2
21+ }
22+ }
23+ Method { name: "createMouse" }
24+ Method { name: "removeMouse" }
25+ Method {
26+ name: "moveMouse"
27+ Parameter { name: "dx"; type: "int" }
28+ Parameter { name: "dy"; type: "int" }
29+ }
30+ Method {
31+ name: "pressMouse"
32+ Parameter { name: "button"; type: "Button" }
33+ }
34+ Method {
35+ name: "releaseMouse"
36+ Parameter { name: "button"; type: "Button" }
37+ }
38+ Method {
39+ name: "scrollMouse"
40+ Parameter { name: "dh"; type: "int" }
41+ Parameter { name: "dv"; type: "int" }
42+ }
43+ }
44+}
plugins/UInput/plugin.cppnew file+26
@@ -0,0 +1,26 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "plugin.h"
18+#include "uinput.h"
19+
20+#include <QtQml/qqml.h>
21+
22+void UInputPlugin::registerTypes(const char *uri)
23+{
24+ Q_ASSERT(uri == QLatin1String("UInput"));
25+ qmlRegisterType<UInput>(uri, 0, 1, "UInput");
26+}
plugins/UInput/plugin.hnew file+32
@@ -0,0 +1,32 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef UINPUT_PLUGIN_H
18+#define UINPUT_PLUGIN_H
19+
20+#include <QtQml/QQmlEngine>
21+#include <QtQml/QQmlExtensionPlugin>
22+
23+class UInputPlugin : public QQmlExtensionPlugin
24+{
25+ Q_OBJECT
26+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
27+
28+public:
29+ void registerTypes(const char *uri) override;
30+};
31+
32+#endif
plugins/UInput/qmldirnew file+3
@@ -0,0 +1,3 @@
1+module UInput
2+plugin UInput-qml
3+typeinfo UInput.qmltypes
plugins/UInput/uinput.cppnew file+182
@@ -0,0 +1,182 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "uinput.h"
18+
19+#include <QFile>
20+#include <QDebug>
21+#include <QDateTime>
22+
23+#include <unistd.h>
24+#include <time.h>
25+
26+UInput::UInput(QObject *parent) :
27+ QObject(parent)
28+{
29+ m_devName = QByteArrayLiteral("unity8-simulated-mouse");
30+ m_uinput.setFileName(QStringLiteral("/dev/uinput"));
31+
32+ memset(&m_uinput_mouse_dev, 0, sizeof(m_uinput_mouse_dev));
33+ m_uinput_mouse_dev.id.bustype = BUS_USB;
34+ m_uinput_mouse_dev.id.version = 1;
35+ strncpy(m_uinput_mouse_dev.name, m_devName.constData(), m_devName.length());
36+}
37+
38+UInput::~UInput()
39+{
40+ if (m_mouseCreated) {
41+ removeMouse();
42+ }
43+}
44+
45+void UInput::createMouse()
46+{
47+ if (m_mouseCreated) {
48+ qDebug() << "Already have a virtual device. Not creating another one.";
49+ return;
50+ }
51+
52+ if (!m_uinput.isOpen() && !m_uinput.open(QFile::WriteOnly)) {
53+ return;
54+ }
55+
56+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_REL);
57+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_X);
58+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_Y);
59+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_HWHEEL);
60+ ioctl(m_uinput.handle(), UI_SET_RELBIT, REL_WHEEL);
61+
62+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_KEY);
63+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_MOUSE);
64+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_LEFT);
65+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_MIDDLE);
66+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_RIGHT);
67+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_FORWARD);
68+ ioctl(m_uinput.handle(), UI_SET_KEYBIT, BTN_BACK);
69+
70+ ioctl(m_uinput.handle(), UI_SET_EVBIT, EV_SYN);
71+
72+ int len = write(m_uinput.handle(), &m_uinput_mouse_dev, sizeof(m_uinput_mouse_dev));
73+ if (len <= 0) {
74+ qWarning() << "Failed to write to uinput. Cannot create virtual uinput mouse.";
75+ return;
76+ }
77+
78+ int err = ioctl(m_uinput.handle(), UI_DEV_CREATE);
79+ if (err != 0) {
80+ qWarning() << "Cannot create virtual uinput device. Create ioctl failed:" << err;
81+ return;
82+ }
83+ m_mouseCreated = true;
84+ qDebug() << "Virtual uinput mouse device created.";
85+}
86+
87+void UInput::removeMouse()
88+{
89+ if (!m_mouseCreated) {
90+ return;
91+ }
92+
93+ if (!m_uinput.isOpen() && !m_uinput.open(QFile::WriteOnly)) {
94+ qWarning() << "cannot open uinput... ";
95+ return;
96+ }
97+
98+ int err = ioctl(m_uinput.handle(), UI_DEV_DESTROY);
99+ if (err != 0) {
100+ qWarning() << "Failed to destroy virtual uinput device. Destroy ioctl failed:" << err;
101+ } else {
102+ qDebug() << "Virtual uinput mouse device removed.";
103+ }
104+ m_uinput.close();
105+ m_mouseCreated = false;
106+}
107+
108+void UInput::moveMouse(int dx, int dy)
109+{
110+ struct input_event event;
111+ memset(&event, 0, sizeof(event));
112+ clock_gettime(CLOCK_MONOTONIC, (timespec*)&event.time);
113+ event.type = EV_REL;
114+ event.code = REL_X;
115+ event.value = dx;
116+ write(m_uinput.handle(), &event, sizeof(event));
117+
118+ event.code = REL_Y;
119+ event.value = dy;
120+ write(m_uinput.handle(), &event, sizeof(event));
121+
122+ event.type = EV_SYN;
123+ event.code = SYN_REPORT;
124+ event.value = 0;
125+ write(m_uinput.handle(), &event, sizeof(event));
126+}
127+
128+void UInput::pressMouse(Button button)
129+{
130+ injectMouse(button, 1);
131+}
132+
133+void UInput::releaseMouse(Button button)
134+{
135+ injectMouse(button, 0);
136+}
137+
138+void UInput::scrollMouse(int dh, int dv)
139+{
140+ struct input_event event;
141+ memset(&event, 0, sizeof(event));
142+ clock_gettime(CLOCK_MONOTONIC, (timespec*)&event.time);
143+ event.type = EV_REL;
144+ event.code = REL_HWHEEL;
145+ event.value = dh;
146+ write(m_uinput.handle(), &event, sizeof(event));
147+
148+ event.code = REL_WHEEL;
149+ event.value = dv;
150+ write(m_uinput.handle(), &event, sizeof(event));
151+
152+ event.type = EV_SYN;
153+ event.code = SYN_REPORT;
154+ event.value = 0;
155+ write(m_uinput.handle(), &event, sizeof(event));
156+}
157+
158+void UInput::injectMouse(Button button, int down)
159+{
160+ struct input_event event;
161+ memset(&event, 0, sizeof(event));
162+ clock_gettime(CLOCK_MONOTONIC, (timespec*)&event.time);
163+ event.type = EV_KEY;
164+ switch (button) {
165+ case ButtonLeft:
166+ event.code = BTN_LEFT;
167+ break;
168+ case ButtonRight:
169+ event.code = BTN_RIGHT;
170+ break;
171+ case ButtonMiddle:
172+ event.code = BTN_MIDDLE;
173+ break;
174+ }
175+ event.value = down;
176+ write(m_uinput.handle(), &event, sizeof(event));
177+
178+ event.type = EV_SYN;
179+ event.code = SYN_REPORT;
180+ event.value = 0;
181+ write(m_uinput.handle(), &event, sizeof(event));
182+}
plugins/UInput/uinput.hnew file+61
@@ -0,0 +1,61 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+
18+#ifndef UINPUT_H
19+#define UINPUT_H
20+
21+#include <QObject>
22+#include <QFile>
23+
24+#include <linux/uinput.h>
25+
26+
27+class UInput : public QObject
28+{
29+ Q_OBJECT
30+ Q_ENUMS(Button)
31+
32+public:
33+ enum Button {
34+ ButtonLeft,
35+ ButtonRight,
36+ ButtonMiddle
37+ };
38+
39+ explicit UInput(QObject *parent = nullptr);
40+ ~UInput();
41+
42+ Q_INVOKABLE void createMouse();
43+ Q_INVOKABLE void removeMouse();
44+
45+ Q_INVOKABLE void moveMouse(int dx, int dy);
46+ Q_INVOKABLE void pressMouse(Button button);
47+ Q_INVOKABLE void releaseMouse(Button button);
48+ Q_INVOKABLE void scrollMouse(int dh, int dv);
49+
50+private:
51+ void injectMouse(Button button, int down);
52+
53+private:
54+ QFile m_uinput;
55+ uinput_user_dev m_uinput_mouse_dev;
56+ QByteArray m_devName;
57+
58+ bool m_mouseCreated = false;
59+};
60+
61+#endif // UINPUT_H
qml/Components/VirtualTouchPad.qmlnew file+138
@@ -0,0 +1,138 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.4
18+import Ubuntu.Components 1.3
19+import UInput 0.1
20+
21+Item {
22+ property var uinput: UInput {
23+ Component.onCompleted: createMouse();
24+ Component.onDestruction: removeMouse();
25+ }
26+
27+ readonly property bool pressed: point1.pressed || point2.pressed
28+
29+ MultiPointTouchArea {
30+ objectName: "touchPadArea"
31+ anchors.fill: parent
32+
33+ // FIXME: Once we have Qt DPR support, this should be Qt.styleHints.startDragDistance
34+ readonly property int clickThreshold: units.gu(1.5)
35+ property bool isClick: false
36+ property bool isDoubleClick: false
37+ property bool isDrag: false
38+
39+ onPressed: {
40+ // If double-tapping *really* fast, it could happen that we end up having only point2 pressed
41+ // Make sure we check for both combos, only point1 or only point2
42+ if (((point1.pressed && !point2.pressed) || (!point1.pressed && point2.pressed))
43+ && clickTimer.running) {
44+ clickTimer.stop();
45+ uinput.pressMouse(UInput.ButtonLeft)
46+ isDoubleClick = true;
47+ }
48+ isClick = true;
49+ }
50+
51+ onUpdated: {
52+ switch (touchPoints.length) {
53+ case 1:
54+ moveMouse(touchPoints);
55+ return;
56+ case 2:
57+ scroll(touchPoints);
58+ return;
59+ }
60+ }
61+
62+ onReleased: {
63+ if (isDoubleClick || isDrag) {
64+ uinput.releaseMouse(UInput.ButtonLeft)
65+ isDoubleClick = false;
66+ }
67+ if (isClick) {
68+ clickTimer.scheduleClick(point1.pressed ? UInput.ButtonRight : UInput.ButtonLeft)
69+ }
70+ isClick = false;
71+ isDrag = false;
72+ }
73+
74+ Timer {
75+ id: clickTimer
76+ repeat: false
77+ interval: 200
78+ property int button: UInput.ButtonLeft
79+ onTriggered: {
80+ uinput.pressMouse(button);
81+ uinput.releaseMouse(button);
82+ }
83+ function scheduleClick(button) {
84+ clickTimer.button = button;
85+ clickTimer.start();
86+ }
87+ }
88+
89+ function moveMouse(touchPoints) {
90+ var tp = touchPoints[0];
91+ if (isClick &&
92+ (Math.abs(tp.x - tp.startX) > clickThreshold ||
93+ Math.abs(tp.y - tp.startY) > clickThreshold)) {
94+ isClick = false;
95+ isDrag = true;
96+ }
97+
98+ uinput.moveMouse(tp.x - tp.previousX, tp.y - tp.previousY);
99+ }
100+
101+ function scroll(touchPoints) {
102+ var dh = 0;
103+ var dv = 0;
104+ var tp = touchPoints[0];
105+ if (isClick &&
106+ (Math.abs(tp.x - tp.startX) > clickThreshold ||
107+ Math.abs(tp.y - tp.startY) > clickThreshold)) {
108+ isClick = false;
109+ }
110+ dh += tp.x - tp.previousX;
111+ dv += tp.y - tp.previousY;
112+
113+ tp = touchPoints[1];
114+ if (isClick &&
115+ (Math.abs(tp.x - tp.startX) > clickThreshold ||
116+ Math.abs(tp.y - tp.startY) > clickThreshold)) {
117+ isClick = false;
118+ }
119+ dh += tp.x - tp.previousX;
120+ dv += tp.y - tp.previousY;
121+
122+ // As we added up the movement of the two fingers, let's divide it again by 2
123+ dh /= 2;
124+ dv /= 2;
125+
126+ uinput.scrollMouse(dh, dv);
127+ }
128+
129+ touchPoints: [
130+ TouchPoint {
131+ id: point1
132+ },
133+ TouchPoint {
134+ id: point2
135+ }
136+ ]
137+ }
138+}
qml/DisabledScreenNotice.qml+68−18
@@ -15,35 +15,85 @@
1515 */
1616
1717import QtQuick 2.4
18+import QtQuick.Layouts 1.1
1819import Ubuntu.Components 1.3
20+import QtQuick.Window 2.2
1921import "Components"
22+import "Rotation"
2023
21−Image {
24+Item {
2225 id: root
2326
27+ property bool infoNoteDisplayed: true
28+
29+ property int physicalOrientation: Screen.orientation
30+
2431 WallpaperResolver {
2532 width: root.width
2633 id: wallpaperResolver
2734 }
2835
29− source: wallpaperResolver.background
36+ Item {
37+ id: contentContainer
38+ anchors.centerIn: parent
39+ height: rotation == 90 || rotation == 270 ? parent.width : parent.height
40+ width: rotation == 90 || rotation == 270 ? parent.height : parent.width
3041
31− UbuntuShape {
32− anchors.fill: text
33− anchors.margins: -units.gu(2)
34− backgroundColor: "black"
35− opacity: 0.4
36− }
42+ rotation: {
43+ switch (Screen.orientation) {
44+ case Qt.PortraitOrientation:
45+ return 0;
46+ case Qt.LandscapeOrientation:
47+ return 270;
48+ case Qt.InvertedPortraitOrientation:
49+ return 180;
50+ case Qt.InvertedLandscapeOrientation:
51+ return 90;
52+ }
53+ }
54+ transformOrigin: Item.Center
3755
38− Label {
39− id: text
40− anchors.centerIn: parent
41− width: parent.width / 2
42− text: i18n.tr("Your device is now connected to an external display.")
43− color: "white"
44− horizontalAlignment: Text.AlignHCenter
45− verticalAlignment: Text.AlignVCenter
46− fontSize: "x-large"
47− wrapMode: Text.Wrap
56+ VirtualTouchPad {
57+ anchors.fill: parent
58+
59+ onPressedChanged: {
60+ if (pressed && infoNoteDisplayed) {
61+ infoNoteDisplayed = false;
62+ }
63+ }
64+ }
65+
66+ Image {
67+ anchors.fill: parent
68+ source: wallpaperResolver.background
69+ }
70+
71+ Item {
72+ objectName: "infoNoticeArea"
73+ anchors.fill: parent
74+ opacity: infoNoteDisplayed ? 1 : 0
75+ visible: opacity > 0
76+ Behavior on opacity {
77+ UbuntuNumberAnimation { }
78+ }
79+
80+ Rectangle {
81+ anchors.fill: parent
82+ color: "black"
83+ opacity: 0.4
84+ }
85+
86+ Label {
87+ id: text
88+ anchors.centerIn: parent
89+ width: parent.width - units.gu(8)
90+ text: i18n.tr("Your device is now connected to an external display. Use this screen as a touch pad to interact with the mouse.")
91+ color: "white"
92+ horizontalAlignment: Text.AlignHCenter
93+ verticalAlignment: Text.AlignVCenter
94+ fontSize: "x-large"
95+ wrapMode: Text.Wrap
96+ }
97+ }
4898 }
4999}
tests/mocks/CMakeLists.txt+1
@@ -43,6 +43,7 @@ add_subdirectory(Unity)
4343add_subdirectory(QtMultimedia)
4444add_subdirectory(Wizard)
4545add_subdirectory(Utils)
46+add_subdirectory(UInput)
4647
4748install(
4849 DIRECTORY data
tests/mocks/UInput/CMakeLists.txtnew file+7
@@ -0,0 +1,7 @@
1+add_library(FakeUInput-qml MODULE
2+ plugin.cpp
3+ mockuinput.cpp
4+)
5+
6+qt5_use_modules(FakeUInput-qml Qml)
7+add_unity8_mock(UInput 0.1 UInput TARGETS FakeUInput-qml)
tests/mocks/UInput/mockuinput.cppnew file+73
@@ -0,0 +1,73 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+
18+#include "mockuinput.h"
19+
20+#include <QFile>
21+#include <QDebug>
22+#include <QDateTime>
23+
24+#include <unistd.h>
25+
26+MockUInput::MockUInput(QObject *parent) :
27+ QObject(parent)
28+{
29+}
30+
31+MockUInput::~MockUInput()
32+{
33+}
34+
35+void MockUInput::createMouse()
36+{
37+ if (m_mouseCreated) {
38+ qDebug() << "Already have a virtual device. Not creating another one.";
39+ return;
40+ }
41+ m_mouseCreated = true;
42+ Q_EMIT mouseCreated();
43+}
44+
45+void MockUInput::removeMouse()
46+{
47+ if (!m_mouseCreated) {
48+ return;
49+ }
50+ qDebug() << "Virtual uinput mouse device removed.";
51+ m_mouseCreated = false;
52+ Q_EMIT mouseRemoved();
53+}
54+
55+void MockUInput::moveMouse(int dx, int dy)
56+{
57+ Q_EMIT mouseMoved(dx, dy);
58+}
59+
60+void MockUInput::pressMouse(Button button)
61+{
62+ Q_EMIT mousePressed(button);
63+}
64+
65+void MockUInput::releaseMouse(Button button)
66+{
67+ Q_EMIT mouseReleased(button);
68+}
69+
70+void MockUInput::scrollMouse(int dh, int dv)
71+{
72+ Q_EMIT mouseScrolled(dh, dv);
73+}
tests/mocks/UInput/mockuinput.hnew file+63
@@ -0,0 +1,63 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+
18+#ifndef MOCKUINPUT_H
19+#define MOCKUINPUT_H
20+
21+#include <QObject>
22+#include <QFile>
23+
24+#include <linux/uinput.h>
25+
26+
27+class MockUInput : public QObject
28+{
29+ Q_OBJECT
30+ Q_ENUMS(Button)
31+
32+public:
33+ enum Button {
34+ ButtonLeft,
35+ ButtonRight,
36+ ButtonMiddle
37+ };
38+
39+ explicit MockUInput(QObject *parent = nullptr);
40+ ~MockUInput();
41+
42+ Q_INVOKABLE void createMouse();
43+ Q_INVOKABLE void removeMouse();
44+
45+ Q_INVOKABLE void moveMouse(int dx, int dy);
46+ Q_INVOKABLE void pressMouse(Button button);
47+ Q_INVOKABLE void releaseMouse(Button button);
48+ Q_INVOKABLE void scrollMouse(int dh, int dv);
49+
50+Q_SIGNALS:
51+ // for testing
52+ void mouseCreated();
53+ void mouseRemoved();
54+ void mouseMoved(int dx, int dy);
55+ void mousePressed(Button button);
56+ void mouseReleased(Button button);
57+ void mouseScrolled(int dh, int dv);
58+
59+private:
60+ bool m_mouseCreated = false;
61+};
62+
63+#endif // MOCKUINPUT_H
tests/mocks/UInput/plugin.cppnew file+26
@@ -0,0 +1,26 @@
1+/*
2+ * Copyright (C) 2015 Canonical Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 3, as published
6+ * by the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but
9+ * WITHOUT ANY WARRANTY; without even the implied warranties of
10+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11+ * PURPOSE. See the GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License along
14+ * with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "plugin.h"
18+#include "mockuinput.h"
19+
20+#include <QtQml/qqml.h>
21+
22+void MockUInputPlugin::registerTypes(const char *uri)
23+{
24+ Q_ASSERT(uri == QLatin1String("UInput"));
25+ qmlRegisterType<MockUInput>(uri, 0, 1, "UInput");
26+}
tests/mocks/UInput/plugin.hnew file+32
@@ -0,0 +1,32 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef MOCKUINPUT_PLUGIN_H
18+#define MOCKUINPUT_PLUGIN_H
19+
20+#include <QtQml/QQmlEngine>
21+#include <QtQml/QQmlExtensionPlugin>
22+
23+class MockUInputPlugin : public QQmlExtensionPlugin
24+{
25+ Q_OBJECT
26+ Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
27+
28+public:
29+ void registerTypes(const char *uri) override;
30+};
31+
32+#endif
tests/mocks/UInput/qmldirnew file+2
@@ -0,0 +1,2 @@
1+module UInput
2+plugin FakeUInput-qml
tests/qmltests/CMakeLists.txt+1
@@ -17,6 +17,7 @@ add_unity8_qmltest(Components Rating)
1717add_unity8_qmltest(Components ResponsiveGridView)
1818add_unity8_qmltest(Components ResponsiveVerticalJournal)
1919add_unity8_qmltest(Components Showable)
20+add_unity8_qmltest(Components VirtualTouchPad)
2021add_unity8_qmltest(Components WallpaperResolver)
2122add_unity8_qmltest(Components ZoomableImage)
2223add_unity8_qmltest(Dash Dash)
tests/qmltests/Components/tst_VirtualTouchPad.qmlnew file+143
@@ -0,0 +1,143 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.4
18+import QtTest 1.0
19+import Unity.Test 0.1
20+import UInput 0.1
21+import "../../../qml/Components"
22+
23+
24+Item {
25+ id: root
26+ width: units.gu(70)
27+ height: units.gu(70)
28+
29+ VirtualTouchPad {
30+ id: touchScreenPad
31+ anchors.fill: parent
32+ }
33+
34+ SignalSpy {
35+ id: mouseEventSpy1
36+ target: touchScreenPad.uinput
37+ }
38+ SignalSpy {
39+ id: mouseEventSpy2
40+ target: touchScreenPad.uinput
41+ }
42+
43+ UnityTestCase {
44+ id: testCase
45+ name: "VirtualTouchPad"
46+ when: windowShown
47+
48+ function init() {
49+ mouseEventSpy1.clear();
50+ mouseEventSpy2.clear();
51+ }
52+
53+ function test_click() {
54+ mouseEventSpy1.signalName = "mousePressed"
55+ mouseEventSpy2.signalName = "mouseReleased"
56+
57+ var touchPadArea = findChild(touchScreenPad, "touchPadArea");
58+
59+ tap(touchPadArea)
60+ tryCompare(mouseEventSpy1, "count", 1)
61+ tryCompare(mouseEventSpy2, "count", 1)
62+ }
63+
64+ function test_doubleClick() {
65+ mouseEventSpy1.signalName = "mousePressed"
66+ mouseEventSpy2.signalName = "mouseReleased"
67+
68+ var touchPadArea = findChild(touchScreenPad, "touchPadArea");
69+
70+ tap(touchPadArea)
71+ tap(touchPadArea)
72+
73+ tryCompare(mouseEventSpy1, "count", 2)
74+ tryCompare(mouseEventSpy2, "count", 2)
75+ }
76+
77+ function test_move() {
78+ mouseEventSpy1.signalName = "mouseMoved"
79+
80+ var touchPadArea = findChild(touchScreenPad, "touchPadArea");
81+
82+ var moveDiff = units.gu(2);
83+ var moveSteps = 5;
84+
85+ touchFlick(touchPadArea, units.gu(1), units.gu(1), units.gu(1) + moveDiff, units.gu(1) + moveDiff, true, true, 10, moveSteps)
86+
87+ tryCompare(mouseEventSpy1, "count", moveSteps)
88+ var movedX = 0;
89+ var movedY = 0
90+ for (var i = 0; i < 5; i++) {
91+ movedX += mouseEventSpy1.signalArguments[i][0]
92+ movedY += mouseEventSpy1.signalArguments[i][1]
93+ }
94+ compare(movedX, moveDiff)
95+ compare(movedY, moveDiff)
96+ }
97+
98+ function test_doubleTapAndHoldToDrag() {
99+ mouseEventSpy1.signalName = "mousePressed"
100+ mouseEventSpy2.signalName = "mouseReleased"
101+
102+ var touchPadArea = findChild(touchScreenPad, "touchPadArea");
103+
104+ mouseClick(touchPadArea)
105+ mousePress(touchPadArea)
106+ mouseMove(touchPadArea, touchPadArea.width / 4, touchPadArea.height / 4)
107+
108+ tryCompare(mouseEventSpy1, "count", 1)
109+ tryCompare(mouseEventSpy2, "count", 0)
110+ mouseRelease(touchPadArea)
111+ tryCompare(mouseEventSpy2, "count", 1)
112+ }
113+
114+ function test_twoFingerScroll() {
115+ var touchPadArea = findChild(touchScreenPad, "touchPadArea");
116+
117+ mouseEventSpy1.signalName = "mouseScrolled"
118+
119+ var startX = touchPadArea.width / 2;
120+ var startY = touchPadArea.height / 2;
121+
122+ var startX1 = startX - units.gu(1);
123+ var startX2 = startX + units.gu(1);
124+
125+ var event = touchEvent(touchPadArea)
126+ event.press(0, startX1, startY)
127+ event.press(1, startX2, startY);
128+ event.commit()
129+
130+ for (var i = 0; i < 10; i++) {
131+ event.move(0, startX1, startY + units.gu(i))
132+ event.move(1, startX2, startY + units.gu(i));
133+ event.commit();
134+
135+ tryCompare(mouseEventSpy1, "count", i + 1);
136+ }
137+
138+ event.release(0, startX1, startY + units.gu(11))
139+ event.release(1, startX2, startY + units.gu(11));
140+ event.commit();
141+ }
142+ }
143+}
tests/qmltests/tst_DisabledScreenNotice.qml+10−2
@@ -19,13 +19,13 @@ import QtTest 1.0
1919import Unity.Test 0.1
2020import "../../qml"
2121
22−
2322Item {
2423 id: root
25− width: units.gu(70)
24+ width: units.gu(40)
2625 height: units.gu(70)
2726
2827 DisabledScreenNotice {
28+ id: touchScreenPad
2929 anchors.fill: parent
3030 }
3131
@@ -33,5 +33,13 @@ Item {
3333 id: testCase
3434 name: "DisabledScreenNotice"
3535 when: windowShown
36+
37+ function test_mouseAreaHidesOnFirstTap() {
38+ var noticeArea = findChild(touchScreenPad, "infoNoticeArea")
39+ print("noticeArea", noticeArea.visible)
40+ compare(noticeArea.visible, true)
41+ tap(root)
42+ tryCompare(noticeArea, "visible", false)
43+ }
3644 }
3745}