mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 18:22:42 -08:00
Fix mpv tlang and profile parsing
This commit is contained in:
54
src/window-trackers/x11-tracker.test.ts
Normal file
54
src/window-trackers/x11-tracker.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { parseX11WindowGeometry, parseX11WindowPid, X11WindowTracker } from './x11-tracker';
|
||||
|
||||
test('parseX11WindowGeometry parses xwininfo output', () => {
|
||||
const geometry = parseX11WindowGeometry(`
|
||||
Absolute upper-left X: 120
|
||||
Absolute upper-left Y: 240
|
||||
Width: 1280
|
||||
Height: 720
|
||||
`);
|
||||
assert.deepEqual(geometry, {
|
||||
x: 120,
|
||||
y: 240,
|
||||
width: 1280,
|
||||
height: 720,
|
||||
});
|
||||
});
|
||||
|
||||
test('parseX11WindowPid parses xprop output', () => {
|
||||
assert.equal(parseX11WindowPid('_NET_WM_PID(CARDINAL) = 4242'), 4242);
|
||||
assert.equal(parseX11WindowPid('_NET_WM_PID(CARDINAL) = not-a-number'), null);
|
||||
});
|
||||
|
||||
test('X11WindowTracker skips overlapping polls while one command is in flight', async () => {
|
||||
let commandCalls = 0;
|
||||
let release: (() => void) | undefined;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
|
||||
const tracker = new X11WindowTracker(undefined, async (command) => {
|
||||
commandCalls += 1;
|
||||
if (command === 'xdotool') {
|
||||
await gate;
|
||||
return '123';
|
||||
}
|
||||
if (command === 'xwininfo') {
|
||||
return `Absolute upper-left X: 0
|
||||
Absolute upper-left Y: 0
|
||||
Width: 640
|
||||
Height: 360`;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
(tracker as unknown as { pollGeometry: () => void }).pollGeometry();
|
||||
(tracker as unknown as { pollGeometry: () => void }).pollGeometry();
|
||||
assert.equal(commandCalls, 1);
|
||||
|
||||
assert.ok(release);
|
||||
release();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
});
|
||||
@@ -16,20 +16,69 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import { execFile } from 'child_process';
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
|
||||
type CommandRunner = (command: string, args: string[]) => Promise<string>;
|
||||
|
||||
function execFileUtf8(command: string, args: string[]): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(command, args, { encoding: 'utf-8' }, (error, stdout) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function parseX11WindowGeometry(winInfo: string): {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
} | null {
|
||||
const xMatch = winInfo.match(/Absolute upper-left X:\s*(\d+)/);
|
||||
const yMatch = winInfo.match(/Absolute upper-left Y:\s*(\d+)/);
|
||||
const widthMatch = winInfo.match(/Width:\s*(\d+)/);
|
||||
const heightMatch = winInfo.match(/Height:\s*(\d+)/);
|
||||
if (!xMatch || !yMatch || !widthMatch || !heightMatch) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
x: parseInt(xMatch[1], 10),
|
||||
y: parseInt(yMatch[1], 10),
|
||||
width: parseInt(widthMatch[1], 10),
|
||||
height: parseInt(heightMatch[1], 10),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseX11WindowPid(raw: string): number | null {
|
||||
const pidMatch = raw.match(/= (\d+)/);
|
||||
if (!pidMatch) {
|
||||
return null;
|
||||
}
|
||||
const pid = Number.parseInt(pidMatch[1], 10);
|
||||
return Number.isInteger(pid) ? pid : null;
|
||||
}
|
||||
|
||||
export class X11WindowTracker extends BaseWindowTracker {
|
||||
private pollInterval: ReturnType<typeof setInterval> | null = null;
|
||||
private readonly targetMpvSocketPath: string | null;
|
||||
private readonly runCommand: CommandRunner;
|
||||
private pollInFlight = false;
|
||||
private currentPollIntervalMs = 750;
|
||||
private readonly stablePollIntervalMs = 250;
|
||||
|
||||
constructor(targetMpvSocketPath?: string) {
|
||||
constructor(targetMpvSocketPath?: string, runCommand: CommandRunner = execFileUtf8) {
|
||||
super();
|
||||
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
|
||||
this.runCommand = runCommand;
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
|
||||
this.resetPollInterval(this.currentPollIntervalMs);
|
||||
this.pollGeometry();
|
||||
}
|
||||
|
||||
@@ -40,60 +89,69 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
}
|
||||
}
|
||||
|
||||
private resetPollInterval(intervalMs: number): void {
|
||||
if (this.pollInterval) {
|
||||
clearInterval(this.pollInterval);
|
||||
this.pollInterval = null;
|
||||
}
|
||||
this.pollInterval = setInterval(() => this.pollGeometry(), intervalMs);
|
||||
}
|
||||
|
||||
private pollGeometry(): void {
|
||||
try {
|
||||
const windowIds = execSync('xdotool search --class mpv', {
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
|
||||
if (!windowIds) {
|
||||
if (this.pollInFlight) {
|
||||
return;
|
||||
}
|
||||
this.pollInFlight = true;
|
||||
void this.pollGeometryAsync()
|
||||
.catch(() => {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const windowIdList = windowIds.split(/\s+/).filter(Boolean);
|
||||
if (windowIdList.length === 0) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const windowId = this.findTargetWindowId(windowIdList);
|
||||
if (!windowId) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const winInfo = execSync(`xwininfo -id ${windowId}`, {
|
||||
encoding: 'utf-8',
|
||||
})
|
||||
.finally(() => {
|
||||
this.pollInFlight = false;
|
||||
});
|
||||
}
|
||||
|
||||
const xMatch = winInfo.match(/Absolute upper-left X:\s*(\d+)/);
|
||||
const yMatch = winInfo.match(/Absolute upper-left Y:\s*(\d+)/);
|
||||
const widthMatch = winInfo.match(/Width:\s*(\d+)/);
|
||||
const heightMatch = winInfo.match(/Height:\s*(\d+)/);
|
||||
|
||||
if (xMatch && yMatch && widthMatch && heightMatch) {
|
||||
this.updateGeometry({
|
||||
x: parseInt(xMatch[1], 10),
|
||||
y: parseInt(yMatch[1], 10),
|
||||
width: parseInt(widthMatch[1], 10),
|
||||
height: parseInt(heightMatch[1], 10),
|
||||
});
|
||||
} else {
|
||||
this.updateGeometry(null);
|
||||
}
|
||||
} catch (err) {
|
||||
private async pollGeometryAsync(): Promise<void> {
|
||||
const windowIdsOutput = await this.runCommand('xdotool', ['search', '--class', 'mpv']);
|
||||
const windowIds = windowIdsOutput.trim();
|
||||
if (!windowIds) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const windowIdList = windowIds.split(/\s+/).filter(Boolean);
|
||||
if (windowIdList.length === 0) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const windowId = await this.findTargetWindowId(windowIdList);
|
||||
if (!windowId) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const winInfo = await this.runCommand('xwininfo', ['-id', windowId]);
|
||||
const geometry = parseX11WindowGeometry(winInfo);
|
||||
if (!geometry) {
|
||||
this.updateGeometry(null);
|
||||
return;
|
||||
}
|
||||
|
||||
this.updateGeometry(geometry);
|
||||
if (this.pollInterval && this.currentPollIntervalMs !== this.stablePollIntervalMs) {
|
||||
this.currentPollIntervalMs = this.stablePollIntervalMs;
|
||||
this.resetPollInterval(this.currentPollIntervalMs);
|
||||
}
|
||||
}
|
||||
|
||||
private findTargetWindowId(windowIds: string[]): string | null {
|
||||
private async findTargetWindowId(windowIds: string[]): Promise<string | null> {
|
||||
if (!this.targetMpvSocketPath) {
|
||||
return windowIds[0] ?? null;
|
||||
}
|
||||
|
||||
for (const windowId of windowIds) {
|
||||
if (this.isWindowForTargetSocket(windowId)) {
|
||||
if (await this.isWindowForTargetSocket(windowId)) {
|
||||
return windowId;
|
||||
}
|
||||
}
|
||||
@@ -101,13 +159,13 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
return null;
|
||||
}
|
||||
|
||||
private isWindowForTargetSocket(windowId: string): boolean {
|
||||
const pid = this.getWindowPid(windowId);
|
||||
private async isWindowForTargetSocket(windowId: string): Promise<boolean> {
|
||||
const pid = await this.getWindowPid(windowId);
|
||||
if (pid === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const commandLine = this.getWindowCommandLine(pid);
|
||||
const commandLine = await this.getWindowCommandLine(pid);
|
||||
if (!commandLine) {
|
||||
return false;
|
||||
}
|
||||
@@ -118,23 +176,24 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
);
|
||||
}
|
||||
|
||||
private getWindowPid(windowId: string): number | null {
|
||||
const windowPid = execSync(`xprop -id ${windowId} _NET_WM_PID`, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
const pidMatch = windowPid.match(/= (\d+)/);
|
||||
if (!pidMatch) {
|
||||
private async getWindowPid(windowId: string): Promise<number | null> {
|
||||
let windowPid: string;
|
||||
try {
|
||||
windowPid = await this.runCommand('xprop', ['-id', windowId, '_NET_WM_PID']);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pid = Number.parseInt(pidMatch[1], 10);
|
||||
return Number.isInteger(pid) ? pid : null;
|
||||
return parseX11WindowPid(windowPid);
|
||||
}
|
||||
|
||||
private getWindowCommandLine(pid: number): string | null {
|
||||
const commandLine = execSync(`ps -p ${pid} -o args=`, {
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
private async getWindowCommandLine(pid: number): Promise<string | null> {
|
||||
let raw: string;
|
||||
try {
|
||||
raw = await this.runCommand('ps', ['-p', String(pid), '-o', 'args=']);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const commandLine = raw.trim();
|
||||
return commandLine || null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user