forked from askmrsinh/touchpad-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
206 lines (183 loc) · 6.97 KB
/
lib.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* TouchpadIndicator - Touchpad management GNOME Shell Extension.
* Orignal work Copyright (C) 2011-2013 Armin Köhler <orangeshirt at web.de>
* Modifcations Copyright (C) 2019 Ashesh Singh <user501254 at gmail.com>
*
* This file is part of TouchpadIndicator, a fork of Armin Köhler's
* 'gnome-shell-extension-touchpad-indicator' project which is licensed GPLv2.
* Orignal source code is available at https://git.io/fjVec.
*
* 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 { Gio, GLib } = imports.gi;
const ByteArray = imports.byteArray;
const Me = imports.misc.extensionUtils.getCurrentExtension();
// Debug Mode Settings
const LOG_FILEPATH = GLib.build_filenamev([Me.path, 'touchpad-indicator.log']);
const LOG_PREFIX = `[${Me.uuid}] `;
var DEBUG = false;
var DEBUG_TO_FILE = false;
// Possible Devices
const TOUCHPADS = ['touchpad', 'glidepoint', 'fingersensingpad', 'bcm5974', 'trackpad', 'smartpad'];
const TRACKPOINTS = ['trackpoint', 'accu point', 'trackstick', 'touchstyk', 'pointing stick', 'dualpoint stick'];
const TOUCHSCREENS = ['touchscreen', 'maxtouch', 'touch digitizer', 'touch system'];
const FINGERTOUCHES = ['finger touch'];
const PENS = ['pen stylus', 'pen eraser'];
const OTHERS = [];
var ALL_TYPES = {
'touchpad': TOUCHPADS,
'trackpoint': TRACKPOINTS,
'touchscreen': TOUCHSCREENS,
'fingertouch': FINGERTOUCHES,
'pen': PENS,
'other': OTHERS
};
var ALL_TOUCHPADS = TOUCHPADS.slice();
var ALL_OTHERS = OTHERS.slice();
// Methods to enable or disable the touchpad
var METHOD = { GSETTINGS: 0, SYNCLIENT: 1, XINPUT: 2 };
//Session Type
var SESSION_TYPE = GLib.getenv('XDG_SESSION_TYPE');
function createLogFile(filepath) {
const PERMISSIONS_MODE = 0o755;
let isSuccess = false;
filepath = (filepath !== undefined) ? filepath : LOG_FILEPATH;
try {
let file = Gio.File.new_for_path(filepath);
let header = `${filepath}\n`;
if (GLib.mkdir_with_parents(file.get_parent().get_path(), PERMISSIONS_MODE) === 0) {
isSuccess = file.replace_contents(header, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null)[0];
}
return isSuccess;
} catch (err) {
let error = `Sorry could not create logfile!\n${err}`;
return [isSuccess, error];
}
}
function writeLog(contents) {
let isSuccess = false;
try {
let file = Gio.File.new_for_path(LOG_FILEPATH);
let fileOutputStream = file.append_to(Gio.FileCreateFlags.NONE, null);
isSuccess = fileOutputStream.write(contents, null).close(null);
return isSuccess;
} catch (err) {
let error = `Sorry could not write to logfile!\n${err}`;
return [isSuccess, error];
}
}
function readLog() {
let [isSuccess, contents] = [false, ''];
try {
[isSuccess, contents] = GLib.file_get_contents(LOG_FILEPATH);
return [isSuccess, ByteArray.toString(contents)];
} catch (err) {
let error = `Sorry could not read from logfile!\n${err}`;
return [isSuccess, error];
}
}
function logger(event) {
// NOTE: Each file has preemptive check on DEBUG variable.
// TODO: Structured logging.
let timestamp = new Date(new Date().getTime()).toISOString();
let message = `${timestamp} ${event}`;
if (typeof global !== 'undefined') {
global.log(LOG_PREFIX + message);
} else {
log(LOG_PREFIX + message);
}
if (DEBUG_TO_FILE) {
writeLog(`${message}\n`);
}
}
function executeCmdAsync(command) {
try {
return GLib.spawn_command_line_async(command);
} catch (err) {
logger(err.message.toString());
return false;
}
}
function executeCmdSync(command) {
let [isSuccess, stdOut] = [false, ''];
try {
[isSuccess, stdOut] = GLib.spawn_command_line_sync(command);
return [isSuccess, ByteArray.toString(stdOut).trim()];
} catch (err) {
logger(err.message.toString());
return false;
}
}
function watchLogFile(filepath) {
filepath = (filepath !== undefined) ? filepath : LOG_FILEPATH;
let file = Gio.File.new_for_path(filepath);
return file.monitor_file(Gio.FileMonitorFlags.NONE, null);
}
function watchDevInput() {
let file = Gio.file_new_for_path('/dev/input');
return file.monitor_directory(Gio.FileMonitorFlags.WATCH_MOUNTS, null);
}
function makePointingDevice(pointingDeviceLines) {
//assuming that N: & P: always appear at lines 2 and 3 respectively
if (pointingDeviceLines[1].startsWith('N: Name=') &&
pointingDeviceLines[2].startsWith('P: Phys=')) {
let pointingDevice = {};
pointingDevice.name = pointingDeviceLines[1].split('"')[1];
pointingDevice.phys = pointingDeviceLines[2].split('=')[1];
pointingDevice.type = 'mouse'; //default
//Dirty fix for ELAN Touchscreen which is present in some DELL models (e.g. XPS 9500, Precision 5500)
if (pointingDevice.name == 'ELAN29E2:00 04F3:29E2'){
pointingDevice.name = pointingDevice.name.concat(" Touchscreen");
}
for (let type in ALL_TYPES) {
if (ALL_TYPES[type].some((t) => {
return (pointingDevice.name.toLowerCase().indexOf(t) >= 0);
})) {
pointingDevice.type = type;
break;
}
}
logger(JSON.stringify(pointingDevice));
return pointingDevice;
}
}
function listPointingDevices() {
let pointingDevices = [];
let comp = executeCmdSync('cat /proc/bus/input/devices');
let allDeviceChunks = comp[1].split('\n\n');
for (let x = 0; x < allDeviceChunks.length; x++) {
if (allDeviceChunks[x].indexOf('mouse') !== -1) {
let pointingDeviceLines = allDeviceChunks[x].split('\n');
let pointingDevice = makePointingDevice(pointingDeviceLines);
if (pointingDevice !== undefined) {
pointingDevices.push(pointingDevice);
}
}
}
if (pointingDevices[0]) {
return [true, pointingDevices];
} else {
return [false, ' - No Pointing Devices detected.\n'];
}
}
function removeSource(...args) {
return GLib.source_remove(...args);
}
function addTimeout(...args) {
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, ...args);
}
/* exported DEBUG METHOD createLogFile readLog executeCmdAsync listPointingDevices watchLogFile watchDevInput removeSource addTimeout */