Unity 8
UnityInputInfo.qml
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  */
16 
17 pragma Singleton
18 
19 import QtQuick 2.4
20 import Unity.InputInfo 0.1
21 // Workaround https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1473471
22 import Ubuntu.Components 1.3
23 
24 Item {
25  id: root
26  readonly property alias mice: priv.miceCount
27  readonly property alias keyboards: priv.keyboardCount
28 
29  property alias inputInfo: inputInfo
30 
31  QtObject {
32  id: priv
33 
34  property var mice: []
35  property var keyboards: []
36 
37  property int miceCount: 0
38  property int keyboardCount: 0
39 
40  function addMouse(devicePath) {
41  mice.push(devicePath);
42  miceCount++;
43  }
44 
45  function addKeyboard(devicePath) {
46  keyboards.push(devicePath);
47  keyboardCount++;
48  }
49 
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  }
65 
66  InputDeviceInfo {
67  id: inputInfo
68  objectName: "inputDeviceInfo"
69 
70  onNewDevice: {
71  var device = inputInfo.get(inputInfo.indexOf(devicePath));
72  if (device === null) {
73  return;
74  }
75 
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
79 
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  }
93 }