2023-10-31 08:32:40 -04:00
|
|
|
import App from 'resource:///com/github/Aylur/ags/app.js';
|
|
|
|
import Service from 'resource:///com/github/Aylur/ags/service.js';
|
|
|
|
import { execAsync, subprocess } from 'resource:///com/github/Aylur/ags/utils.js';
|
2023-10-24 14:21:33 -04:00
|
|
|
import GUdev from 'gi://GUdev';
|
|
|
|
|
|
|
|
const UDEV_POINTERS = [
|
|
|
|
'ID_INPUT_MOUSE',
|
|
|
|
'ID_INPUT_POINTINGSTICK',
|
|
|
|
'ID_INPUT_TOUCHPAD',
|
|
|
|
'ID_INPUT_TOUCHSCREEN',
|
|
|
|
'ID_INPUT_TABLET',
|
|
|
|
];
|
2023-10-24 17:26:38 -04:00
|
|
|
const ON_RELEASE_TRIGGERS = [
|
2023-10-24 14:21:33 -04:00
|
|
|
'released',
|
|
|
|
'TOUCH_UP',
|
|
|
|
'HOLD_END',
|
|
|
|
];
|
2023-10-24 17:26:38 -04:00
|
|
|
const ON_CLICK_TRIGGERS = [
|
|
|
|
'pressed',
|
|
|
|
'TOUCH_DOWN',
|
|
|
|
];
|
2023-10-16 17:06:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Pointers extends Service {
|
2023-10-20 23:11:21 -04:00
|
|
|
static {
|
|
|
|
Service.register(this, {
|
|
|
|
'proc-started': ['boolean'],
|
|
|
|
'proc-destroyed': ['boolean'],
|
|
|
|
'device-fetched': ['boolean'],
|
|
|
|
'new-line': ['string'],
|
2023-10-24 17:26:38 -04:00
|
|
|
'released': ['string'],
|
|
|
|
'clicked': ['string'],
|
2023-10-20 23:11:21 -04:00
|
|
|
});
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
proc = undefined;
|
|
|
|
output = '';
|
2023-10-24 14:21:33 -04:00
|
|
|
devices = [];
|
|
|
|
udevClient = GUdev.Client.new(['input']);
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-24 17:26:38 -04:00
|
|
|
get process() { return this.proc; }
|
2023-10-20 23:11:21 -04:00
|
|
|
get lastLine() { return this.output; }
|
|
|
|
get pointers() { return this.devices; }
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
2023-10-24 14:21:33 -04:00
|
|
|
this.initUdevConnection();
|
2023-10-24 17:26:38 -04:00
|
|
|
this.initAppConnection();
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-24 14:21:33 -04:00
|
|
|
// FIXME: logitech mouse screws everything up on disconnect
|
2023-10-24 17:26:38 -04:00
|
|
|
initUdevConnection() {
|
|
|
|
this.getDevices();
|
|
|
|
this.udevClient.connect('uevent', (_, action) => {
|
|
|
|
if (action === 'add' || action === 'remove') {
|
|
|
|
this.getDevices();
|
|
|
|
if (this.proc) {
|
|
|
|
this.killProc();
|
|
|
|
this.startProc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-24 14:21:33 -04:00
|
|
|
getDevices() {
|
|
|
|
this.devices = [];
|
|
|
|
this.udevClient.query_by_subsystem('input').forEach(dev => {
|
|
|
|
const isPointer = UDEV_POINTERS.some(p => dev.has_property(p));
|
|
|
|
if (isPointer) {
|
|
|
|
const hasEventFile = dev.has_property('DEVNAME') &&
|
2023-10-24 17:26:38 -04:00
|
|
|
dev.get_property('DEVNAME').includes('event');
|
2023-10-24 14:21:33 -04:00
|
|
|
if (hasEventFile)
|
|
|
|
this.devices.push(dev.get_property('DEVNAME'));
|
|
|
|
}
|
|
|
|
});
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-24 14:21:33 -04:00
|
|
|
this.emit('device-fetched', true);
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
startProc() {
|
|
|
|
if (this.proc)
|
|
|
|
return;
|
2023-10-16 18:43:12 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
const args = [];
|
|
|
|
this.devices.forEach(dev => {
|
2023-10-24 14:21:33 -04:00
|
|
|
args.push('--device');
|
|
|
|
args.push(dev);
|
2023-10-20 23:11:21 -04:00
|
|
|
});
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
this.proc = subprocess(
|
2023-10-20 23:11:21 -04:00
|
|
|
['libinput', 'debug-events', ...args],
|
|
|
|
output => {
|
2023-10-24 17:26:38 -04:00
|
|
|
if (output.includes('cancelled'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ON_RELEASE_TRIGGERS.some(p => output.includes(p))) {
|
|
|
|
this.output = output;
|
|
|
|
this.detectClickedOutside('released');
|
|
|
|
this.emit('released', output);
|
|
|
|
this.emit('new-line', output);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ON_CLICK_TRIGGERS.some(p => output.includes(p))) {
|
|
|
|
this.output = output;
|
|
|
|
this.detectClickedOutside('clicked');
|
|
|
|
this.emit('clicked', output);
|
|
|
|
this.emit('new-line', output);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
err => logError(err),
|
|
|
|
);
|
|
|
|
this.emit('proc-started', true);
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
killProc() {
|
|
|
|
if (this.proc) {
|
|
|
|
this.proc.force_exit();
|
|
|
|
this.proc = undefined;
|
|
|
|
this.emit('proc-destroyed', true);
|
2023-10-16 17:06:19 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
|
|
|
|
initAppConnection() {
|
|
|
|
App.connect('window-toggled', () => {
|
|
|
|
const anyVisibleAndClosable = Array.from(App.windows).some(w => {
|
|
|
|
const closable = (w[1].closeOnUnfocus &&
|
|
|
|
w[1].closeOnUnfocus !== 'none');
|
|
|
|
|
|
|
|
return w[1].visible && closable;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (anyVisibleAndClosable)
|
|
|
|
this.startProc();
|
|
|
|
|
|
|
|
else
|
|
|
|
this.killProc();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
detectClickedOutside(clickStage) {
|
|
|
|
const toClose = Array.from(App.windows).some(w => {
|
|
|
|
const closable = (w[1].closeOnUnfocus &&
|
|
|
|
w[1].closeOnUnfocus === clickStage);
|
|
|
|
|
|
|
|
return w[1].visible && closable;
|
|
|
|
});
|
|
|
|
if (!toClose)
|
|
|
|
return;
|
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
execAsync('hyprctl layers -j').then(layers => {
|
2023-10-24 17:26:38 -04:00
|
|
|
layers = JSON.parse(layers);
|
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
execAsync('hyprctl cursorpos -j').then(pos => {
|
2023-10-24 17:26:38 -04:00
|
|
|
pos = JSON.parse(pos);
|
|
|
|
|
|
|
|
Object.values(layers).forEach(key => {
|
|
|
|
const bar = key['levels']['3']
|
|
|
|
.find(n => n.namespace === 'bar');
|
|
|
|
|
|
|
|
const widgets = key['levels']['3'].filter(n => {
|
|
|
|
const window = App.getWindow(n.namespace);
|
|
|
|
return window.closeOnUnfocus &&
|
|
|
|
window.closeOnUnfocus === clickStage;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (pos.x > bar.x && pos.x < bar.x + bar.w &&
|
|
|
|
pos.y > bar.y && pos.y < bar.y + bar.h) {
|
|
|
|
|
|
|
|
// don't handle clicks when on bar
|
|
|
|
// TODO: make this configurable
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
widgets.forEach(w => {
|
|
|
|
if (!(pos.x > w.x && pos.x < w.x + w.w &&
|
|
|
|
pos.y > w.y && pos.y < w.y + w.h))
|
|
|
|
App.closeWindow(w.namespace);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(print);
|
|
|
|
}).catch(print);
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const pointersService = new Pointers();
|
|
|
|
export default pointersService;
|