on d28fa91 help sign in
unity8/qml/Components/UnityInputInfo/UnityInputInfo.qml
93 lines 2.9 kB history raw
1/*
2 * Copyright 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 Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17pragma Singleton
19import QtQuick 2.3
20import Unity.InputInfo 0.1
21// Workaround https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1473471
22import Ubuntu.Components 1.2
24Item {
25 id: root
26 readonly property alias mice: priv.miceCount
27 readonly property alias keyboards: priv.keyboardCount
29 property alias inputInfo: inputInfo
31 QtObject {
32 id: priv
34 property var mice: []
35 property var keyboards: []
37 property int miceCount: 0
38 property int keyboardCount: 0
40 function addMouse(devicePath) {
41 mice.push(devicePath);
42 miceCount++;
43 }
45 function addKeyboard(devicePath) {
46 keyboards.push(devicePath);
47 keyboardCount++;
48 }
50 function removeDevice(devicePath) {
51 for (var i = 0; i < priv.mice.length; i++) {
52 if (priv.mice[i] == devicePath) {
53 priv.mice.splice(i, 1);
54 priv.miceCount--;
55 }
56 }
57 for (var i = 0; i < priv.keyboards.length; i++) {
58 if (priv.keyboards[i] == devicePath) {
59 priv.keyboards.splice(i, 1);
60 priv.keyboardCount--;
61 }
62 }
63 }
64 }
66 InputDeviceInfo {
67 id: inputInfo
68 objectName: "inputDeviceInfo"
70 onNewDevice: {
71 var device = inputInfo.get(inputInfo.indexOf(devicePath));
72 if (device === null) {
73 return;
74 }
76 var hasMouse = (device.types & InputInfo.Mouse) == InputInfo.Mouse
77 var hasTouchpad = (device.types & InputInfo.TouchPad) == InputInfo.TouchPad
78 var hasKeyboard = (device.types & InputInfo.Keyboard) == InputInfo.Keyboard
80 if (hasMouse || hasTouchpad) {
81 priv.addMouse(devicePath);
82 } else if (hasKeyboard) {
83 // Only accepting keyboards that do not claim to be a mouse too
84 // This will be a bit buggy for real hybrid devices, but doesn't
85 // fall for Microsoft mice that claim to be Keyboards too.
86 priv.addKeyboard(devicePath)
87 }
88 }
89 onDeviceRemoved: {
90 priv.removeDevice(devicePath)
91 }
92 }