2012-08-20 22:19:50 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported getPointerWatcher */
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2023-06-08 04:52:46 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2012-08-20 22:19:50 +00:00
|
|
|
|
|
|
|
// We stop polling if the user is idle for more than this amount of time
|
2017-07-18 17:47:27 +00:00
|
|
|
var IDLE_TIME = 1000;
|
2012-08-20 22:19:50 +00: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-31 01:19:44 +00:00
|
|
|
var PointerWatch = class {
|
|
|
|
constructor(watcher, interval, callback) {
|
2012-08-20 22:19:50 +00:00
|
|
|
this.watcher = watcher;
|
|
|
|
this.interval = interval;
|
|
|
|
this.callback = callback;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
|
|
|
// remove:
|
|
|
|
// remove this watch. This function may safely be called
|
|
|
|
// while the callback is executing.
|
2017-10-31 00:03:21 +00:00
|
|
|
remove() {
|
2012-08-20 22:19:50 +00:00
|
|
|
this.watcher._removeWatch(this);
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
};
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2017-10-31 01:19:44 +00:00
|
|
|
var PointerWatcher = class {
|
|
|
|
constructor() {
|
2021-05-06 20:31:11 +00:00
|
|
|
this._idleMonitor = global.backend.get_core_idle_monitor();
|
2017-12-02 00:27:35 +00:00
|
|
|
this._idleMonitor.add_idle_watch(IDLE_TIME, this._onIdleMonitorBecameIdle.bind(this));
|
2013-01-17 21:11:01 +00:00
|
|
|
this._idle = this._idleMonitor.get_idletime() > IDLE_TIME;
|
2012-08-20 22:19:50 +00:00
|
|
|
this._watches = [];
|
|
|
|
this.pointerX = null;
|
|
|
|
this.pointerY = null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00: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-31 00:03:21 +00:00
|
|
|
// @callback to call when the pointer position changes - takes
|
2012-08-20 22:19:50 +00: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-31 00:03:21 +00:00
|
|
|
addWatch(interval, callback) {
|
2012-08-20 22:19:50 +00: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-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_removeWatch(watch) {
|
2012-08-20 22:19:50 +00: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-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2019-02-04 11:30:53 +00:00
|
|
|
_onIdleMonitorBecameActive() {
|
2012-08-20 04:06:50 +00:00
|
|
|
this._idle = false;
|
|
|
|
this._updatePointer();
|
|
|
|
this._updateTimeout();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2019-02-04 11:30:53 +00:00
|
|
|
_onIdleMonitorBecameIdle() {
|
2012-08-20 04:06:50 +00:00
|
|
|
this._idle = true;
|
2017-12-02 00:27:35 +00:00
|
|
|
this._idleMonitor.add_user_active_watch(this._onIdleMonitorBecameActive.bind(this));
|
2012-08-20 22:19:50 +00:00
|
|
|
this._updateTimeout();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updateTimeout() {
|
2012-08-20 22:19:50 +00:00
|
|
|
if (this._timeoutId) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._timeoutId);
|
2012-08-20 22:19:50 +00: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 18:50:33 +00:00
|
|
|
this._timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, minInterval,
|
|
|
|
this._onTimeout.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._timeoutId, '[gnome-shell] this._onTimeout');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onTimeout() {
|
2012-08-20 22:19:50 +00:00
|
|
|
this._updatePointer();
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_CONTINUE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-08-20 22:19:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_updatePointer() {
|
2019-02-04 11:30:53 +00:00
|
|
|
let [x, y] = global.get_pointer();
|
2012-08-20 22:19:50 +00: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-31 01:19:44 +00:00
|
|
|
};
|