2012-08-20 18:19:50 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported getPointerWatcher */
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2021-05-06 16:31:11 -04:00
|
|
|
const { GLib } = imports.gi;
|
2012-08-20 18:19:50 -04:00
|
|
|
|
|
|
|
// We stop polling if the user is idle for more than this amount of time
|
2017-07-18 13:47:27 -04:00
|
|
|
var IDLE_TIME = 1000;
|
2012-08-20 18:19:50 -04:00
|
|
|
|
|
|
|
// This file implements a reasonably efficient system for tracking the position
|
|
|
|
// of the mouse pointer. We simply query the pointer from the X server in a loop,
|
|
|
|
// but we turn off the polling when the user is idle.
|
|
|
|
|
|
|
|
let _pointerWatcher = null;
|
|
|
|
function getPointerWatcher() {
|
|
|
|
if (_pointerWatcher == null)
|
|
|
|
_pointerWatcher = new PointerWatcher();
|
|
|
|
|
|
|
|
return _pointerWatcher;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var PointerWatch = class {
|
|
|
|
constructor(watcher, interval, callback) {
|
2012-08-20 18:19:50 -04:00
|
|
|
this.watcher = watcher;
|
|
|
|
this.interval = interval;
|
|
|
|
this.callback = callback;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
|
|
|
// remove:
|
|
|
|
// remove this watch. This function may safely be called
|
|
|
|
// while the callback is executing.
|
2017-10-30 20:03:21 -04:00
|
|
|
remove() {
|
2012-08-20 18:19:50 -04:00
|
|
|
this.watcher._removeWatch(this);
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var PointerWatcher = class {
|
|
|
|
constructor() {
|
2021-05-06 16:31:11 -04:00
|
|
|
this._idleMonitor = global.backend.get_core_idle_monitor();
|
2017-12-01 19:27:35 -05:00
|
|
|
this._idleMonitor.add_idle_watch(IDLE_TIME, this._onIdleMonitorBecameIdle.bind(this));
|
2013-01-17 16:11:01 -05:00
|
|
|
this._idle = this._idleMonitor.get_idletime() > IDLE_TIME;
|
2012-08-20 18:19:50 -04:00
|
|
|
this._watches = [];
|
|
|
|
this.pointerX = null;
|
|
|
|
this.pointerY = null;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
|
|
|
// addWatch:
|
|
|
|
// @interval: hint as to the time resolution needed. When the user is
|
|
|
|
// not idle, the position of the pointer will be queried at least
|
|
|
|
// once every this many milliseconds.
|
2017-10-30 20:03:21 -04:00
|
|
|
// @callback to call when the pointer position changes - takes
|
2012-08-20 18:19:50 -04:00
|
|
|
// two arguments, X and Y.
|
|
|
|
//
|
|
|
|
// Set up a watch on the position of the mouse pointer. Returns a
|
|
|
|
// PointerWatch object which has a remove() method to remove the watch.
|
2017-10-30 20:03:21 -04:00
|
|
|
addWatch(interval, callback) {
|
2012-08-20 18:19:50 -04:00
|
|
|
// Avoid unreliably calling the watch for the current position
|
|
|
|
this._updatePointer();
|
|
|
|
|
|
|
|
let watch = new PointerWatch(this, interval, callback);
|
|
|
|
this._watches.push(watch);
|
|
|
|
this._updateTimeout();
|
|
|
|
return watch;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_removeWatch(watch) {
|
2012-08-20 18:19:50 -04:00
|
|
|
for (let i = 0; i < this._watches.length; i++) {
|
|
|
|
if (this._watches[i] == watch) {
|
|
|
|
this._watches.splice(i, 1);
|
|
|
|
this._updateTimeout();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_onIdleMonitorBecameActive() {
|
2012-08-20 00:06:50 -04:00
|
|
|
this._idle = false;
|
|
|
|
this._updatePointer();
|
|
|
|
this._updateTimeout();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2019-02-04 06:30:53 -05:00
|
|
|
_onIdleMonitorBecameIdle() {
|
2012-08-20 00:06:50 -04:00
|
|
|
this._idle = true;
|
2017-12-01 19:27:35 -05:00
|
|
|
this._idleMonitor.add_user_active_watch(this._onIdleMonitorBecameActive.bind(this));
|
2012-08-20 18:19:50 -04:00
|
|
|
this._updateTimeout();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateTimeout() {
|
2012-08-20 18:19:50 -04:00
|
|
|
if (this._timeoutId) {
|
2019-08-19 14:50:33 -04:00
|
|
|
GLib.source_remove(this._timeoutId);
|
2012-08-20 18:19:50 -04:00
|
|
|
this._timeoutId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._idle || this._watches.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let minInterval = this._watches[0].interval;
|
|
|
|
for (let i = 1; i < this._watches.length; i++)
|
|
|
|
minInterval = Math.min(this._watches[i].interval, minInterval);
|
|
|
|
|
2019-08-19 14:50:33 -04:00
|
|
|
this._timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, minInterval,
|
|
|
|
this._onTimeout.bind(this));
|
2014-04-10 13:26:52 -04:00
|
|
|
GLib.Source.set_name_by_id(this._timeoutId, '[gnome-shell] this._onTimeout');
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onTimeout() {
|
2012-08-20 18:19:50 -04:00
|
|
|
this._updatePointer();
|
2013-11-28 19:45:39 -05:00
|
|
|
return GLib.SOURCE_CONTINUE;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-20 18:19:50 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updatePointer() {
|
2019-02-04 06:30:53 -05:00
|
|
|
let [x, y] = global.get_pointer();
|
2012-08-20 18:19:50 -04:00
|
|
|
if (this.pointerX == x && this.pointerY == y)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.pointerX = x;
|
|
|
|
this.pointerY = y;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._watches.length;) {
|
|
|
|
let watch = this._watches[i];
|
|
|
|
watch.callback(x, y);
|
|
|
|
if (watch == this._watches[i]) // guard against self-removal
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|