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.3
20 import Unity.InputInfo 0.1
21 
22 Item {
23  id: root
24  readonly property alias mice: priv.miceCount
25  readonly property alias keyboards: priv.keyboardCount
26 
27  property alias inputInfo: inputInfo
28 
29  QtObject {
30  id: priv
31 
32  property var mice: []
33  property var keyboards: []
34 
35  property int miceCount: 0
36  property int keyboardCount: 0
37 
38  function addMouse(devicePath) {
39  mice.push(devicePath);
40  miceCount++;
41  }
42 
43  function addKeyboard(devicePath) {
44  keyboards.push(devicePath);
45  keyboardCount++;
46  }
47 
48  function removeDevice(devicePath) {
49  for (var i = 0; i < priv.mice.length; i++) {
50  if (priv.mice[i] == devicePath) {
51  priv.mice.splice(i, 1);
52  priv.miceCount--;
53  }
54  }
55  for (var i = 0; i < priv.keyboards.length; i++) {
56  if (priv.keyboards[i] == devicePath) {
57  priv.keyboards.splice(i, 1);
58  priv.keyboardCount--;
59  }
60  }
61  }
62  }
63 
64  InputDeviceInfo {
65  id: inputInfo
66  objectName: "inputDeviceInfo"
67 
68  onNewDevice: {
69  var device = inputInfo.get(inputInfo.indexOf(devicePath));
70  if (device === null) {
71  return;
72  }
73 
74  var hasMouse = (device.types & InputInfo.Mouse) == InputInfo.Mouse
75  var hasTouchpad = (device.types & InputInfo.TouchPad) == InputInfo.TouchPad
76  var hasKeyboard = (device.types & InputInfo.Keyboard) == InputInfo.Keyboard
77 
78  if (hasMouse || hasTouchpad) {
79  priv.addMouse(devicePath);
80  } else if (hasKeyboard) {
81  // Only accepting keyboards that do not claim to be a mouse too
82  // This will be a bit buggy for real hybrid devices, but doesn't
83  // fall for Microsoft mice that claim to be Keyboards too.
84  priv.addKeyboard(devicePath)
85  }
86  }
87  onDeviceRemoved: {
88  priv.removeDevice(devicePath)
89  }
90  }
91 }