Track mpv overlays by configured socket window only

This commit is contained in:
2026-02-14 13:44:10 -08:00
parent d2ca24f1c7
commit 910cf2dca4
8 changed files with 527 additions and 268 deletions

View File

@@ -27,11 +27,18 @@ interface HyprlandClient {
class: string;
at: [number, number];
size: [number, number];
pid?: number;
}
export class HyprlandWindowTracker extends BaseWindowTracker {
private pollInterval: ReturnType<typeof setInterval> | null = null;
private eventSocket: net.Socket | null = null;
private readonly targetMpvSocketPath: string | null;
constructor(targetMpvSocketPath?: string) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
}
start(): void {
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
@@ -95,7 +102,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
try {
const output = execSync("hyprctl clients -j", { encoding: "utf-8" });
const clients: HyprlandClient[] = JSON.parse(output);
const mpvWindow = clients.find((c) => c.class === "mpv");
const mpvWindow = this.findTargetWindow(clients);
if (mpvWindow) {
this.updateGeometry({
@@ -111,4 +118,38 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
// hyprctl not available or failed - silent fail
}
}
private findTargetWindow(clients: HyprlandClient[]): HyprlandClient | null {
const mpvWindows = clients.filter((client) => client.class === "mpv");
if (!this.targetMpvSocketPath) {
return mpvWindows[0] || null;
}
for (const mpvWindow of mpvWindows) {
if (!mpvWindow.pid) {
continue;
}
const commandLine = this.getWindowCommandLine(mpvWindow.pid);
if (!commandLine) {
continue;
}
if (
commandLine.includes(`--input-ipc-server=${this.targetMpvSocketPath}`) ||
commandLine.includes(`--input-ipc-server ${this.targetMpvSocketPath}`)
) {
return mpvWindow;
}
}
return null;
}
private getWindowCommandLine(pid: number): string | null {
const commandLine = execSync(`ps -p ${pid} -o args=`, {
encoding: "utf-8",
}).trim();
return commandLine || null;
}
}

View File

@@ -47,6 +47,7 @@ function normalizeCompositor(value: string): Compositor | null {
export function createWindowTracker(
override?: string | null,
targetMpvSocketPath?: string | null,
): BaseWindowTracker | null {
let compositor = detectCompositor();
@@ -64,13 +65,21 @@ export function createWindowTracker(
switch (compositor) {
case "hyprland":
return new HyprlandWindowTracker();
return new HyprlandWindowTracker(
targetMpvSocketPath?.trim() || undefined,
);
case "sway":
return new SwayWindowTracker();
return new SwayWindowTracker(
targetMpvSocketPath?.trim() || undefined,
);
case "x11":
return new X11WindowTracker();
return new X11WindowTracker(
targetMpvSocketPath?.trim() || undefined,
);
case "macos":
return new MacOSWindowTracker();
return new MacOSWindowTracker(
targetMpvSocketPath?.trim() || undefined,
);
default:
log.warn("No supported compositor detected. Window tracking disabled.");
return null;

View File

@@ -32,9 +32,11 @@ export class MacOSWindowTracker extends BaseWindowTracker {
private helperType: "binary" | "swift" | null = null;
private lastExecErrorFingerprint: string | null = null;
private lastExecErrorLoggedAtMs = 0;
private readonly targetMpvSocketPath: string | null;
constructor() {
constructor(targetMpvSocketPath?: string) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
this.detectHelper();
}
@@ -85,6 +87,21 @@ export class MacOSWindowTracker extends BaseWindowTracker {
}
private detectHelper(): void {
const shouldFilterBySocket = this.targetMpvSocketPath !== null;
// Fall back to Swift helper first when filtering by socket path to avoid
// stale prebuilt binaries that don't support the new socket filter argument.
const swiftPath = path.join(
__dirname,
"..",
"..",
"scripts",
"get-mpv-window-macos.swift",
);
if (shouldFilterBySocket && this.tryUseHelper(swiftPath, "swift")) {
return;
}
// Prefer resources path (outside asar) in packaged apps.
const resourcesPath = process.resourcesPath;
if (resourcesPath) {
@@ -110,14 +127,8 @@ export class MacOSWindowTracker extends BaseWindowTracker {
return;
}
// Fall back to Swift script for development.
const swiftPath = path.join(
__dirname,
"..",
"..",
"scripts",
"get-mpv-window-macos.swift",
);
// Fall back to Swift script for development or if binary filtering is not
// supported in the current environment.
if (this.tryUseHelper(swiftPath, "swift")) {
return;
}
@@ -167,6 +178,9 @@ export class MacOSWindowTracker extends BaseWindowTracker {
// This works with both bundled and unbundled mpv installations
const command = this.helperType === "binary" ? this.helperPath : "swift";
const args = this.helperType === "binary" ? [] : [this.helperPath];
if (this.targetMpvSocketPath) {
args.push(this.targetMpvSocketPath);
}
execFile(
command,

View File

@@ -27,6 +27,7 @@ interface SwayRect {
}
interface SwayNode {
pid?: number;
app_id?: string;
window_properties?: { class?: string };
rect?: SwayRect;
@@ -36,6 +37,12 @@ interface SwayNode {
export class SwayWindowTracker extends BaseWindowTracker {
private pollInterval: ReturnType<typeof setInterval> | null = null;
private readonly targetMpvSocketPath: string | null;
constructor(targetMpvSocketPath?: string) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
}
start(): void {
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
@@ -49,33 +56,66 @@ export class SwayWindowTracker extends BaseWindowTracker {
}
}
private findMpvWindow(node: SwayNode): SwayNode | null {
private collectMpvWindows(node: SwayNode): SwayNode[] {
const windows: SwayNode[] = [];
if (node.app_id === "mpv" || node.window_properties?.class === "mpv") {
return node;
windows.push(node);
}
if (node.nodes) {
for (const child of node.nodes) {
const found = this.findMpvWindow(child);
if (found) return found;
windows.push(...this.collectMpvWindows(child));
}
}
if (node.floating_nodes) {
for (const child of node.floating_nodes) {
const found = this.findMpvWindow(child);
if (found) return found;
windows.push(...this.collectMpvWindows(child));
}
}
return null;
return windows;
}
private findTargetSocketWindow(node: SwayNode): SwayNode | null {
const windows = this.collectMpvWindows(node);
if (!this.targetMpvSocketPath) {
return windows[0] || null;
}
return windows.find((candidate) =>
this.isWindowForTargetSocket(candidate),
) || null;
}
private isWindowForTargetSocket(node: SwayNode): boolean {
if (!node.pid) {
return false;
}
const commandLine = this.getWindowCommandLine(node.pid);
if (!commandLine) {
return false;
}
return (
commandLine.includes(`--input-ipc-server=${this.targetMpvSocketPath}`) ||
commandLine.includes(`--input-ipc-server ${this.targetMpvSocketPath}`)
);
}
private getWindowCommandLine(pid: number): string | null {
const commandLine = execSync(`ps -p ${pid} -o args=`, {
encoding: "utf-8",
}).trim();
return commandLine || null;
}
private pollGeometry(): void {
try {
const output = execSync("swaymsg -t get_tree", { encoding: "utf-8" });
const tree: SwayNode = JSON.parse(output);
const mpvWindow = this.findMpvWindow(tree);
const mpvWindow = this.findTargetSocketWindow(tree);
if (mpvWindow && mpvWindow.rect) {
this.updateGeometry({

View File

@@ -21,6 +21,12 @@ import { BaseWindowTracker } from "./base-tracker";
export class X11WindowTracker extends BaseWindowTracker {
private pollInterval: ReturnType<typeof setInterval> | null = null;
private readonly targetMpvSocketPath: string | null;
constructor(targetMpvSocketPath?: string) {
super();
this.targetMpvSocketPath = targetMpvSocketPath?.trim() || null;
}
start(): void {
this.pollInterval = setInterval(() => this.pollGeometry(), 250);
@@ -45,7 +51,17 @@ export class X11WindowTracker extends BaseWindowTracker {
return;
}
const windowId = windowIds.split("\n")[0];
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",
@@ -70,4 +86,55 @@ export class X11WindowTracker extends BaseWindowTracker {
this.updateGeometry(null);
}
}
private findTargetWindowId(windowIds: string[]): string | null {
if (!this.targetMpvSocketPath) {
return windowIds[0] ?? null;
}
for (const windowId of windowIds) {
if (this.isWindowForTargetSocket(windowId)) {
return windowId;
}
}
return null;
}
private isWindowForTargetSocket(windowId: string): boolean {
const pid = this.getWindowPid(windowId);
if (pid === null) {
return false;
}
const commandLine = this.getWindowCommandLine(pid);
if (!commandLine) {
return false;
}
return (
commandLine.includes(`--input-ipc-server=${this.targetMpvSocketPath}`) ||
commandLine.includes(`--input-ipc-server ${this.targetMpvSocketPath}`)
);
}
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) {
return null;
}
const pid = Number.parseInt(pidMatch[1], 10);
return Number.isInteger(pid) ? pid : null;
}
private getWindowCommandLine(pid: number): string | null {
const commandLine = execSync(`ps -p ${pid} -o args=`, {
encoding: "utf-8",
}).trim();
return commandLine || null;
}
}