mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
pretty
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { WindowGeometry } from "../types";
|
||||
import { WindowGeometry } from '../types';
|
||||
|
||||
export type GeometryChangeCallback = (geometry: WindowGeometry) => void;
|
||||
export type WindowFoundCallback = (geometry: WindowGeometry) => void;
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import * as net from "net";
|
||||
import { execSync } from "child_process";
|
||||
import { BaseWindowTracker } from "./base-tracker";
|
||||
import { createLogger } from "../logger";
|
||||
import * as net from 'net';
|
||||
import { execSync } from 'child_process';
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
import { createLogger } from '../logger';
|
||||
|
||||
const log = createLogger("tracker").child("hyprland");
|
||||
const log = createLogger('tracker').child('hyprland');
|
||||
|
||||
interface HyprlandClient {
|
||||
class: string;
|
||||
@@ -60,39 +60,39 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
|
||||
private connectEventSocket(): void {
|
||||
const hyprlandSig = process.env.HYPRLAND_INSTANCE_SIGNATURE;
|
||||
if (!hyprlandSig) {
|
||||
log.info("HYPRLAND_INSTANCE_SIGNATURE not set, skipping event socket");
|
||||
log.info('HYPRLAND_INSTANCE_SIGNATURE not set, skipping event socket');
|
||||
return;
|
||||
}
|
||||
|
||||
const xdgRuntime = process.env.XDG_RUNTIME_DIR || "/tmp";
|
||||
const xdgRuntime = process.env.XDG_RUNTIME_DIR || '/tmp';
|
||||
const socketPath = `${xdgRuntime}/hypr/${hyprlandSig}/.socket2.sock`;
|
||||
this.eventSocket = new net.Socket();
|
||||
|
||||
this.eventSocket.on("connect", () => {
|
||||
log.info("Connected to Hyprland event socket");
|
||||
this.eventSocket.on('connect', () => {
|
||||
log.info('Connected to Hyprland event socket');
|
||||
});
|
||||
|
||||
this.eventSocket.on("data", (data: Buffer) => {
|
||||
const events = data.toString().split("\n");
|
||||
this.eventSocket.on('data', (data: Buffer) => {
|
||||
const events = data.toString().split('\n');
|
||||
for (const event of events) {
|
||||
if (
|
||||
event.includes("movewindow") ||
|
||||
event.includes("windowtitle") ||
|
||||
event.includes("openwindow") ||
|
||||
event.includes("closewindow") ||
|
||||
event.includes("fullscreen")
|
||||
event.includes('movewindow') ||
|
||||
event.includes('windowtitle') ||
|
||||
event.includes('openwindow') ||
|
||||
event.includes('closewindow') ||
|
||||
event.includes('fullscreen')
|
||||
) {
|
||||
this.pollGeometry();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.eventSocket.on("error", (err: Error) => {
|
||||
log.error("Hyprland event socket error:", err.message);
|
||||
this.eventSocket.on('error', (err: Error) => {
|
||||
log.error('Hyprland event socket error:', err.message);
|
||||
});
|
||||
|
||||
this.eventSocket.on("close", () => {
|
||||
log.info("Hyprland event socket closed");
|
||||
this.eventSocket.on('close', () => {
|
||||
log.info('Hyprland event socket closed');
|
||||
});
|
||||
|
||||
this.eventSocket.connect(socketPath);
|
||||
@@ -100,7 +100,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
|
||||
|
||||
private pollGeometry(): void {
|
||||
try {
|
||||
const output = execSync("hyprctl clients -j", { encoding: "utf-8" });
|
||||
const output = execSync('hyprctl clients -j', { encoding: 'utf-8' });
|
||||
const clients: HyprlandClient[] = JSON.parse(output);
|
||||
const mpvWindow = this.findTargetWindow(clients);
|
||||
|
||||
@@ -120,7 +120,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
|
||||
}
|
||||
|
||||
private findTargetWindow(clients: HyprlandClient[]): HyprlandClient | null {
|
||||
const mpvWindows = clients.filter((client) => client.class === "mpv");
|
||||
const mpvWindows = clients.filter((client) => client.class === 'mpv');
|
||||
if (!this.targetMpvSocketPath) {
|
||||
return mpvWindows[0] || null;
|
||||
}
|
||||
@@ -136,9 +136,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
|
||||
}
|
||||
|
||||
if (
|
||||
commandLine.includes(
|
||||
`--input-ipc-server=${this.targetMpvSocketPath}`,
|
||||
) ||
|
||||
commandLine.includes(`--input-ipc-server=${this.targetMpvSocketPath}`) ||
|
||||
commandLine.includes(`--input-ipc-server ${this.targetMpvSocketPath}`)
|
||||
) {
|
||||
return mpvWindow;
|
||||
@@ -150,7 +148,7 @@ export class HyprlandWindowTracker extends BaseWindowTracker {
|
||||
|
||||
private getWindowCommandLine(pid: number): string | null {
|
||||
const commandLine = execSync(`ps -p ${pid} -o args=`, {
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
return commandLine || null;
|
||||
}
|
||||
|
||||
@@ -16,32 +16,32 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { BaseWindowTracker } from "./base-tracker";
|
||||
import { HyprlandWindowTracker } from "./hyprland-tracker";
|
||||
import { SwayWindowTracker } from "./sway-tracker";
|
||||
import { X11WindowTracker } from "./x11-tracker";
|
||||
import { MacOSWindowTracker } from "./macos-tracker";
|
||||
import { createLogger } from "../logger";
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
import { HyprlandWindowTracker } from './hyprland-tracker';
|
||||
import { SwayWindowTracker } from './sway-tracker';
|
||||
import { X11WindowTracker } from './x11-tracker';
|
||||
import { MacOSWindowTracker } from './macos-tracker';
|
||||
import { createLogger } from '../logger';
|
||||
|
||||
const log = createLogger("tracker");
|
||||
const log = createLogger('tracker');
|
||||
|
||||
export type Compositor = "hyprland" | "sway" | "x11" | "macos" | null;
|
||||
export type Backend = "auto" | Exclude<Compositor, null>;
|
||||
export type Compositor = 'hyprland' | 'sway' | 'x11' | 'macos' | null;
|
||||
export type Backend = 'auto' | Exclude<Compositor, null>;
|
||||
|
||||
export function detectCompositor(): Compositor {
|
||||
if (process.platform === "darwin") return "macos";
|
||||
if (process.env.HYPRLAND_INSTANCE_SIGNATURE) return "hyprland";
|
||||
if (process.env.SWAYSOCK) return "sway";
|
||||
if (process.platform === "linux") return "x11";
|
||||
if (process.platform === 'darwin') return 'macos';
|
||||
if (process.env.HYPRLAND_INSTANCE_SIGNATURE) return 'hyprland';
|
||||
if (process.env.SWAYSOCK) return 'sway';
|
||||
if (process.platform === 'linux') return 'x11';
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizeCompositor(value: string): Compositor | null {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === "hyprland") return "hyprland";
|
||||
if (normalized === "sway") return "sway";
|
||||
if (normalized === "x11") return "x11";
|
||||
if (normalized === "macos") return "macos";
|
||||
if (normalized === 'hyprland') return 'hyprland';
|
||||
if (normalized === 'sway') return 'sway';
|
||||
if (normalized === 'x11') return 'x11';
|
||||
if (normalized === 'macos') return 'macos';
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -51,31 +51,27 @@ export function createWindowTracker(
|
||||
): BaseWindowTracker | null {
|
||||
let compositor = detectCompositor();
|
||||
|
||||
if (override && override !== "auto") {
|
||||
if (override && override !== 'auto') {
|
||||
const normalized = normalizeCompositor(override);
|
||||
if (normalized) {
|
||||
compositor = normalized;
|
||||
} else {
|
||||
log.warn(
|
||||
`Unsupported backend override "${override}", falling back to auto.`,
|
||||
);
|
||||
log.warn(`Unsupported backend override "${override}", falling back to auto.`);
|
||||
}
|
||||
}
|
||||
log.info(`Detected compositor: ${compositor || "none"}`);
|
||||
log.info(`Detected compositor: ${compositor || 'none'}`);
|
||||
|
||||
switch (compositor) {
|
||||
case "hyprland":
|
||||
return new HyprlandWindowTracker(
|
||||
targetMpvSocketPath?.trim() || undefined,
|
||||
);
|
||||
case "sway":
|
||||
case 'hyprland':
|
||||
return new HyprlandWindowTracker(targetMpvSocketPath?.trim() || undefined);
|
||||
case 'sway':
|
||||
return new SwayWindowTracker(targetMpvSocketPath?.trim() || undefined);
|
||||
case "x11":
|
||||
case 'x11':
|
||||
return new X11WindowTracker(targetMpvSocketPath?.trim() || undefined);
|
||||
case "macos":
|
||||
case 'macos':
|
||||
return new MacOSWindowTracker(targetMpvSocketPath?.trim() || undefined);
|
||||
default:
|
||||
log.warn("No supported compositor detected. Window tracking disabled.");
|
||||
log.warn('No supported compositor detected. Window tracking disabled.');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,20 +16,20 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { execFile } from "child_process";
|
||||
import * as path from "path";
|
||||
import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import { BaseWindowTracker } from "./base-tracker";
|
||||
import { createLogger } from "../logger";
|
||||
import { execFile } from 'child_process';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
import { createLogger } from '../logger';
|
||||
|
||||
const log = createLogger("tracker").child("macos");
|
||||
const log = createLogger('tracker').child('macos');
|
||||
|
||||
export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
private pollInterval: ReturnType<typeof setInterval> | null = null;
|
||||
private pollInFlight = false;
|
||||
private helperPath: string | null = null;
|
||||
private helperType: "binary" | "swift" | null = null;
|
||||
private helperType: 'binary' | 'swift' | null = null;
|
||||
private lastExecErrorFingerprint: string | null = null;
|
||||
private lastExecErrorLoggedAtMs = 0;
|
||||
private readonly targetMpvSocketPath: string | null;
|
||||
@@ -40,19 +40,14 @@ export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
this.detectHelper();
|
||||
}
|
||||
|
||||
private materializeAsarHelper(
|
||||
sourcePath: string,
|
||||
helperType: "binary" | "swift",
|
||||
): string | null {
|
||||
if (!sourcePath.includes(".asar")) {
|
||||
private materializeAsarHelper(sourcePath: string, helperType: 'binary' | 'swift'): string | null {
|
||||
if (!sourcePath.includes('.asar')) {
|
||||
return sourcePath;
|
||||
}
|
||||
|
||||
const fileName =
|
||||
helperType === "binary"
|
||||
? "get-mpv-window-macos"
|
||||
: "get-mpv-window-macos.swift";
|
||||
const targetDir = path.join(os.tmpdir(), "subminer", "helpers");
|
||||
helperType === 'binary' ? 'get-mpv-window-macos' : 'get-mpv-window-macos.swift';
|
||||
const targetDir = path.join(os.tmpdir(), 'subminer', 'helpers');
|
||||
const targetPath = path.join(targetDir, fileName);
|
||||
|
||||
try {
|
||||
@@ -67,10 +62,7 @@ export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
}
|
||||
}
|
||||
|
||||
private tryUseHelper(
|
||||
candidatePath: string,
|
||||
helperType: "binary" | "swift",
|
||||
): boolean {
|
||||
private tryUseHelper(candidatePath: string, helperType: 'binary' | 'swift'): boolean {
|
||||
if (!fs.existsSync(candidatePath)) {
|
||||
return false;
|
||||
}
|
||||
@@ -91,63 +83,46 @@ export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
|
||||
// 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")) {
|
||||
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) {
|
||||
const resourcesBinaryPath = path.join(
|
||||
resourcesPath,
|
||||
"scripts",
|
||||
"get-mpv-window-macos",
|
||||
);
|
||||
if (this.tryUseHelper(resourcesBinaryPath, "binary")) {
|
||||
const resourcesBinaryPath = path.join(resourcesPath, 'scripts', 'get-mpv-window-macos');
|
||||
if (this.tryUseHelper(resourcesBinaryPath, 'binary')) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Dist binary path (development / unpacked installs).
|
||||
const distBinaryPath = path.join(
|
||||
__dirname,
|
||||
"..",
|
||||
"..",
|
||||
"scripts",
|
||||
"get-mpv-window-macos",
|
||||
);
|
||||
if (this.tryUseHelper(distBinaryPath, "binary")) {
|
||||
const distBinaryPath = path.join(__dirname, '..', '..', 'scripts', 'get-mpv-window-macos');
|
||||
if (this.tryUseHelper(distBinaryPath, 'binary')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fall back to Swift script for development or if binary filtering is not
|
||||
// supported in the current environment.
|
||||
if (this.tryUseHelper(swiftPath, "swift")) {
|
||||
if (this.tryUseHelper(swiftPath, 'swift')) {
|
||||
return;
|
||||
}
|
||||
|
||||
log.warn("macOS window tracking helper not found");
|
||||
log.warn('macOS window tracking helper not found');
|
||||
}
|
||||
|
||||
private maybeLogExecError(err: Error, stderr: string): void {
|
||||
const now = Date.now();
|
||||
const fingerprint = `${err.message}|${stderr.trim()}`;
|
||||
const shouldLog =
|
||||
this.lastExecErrorFingerprint !== fingerprint ||
|
||||
now - this.lastExecErrorLoggedAtMs >= 5000;
|
||||
this.lastExecErrorFingerprint !== fingerprint || now - this.lastExecErrorLoggedAtMs >= 5000;
|
||||
if (!shouldLog) {
|
||||
return;
|
||||
}
|
||||
this.lastExecErrorFingerprint = fingerprint;
|
||||
this.lastExecErrorLoggedAtMs = now;
|
||||
log.warn("macOS helper execution failed", {
|
||||
log.warn('macOS helper execution failed', {
|
||||
helperPath: this.helperPath,
|
||||
helperType: this.helperType,
|
||||
error: err.message,
|
||||
@@ -176,8 +151,8 @@ export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
|
||||
// Use Core Graphics API via Swift helper for reliable window detection
|
||||
// This works with both bundled and unbundled mpv installations
|
||||
const command = this.helperType === "binary" ? this.helperPath : "swift";
|
||||
const args = this.helperType === "binary" ? [] : [this.helperPath];
|
||||
const command = this.helperType === 'binary' ? this.helperPath : 'swift';
|
||||
const args = this.helperType === 'binary' ? [] : [this.helperPath];
|
||||
if (this.targetMpvSocketPath) {
|
||||
args.push(this.targetMpvSocketPath);
|
||||
}
|
||||
@@ -186,21 +161,21 @@ export class MacOSWindowTracker extends BaseWindowTracker {
|
||||
command,
|
||||
args,
|
||||
{
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
timeout: 1000,
|
||||
maxBuffer: 1024 * 1024,
|
||||
},
|
||||
(err, stdout, stderr) => {
|
||||
if (err) {
|
||||
this.maybeLogExecError(err, stderr || "");
|
||||
this.maybeLogExecError(err, stderr || '');
|
||||
this.updateGeometry(null);
|
||||
this.pollInFlight = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const result = (stdout || "").trim();
|
||||
if (result && result !== "not-found") {
|
||||
const parts = result.split(",");
|
||||
const result = (stdout || '').trim();
|
||||
if (result && result !== 'not-found') {
|
||||
const parts = result.split(',');
|
||||
if (parts.length === 4) {
|
||||
const x = parseInt(parts[0], 10);
|
||||
const y = parseInt(parts[1], 10);
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { execSync } from "child_process";
|
||||
import { BaseWindowTracker } from "./base-tracker";
|
||||
import { execSync } from 'child_process';
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
|
||||
interface SwayRect {
|
||||
x: number;
|
||||
@@ -58,7 +58,7 @@ export class SwayWindowTracker extends BaseWindowTracker {
|
||||
|
||||
private collectMpvWindows(node: SwayNode): SwayNode[] {
|
||||
const windows: SwayNode[] = [];
|
||||
if (node.app_id === "mpv" || node.window_properties?.class === "mpv") {
|
||||
if (node.app_id === 'mpv' || node.window_properties?.class === 'mpv') {
|
||||
windows.push(node);
|
||||
}
|
||||
|
||||
@@ -83,10 +83,7 @@ export class SwayWindowTracker extends BaseWindowTracker {
|
||||
return windows[0] || null;
|
||||
}
|
||||
|
||||
return (
|
||||
windows.find((candidate) => this.isWindowForTargetSocket(candidate)) ||
|
||||
null
|
||||
);
|
||||
return windows.find((candidate) => this.isWindowForTargetSocket(candidate)) || null;
|
||||
}
|
||||
|
||||
private isWindowForTargetSocket(node: SwayNode): boolean {
|
||||
@@ -107,14 +104,14 @@ export class SwayWindowTracker extends BaseWindowTracker {
|
||||
|
||||
private getWindowCommandLine(pid: number): string | null {
|
||||
const commandLine = execSync(`ps -p ${pid} -o args=`, {
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
return commandLine || null;
|
||||
}
|
||||
|
||||
private pollGeometry(): void {
|
||||
try {
|
||||
const output = execSync("swaymsg -t get_tree", { encoding: "utf-8" });
|
||||
const output = execSync('swaymsg -t get_tree', { encoding: 'utf-8' });
|
||||
const tree: SwayNode = JSON.parse(output);
|
||||
const mpvWindow = this.findTargetSocketWindow(tree);
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { execSync } from "child_process";
|
||||
import { BaseWindowTracker } from "./base-tracker";
|
||||
import { execSync } from 'child_process';
|
||||
import { BaseWindowTracker } from './base-tracker';
|
||||
|
||||
export class X11WindowTracker extends BaseWindowTracker {
|
||||
private pollInterval: ReturnType<typeof setInterval> | null = null;
|
||||
@@ -42,8 +42,8 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
|
||||
private pollGeometry(): void {
|
||||
try {
|
||||
const windowIds = execSync("xdotool search --class mpv", {
|
||||
encoding: "utf-8",
|
||||
const windowIds = execSync('xdotool search --class mpv', {
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
|
||||
if (!windowIds) {
|
||||
@@ -64,7 +64,7 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
}
|
||||
|
||||
const winInfo = execSync(`xwininfo -id ${windowId}`, {
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
|
||||
const xMatch = winInfo.match(/Absolute upper-left X:\s*(\d+)/);
|
||||
@@ -120,7 +120,7 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
|
||||
private getWindowPid(windowId: string): number | null {
|
||||
const windowPid = execSync(`xprop -id ${windowId} _NET_WM_PID`, {
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
const pidMatch = windowPid.match(/= (\d+)/);
|
||||
if (!pidMatch) {
|
||||
@@ -133,7 +133,7 @@ export class X11WindowTracker extends BaseWindowTracker {
|
||||
|
||||
private getWindowCommandLine(pid: number): string | null {
|
||||
const commandLine = execSync(`ps -p ${pid} -o args=`, {
|
||||
encoding: "utf-8",
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
return commandLine || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user