~wasta-linux/wasta-core-wily/master

« back to all changes in this revision

Viewing changes to install-files/extensions/touchpad-indicator@orangeshirt/lib.js

  • Committer: Rik Shaw
  • Date: 2015-11-02 14:31:40 UTC
  • mfrom: (2.1.1)
  • Revision ID: git-v1:22cdf473bc08069ccbbd6a3720beefb87dcae603
Merge pull request #1 from wasta-linux/11-2-updates

11-02 minor tweaks

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011-2013 Armin Köhler <orangeshirt at web.de>
 
3
 *
 
4
 * Thanks to Lorenzo Carbonell Cerezo and Miguel Angel Santamaría Rogado
 
5
 * which has written touchpad-indicator
 
6
 * (https://launchpad.net/touchpad-indicator) as python app and inspired
 
7
 * myself to write this extension for gnome-shell.
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify it
 
10
 * under the terms of the GNU General Public License as published by the Free
 
11
 * Software Foundation; either version 2 of the License, or (at your option)
 
12
 * any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
16
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
17
 * more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License along with
 
20
 * this program; if not, write to:
 
21
 * The Free Software Foundation, Inc.,
 
22
 * 51 Franklin Street, Fifth Floor
 
23
 * Boston, MA 02110-1301, USA.
 
24
 */
 
25
 
 
26
 
 
27
const GLib = imports.gi.GLib;
 
28
const Gio = imports.gi.Gio;
 
29
 
 
30
const Gettext = imports.gettext.domain('touchpad-indicator@orangeshirt');
 
31
const _ = Gettext.gettext;
 
32
 
 
33
const Me = imports.misc.extensionUtils.getCurrentExtension();
 
34
 
 
35
 
 
36
const StoragePath = '.local/share/gnome-shell/extensions/'+
 
37
                        Me.metadata.uuid.toString();
 
38
GLib.mkdir_with_parents(StoragePath, parseInt('0775', 8));
 
39
 
 
40
// Debug Mode Settings
 
41
var DEBUG = false; // overwritten by settings
 
42
const FORCE_DEBUG = false;
 
43
var DEBUG_TO_FILE = false; // overwritten by settings
 
44
var DEBUG_INFO = 'Extension '+ Me.metadata.name.toString() +': ';
 
45
var DEBUG_LOG_FILE = GLib.build_filenamev([StoragePath,
 
46
   'touchpad-indicator.log']);
 
47
if (GLib.file_test(DEBUG_LOG_FILE, GLib.FileTest.EXISTS) === false)
 
48
    GLib.file_set_contents(DEBUG_LOG_FILE, "");
 
49
    
 
50
 
 
51
let LOGS = "";
 
52
 
 
53
// Possible Devices
 
54
const TOUCHPADS = new Array('touchpad','glidepoint','fingersensingpad',
 
55
                            'bcm5974','trackpad','smartpad');
 
56
var ALL_TOUCHPADS = TOUCHPADS.slice();
 
57
const TRACKPOINTS = new Array('trackpoint','accu point','trackstick',
 
58
                              'touchstyk','pointing stick','dualpoint stick');
 
59
const FINGER_TOUCHES = Array('finger touch');
 
60
const TOUCHSCREENS = Array('touchscreen', 'maxtouch');
 
61
const PENS = Array('pen stylus', 'pen eraser');
 
62
const OTHERS = new Array();
 
63
var ALL_OTHERS = OTHERS.slice();
 
64
 
 
65
// Methods to en- or disable the touchpad
 
66
const METHOD = {
 
67
    GCONF: 0,
 
68
    SYNCLIENT: 1,
 
69
    XINPUT: 2
 
70
};
 
71
 
 
72
// Settings
 
73
const SETTINGS_SCHEMA = 'org.gnome.shell.extensions.touchpad-indicator';
 
74
const TOUCHPAD_SETTINGS_SCHEMA =
 
75
    'org.gnome.desktop.peripherals.touchpad';
 
76
 
 
77
 
 
78
function get_logs() {
 
79
    let logtxt;
 
80
    try {
 
81
        logtxt = GLib.file_get_contents(DEBUG_LOG_FILE)[1].toString();
 
82
    } catch (err) {
 
83
        logtxt = _("Sorry could not read logfile!\n") + err;
 
84
    }
 
85
    return logtxt;
 
86
}
 
87
 
 
88
function logging(message, debug, debug_to_file) {
 
89
    if (debug === undefined) {
 
90
        debug = DEBUG;
 
91
    } else {
 
92
        DEBUG = debug;
 
93
    }
 
94
    if (debug_to_file === undefined) {
 
95
        debug_to_file = DEBUG_TO_FILE;
 
96
    } else {
 
97
        DEBUG_TO_FILE = debug_to_file;
 
98
    }
 
99
    if (debug || FORCE_DEBUG) {
 
100
        let timestamp = format_time(new Date(new Date().getTime()));
 
101
        message = timestamp + "    " + message + "\n";
 
102
        global.log(DEBUG_INFO + message);
 
103
        if (debug_to_file) {
 
104
            let file = get_logs();
 
105
            let txt = file + message;
 
106
            GLib.file_set_contents(DEBUG_LOG_FILE, txt);
 
107
        }
 
108
    }
 
109
};
 
110
 
 
111
function format_time(d) {
 
112
    function pad(n) { return n < 10 ? '0' + n : n; }
 
113
    return d.getUTCFullYear()+'-'
 
114
        + pad(d.getUTCMonth()+1)+'-'
 
115
        + pad(d.getUTCDate())+'T'
 
116
        + pad(d.getUTCHours())+':'
 
117
        + pad(d.getUTCMinutes())+':'
 
118
        + pad(d.getUTCSeconds())+'Z';
 
119
};
 
120
 
 
121
function execute_sync(command) {
 
122
    try {
 
123
        return GLib.spawn_command_line_sync(command);
 
124
    } catch (err) {
 
125
        logging(err.message.toString());
 
126
        return false;
 
127
    }
 
128
};
 
129
 
 
130
function execute_async(command) {
 
131
    try {
 
132
        return GLib.spawn_command_line_async(command);
 
133
    } catch (err) {
 
134
        logging(err.message.toString());
 
135
        return false;
 
136
    }
 
137
};
 
138
 
 
139
function list_mouse_devices() {
 
140
    logging('Lib.list_mouse_devices()');
 
141
    let comp = execute_sync('cat /proc/bus/input/devices');
 
142
    if (comp) {
 
143
        let where = comp[1].toString().split("\n\n"),
 
144
            mouses = new Array(),
 
145
            name,
 
146
            hits = 0;
 
147
        for (let x = 0; x < where.length; x++) {
 
148
            if (!(where[x].indexOf('mouse') == -1)) {
 
149
                let data = where[x].toString().split("\n");
 
150
                for (let z = 0; z < data.length; z++) {
 
151
                    if (!(data[z].indexOf("N: Name=") == -1)) {
 
152
                        name = data[z].split("\"")[1];
 
153
                        logging('Lib.list_mouse_devices(): Device found: '
 
154
                            + name.toString());
 
155
                        mouses[hits] = name.toString();
 
156
                        hits++;
 
157
                    }
 
158
                }
 
159
            }
 
160
        }
 
161
        if (mouses[0]) {
 
162
            return [true, mouses];
 
163
        } else {
 
164
            logging('Lib.list_mouse_devices(): Could not detect a mouse device');
 
165
            return [false, _("    - No mouse device detected.") + "\n"];
 
166
        }
 
167
    }
 
168
    logging('Lib.list_mouse_devices(): Sorry "cat" has no output');
 
169
    return [false, _("    - No mouse device detected.") + "\n"];
 
170
};
 
171
 
 
172
function search_touchpads() {
 
173
    logging('Lib.search_touchpads()');
 
174
    var where = list_mouse_devices();
 
175
    if (where[0]) {
 
176
        where = where[1];
 
177
        let touchpads = "";
 
178
        let hits = 0;
 
179
        for (let x = 0; x < where.length; x++) {
 
180
            for (let tpd = 0; tpd < TOUCHPADS.length; tpd++) {
 
181
                if (!(where[x].toLowerCase().indexOf(
 
182
                        TOUCHPADS[tpd].toString()) == -1)) {
 
183
                    logging('Lib.search_touchpads(): Touchpad found: '
 
184
                        + where[x].toString());
 
185
                    if (hits > 0)
 
186
                        touchpads += " | ";
 
187
                    touchpads += where[x].toString();
 
188
                    hits++;
 
189
                }
 
190
            }
 
191
        }
 
192
        if (touchpads != "") {
 
193
            return [true, touchpads + "\n"];
 
194
        } else {
 
195
            logging('Lib.search_touchpads(): Could not detect a touchpad');
 
196
            return [false, _("No Touchpad detected.") + "\n"];
 
197
        }
 
198
    }
 
199
    logging('Lib.search_touchpads(): Sorry "cat" has no output');
 
200
    return [false, _("No Touchpad detected.") + "\n"];
 
201
};
 
202
 
 
203
function list_mouses(skip_excluded) {
 
204
    logging('Lib.list_mouses()');
 
205
    let where = list_mouse_devices(),
 
206
        mouses = new Array(false, []);
 
207
    logging('Lib.list_mouses(): ' + where.toString());
 
208
    if (where[0]) {
 
209
        where = where[1];
 
210
        let hits = 0;
 
211
        for (let x = 0; x < where.length; x++) {
 
212
            for (let tpd = 0; tpd < TOUCHPADS.length; tpd++) {
 
213
                if (!(where[x].toLowerCase().indexOf(
 
214
                        TOUCHPADS[tpd].toString()) == -1)) {
 
215
                    logging('Lib.list_mouses(): Touchpad found: '
 
216
                        + where[x].toString());
 
217
                    hits++;
 
218
                    break;
 
219
                }
 
220
            }
 
221
            for (let tpt = 0; tpt < TRACKPOINTS.length; tpt++) {
 
222
                if (!(where[x].toLowerCase().indexOf(
 
223
                        TRACKPOINTS[tpt].toString()) == -1)) {
 
224
                    logging('Lib.list_mouses(): Trackpoint found: '
 
225
                        + where[x].toString());
 
226
                    hits++;
 
227
                    break;
 
228
                }
 
229
            }
 
230
            for (let tch = 0; tch < TOUCHSCREENS.length; tch++) {
 
231
                if (!(where[x].toLowerCase().indexOf(
 
232
                        TOUCHSCREENS[tch].toString()) == -1)) {
 
233
                    logging('Lib.list_mouses(): Touchscreen found: '
 
234
                        + where[x].toString());
 
235
                    hits++;
 
236
                    break;
 
237
                }
 
238
            }
 
239
            for (let tch = 0; tch < FINGER_TOUCHES.length; tch++) {
 
240
                if (!(where[x].toLowerCase().indexOf(
 
241
                        FINGER_TOUCHES[tch].toString()) == -1)) {
 
242
                    logging('Lib.list_mouses(): Fingertouch found: '
 
243
                        + where[x].toString());
 
244
                    hits++;
 
245
                    break;
 
246
                }
 
247
            }
 
248
            for (let pen = 0; pen < PENS.length; pen++) {
 
249
                if (!(where[x].toLowerCase().indexOf(
 
250
                        PENS[pen].toString()) == -1)) {
 
251
                    logging('Lib.list_mouses(): Pen found: '
 
252
                        + where[x].toString());
 
253
                    hits++;
 
254
                    break;
 
255
                }
 
256
            }
 
257
            if (skip_excluded) {
 
258
                for (let oth = 0; oth < ALL_OTHERS.length; oth++) {
 
259
                    if (!(where[x].toLowerCase().indexOf(
 
260
                            ALL_OTHERS[oth].toString()) == -1)) {
 
261
                        hits++;
 
262
                        logging('Lib.list_mouses(): Other device to ignore'
 
263
                            + ' found: '+ where[x].toString());
 
264
                        break;
 
265
                    }
 
266
                }
 
267
            }
 
268
            if (hits == 0) {
 
269
                logging('Lib.list_mouses(): Mouse found: '
 
270
                    + where[x].toString());
 
271
                mouses[0] = true;
 
272
                mouses[1][mouses[1].length] = where[x].toString();
 
273
            } else {
 
274
                hits = 0;
 
275
            }
 
276
        }
 
277
    }
 
278
    if (!mouses[0])
 
279
        logging('Lib.list_mouses(): Could not detect a mouse ');
 
280
    return mouses;
 
281
};
 
282
 
 
283
function watch_mouse() {
 
284
    let file = Gio.file_new_for_path("/dev/input")
 
285
    return file.monitor_directory(Gio.FileMonitorFlags.NONE, null);
 
286
};
 
287
 
 
288
function load_excluded_mouses(excluded_mouses) {
 
289
    ALL_OTHERS = OTHERS.slice();
 
290
    for(var key in excluded_mouses) {
 
291
        if (excluded_mouses[key])
 
292
            ALL_OTHERS[ALL_OTHERS.length] = key.toString().toLowerCase();
 
293
    }
 
294
    return ALL_OTHERS;
 
295
};