-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxinput.js
167 lines (149 loc) · 5.21 KB
/
xinput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright 2011-2013 Armin Köhler <orangeshirt at web.de>
*
* Thanks to Lorenzo Carbonell Cerezo and Miguel Angel Santamaría Rogado
* which has written touchpad-indicator
* (https://launchpad.net/touchpad-indicator) as python app and inspired
* myself to write this extension for gnome-shell.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to:
* The Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA.
*/
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Lib = Me.imports.lib;
let logging = Lib.logging;
function XInput(devices) {
this._init(devices);
};
XInput.prototype = {
_init: function(devices) {
logging('XInput._init(' + devices + ')');
this.devices = devices;
this.ids = this._get_ids();
this.is_there_device = this._is_there_device();
logging('XInput._init(): Found Device - ' +
this.is_there_device.toString() + ' ' + this.ids);
},
_get_ids: function() {
var tpids = new Array();
let y = 0;
let all_ids = this._get_all_ids();
for (let id = 0; id < all_ids.length; id++) {
if (this._is_device(all_ids[id]) == true) {
tpids[y] = all_ids[id];
y++;
}
}
return tpids;
},
_get_all_ids: function() {
var devids = new Array();
let lines = Lib.execute_sync('xinput --list');
if (lines) {
lines = lines[1].toString().split('\n');
let y = 0;
for (let line = 0; line < lines.length; line++) {
if (lines[line].indexOf('pointer')!=-1) {
devids[y] = lines[line].toString().split('=')[1].
split('[')[0].split('\t')[0];
y++;
}
}
}
return devids;
},
_is_device: function(id) {
let comp = Lib.execute_sync('xinput --list-props ' + id.toString());
return this._search_device(comp[1]);
},
_is_there_device: function() {
if (this.ids.length > 0)
return true;
return false;
},
_search_device: function(where) {
if (where) {
where = where.toString().toLowerCase();
for (let tpid = 0; tpid < this.devices.length; tpid++) {
if (!(where.indexOf(
this.devices[tpid].toString().toLowerCase()) == -1)) {
return true;
}
}
}
return false;
},
_set_device_enabled: function(id) {
logging('XInput._set_device_enabled() id: '+id.toString());
return Lib.execute_async('xinput set-prop ' + id.toString()
+ ' "Device Enabled" 1');
},
_set_device_disabled: function(id) {
logging('XInput._set_device_disabled() id: '+id.toString());
return Lib.execute_async('xinput set-prop ' + id.toString()
+ ' "Device Enabled" 0');
},
_disable_all_devices: function() {
for (let id = 0; id < this.ids.length; id++) {
this._set_device_disabled(this.ids[id]);
}
return !this._all_devices_enabled();
},
_enable_all_devices: function() {
for (let id = 0; id < this.ids.length; id++) {
this._set_device_enabled(this.ids[id]);
}
return this._all_devices_enabled();
},
_switch_all_devices: function(state) {
for (let id = 0; id < this.ids.length; id++) {
if (state) {
this._set_device_enabled(this.ids[id]);
} else {
this._set_device_disabled(this.ids[id]);
}
}
return this._all_devices_enabled();
},
_is_device_enabled: function(id) {
logging('XInput._is_device_enabled()');
var lines = Lib.execute_sync('xinput --list-props ' + id.toString());
if (lines) {
lines = lines[1].toString().split('\n');
for (let line = 0; line < lines.length; line++) {
if (lines[line].toString().toLowerCase().indexOf(
'device enabled') != -1) {
if (lines[line].toString().split(':')[1].indexOf('1')
!= -1) {
return true;
}
}
}
}
return false;
},
_all_devices_enabled: function() {
if (!this.is_there_device) {
return false;
}
for (let id = 0; id < this.ids.length; id++) {
if (this._is_device_enabled(this.ids[id]) == false) {
return false;
}
}
return true;
}
};