wip: woork on pointer service

This commit is contained in:
matt1432 2023-10-15 12:41:13 -04:00
parent b93b29722e
commit 844b0cfdfb

View file

@ -12,103 +12,67 @@ Libinput.instance.connect('device-init', () => {
}); });
*/ */
/*
// WIP
Utils.execAsync('hyprctl layers -j').then(layers => {
layers = JSON.parse(layers);
Utils.execAsync('hyprctl cursorpos -j').then(pos => {
pos = JSON.parse(pos);
Object.values(layers).forEach(key => {
key['levels']['3'].forEach(l => {
print(l.namespace);
if (pos.x > l.x && pos.x < l.x + l.w &&
pos.y > l.y && pos.y < l.y + l.h)
{
print('inside window');
}
else {
print('outside window');
}
});
});
}).catch(print);
}).catch(print);
*/
// TODO: use hyprctl layers to determine if clicks were on a widget
// read /dev to recalculate devices and remake subprocess
const { Service, Utils } = '../imports.js'; const { Service, Utils } = '../imports.js';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
class DebugInstance extends GObject.Object {
static {
GObject.registerClass({
Signals: {
'changed': {},
'closed': {},
'new-line': {},
},
}, this);
}
devices = [];
name = '';
lastLine = '';
readOutput(stdout, stdin) { class PointersService extends Service {
stdout.read_line_async(GLib.PRIORITY_LOW, null, (stream, result) => {
try {
const [line] = stream.read_line_finish_utf8(result);
if (line !== null) {
this.lastLine = line;
this.emit('new-line');
this.readOutput(stdout, stdin);
}
} catch (e) {
logError(e);
}
});
}
getOutput(devs) {
try {
let args = [];
devs.forEach(dev => {
if (dev.Kernel) {
args.push('--device');
args.push(dev.Kernel);
}
});
const proc = Gio.Subprocess.new(['libinput', 'debug-events', ...args],
Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE);
// Get the `stdin`and `stdout` pipes, wrapping `stdout` to make it easier to
// read lines of text
const stdinStream = proc.get_stdin_pipe();
const stdoutStream = new Gio.DataInputStream({
base_stream: proc.get_stdout_pipe(),
close_base_stream: true,
});
// Start the loop
this.readOutput(stdoutStream, stdinStream);
} catch (e) {
logError(e);
}
}
constructor(name, devs) {
super();
this.devices = devs;
this.name = name;
this.getOutput(devs);
}
}
class LibinputService extends Service {
static { static {
Service.register(this, { Service.register(this, {
'device-init': ['boolean'], 'log-started': ['boolean'],
'instance-closed': ['string'], 'device-fetched': ['boolean'],
'instance-added': ['string'],
}); });
} }
debugInstances = new Map(); log;
output = "";
devices = new Map(); devices = new Map();
get devices() { return this._devices; } get log() {return this.log;}
get output() {return this.output;}
get devices() {return this.devices;}
parseOutput(output) { parseDevices() {
let lines = output.split('\n'); Utils.execAsync(['libinput', 'list-devices']).then(out => {
let lines = out.split('\n');
let device = null; let device = null;
let devices = new Map();
lines.forEach(line => { lines.forEach(line => {
let parts = line.split(':'); let parts = line.split(':');
if (parts[0] === 'Device') { if (parts[0] === 'Device') {
device = {}; device = {};
this.devices.set(parts[1].trim(), device); devices.set(parts[1].trim(), device);
} }
else if (device && parts[1]) { else if (device && parts[1]) {
let key = parts[0].trim(); let key = parts[0].trim();
@ -116,46 +80,27 @@ class LibinputService extends Service {
device[key] = value; device[key] = value;
} }
}); });
this.emit('device-init', true); this.devices = devices.filter(dev => dev.Capabilities && dev.Capabilities.includes('pointer'));
this.emit('device-fetched', true);
});
} }
constructor() { startLog() {
super(); let args = [];
this.debugInstances = new Map(); this.devices.forEach(dev => {
Utils.execAsync(['libinput', 'list-devices']) if (dev.Kernel) {
.then(out => this.parseOutput(out)) args.push('--device');
.catch(console.error); args.push(dev.Kernel);
} }
addDebugInstance(name, devs) {
if (this.debugInstances.get(name))
return;
devs = Array(devs);
if (devs.some(dev => dev.Capabilities && dev.Capabilities.includes('pointer'))) {
}
const debugInst = new DebugInstance(name, devs);
debugInst.connect('closed', () => {
this.debugInstances.delete(name);
this.emit('instance-closed', name);
this.emit('changed');
}); });
this.debugInstances.set(name, debugInst); this.log = Utils.subprocess(
this.emit('instance-added', name); ['libinput', 'debug-events', ...args],
return debugInst; (output) => {
} this.output = output;
} },
(err) => logError(err)
export default class Libinput { );
static { Service.Libinput = this; } this.emit('log-started', true);
static instance = new LibinputService();
static get devices() { return Libinput.instance.devices; }
static get debugInstances() { return Libinput.instance.debugInstances; }
static addDebugInstance(name, dev) {
return Libinput.instance.addDebugInstance(name, dev);
} }
} }