<?phpnamespace App\Native;use App\Media\Process;/** * The window of another program in front (2001, Form4.Timer1: GetForegroundWindow + GetWindowRect): the compact bar * sits on its title bar, at the right before its buttons, and follows it. A helper watches it and writes one line each * time it changes: "x y w h titleHeight" in screen pixels (the client area's top-left and size, the title bar's height), * or "-" (none / ours / a desktop). Windows: user32 through PowerShell; Linux X11: xprop + xwininfo. Elsewhere (Wayland, * macOS): nothing, the bar stays where it is put. */final class Foreground{ private ?Process $process = null; /** @var array{0: int, 1: int, 2: int, 3: int, 4: int}|null */ private ?array $rect = null; public function __construct(string $logDir) { $pid = (int)getmypid(); if (PHP_OS_FAMILY === 'Windows') { $this->process = Process::start(['powershell', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-WindowStyle', 'Hidden', '-Command', self::powershell($pid)], $logDir); } elseif (PHP_OS_FAMILY === 'Linux' && getenv('DISPLAY') && getenv('XDG_SESSION_TYPE') !== 'wayland' && self::has('xprop') && self::has('xwininfo')) { $this->process = Process::start(['sh', '-c', self::shell(), 'foreground', (string)$pid], $logDir); } } private static function has(string $name): bool { foreach (explode(PATH_SEPARATOR, (string)getenv('PATH')) as $dir) { if ($dir !== '' && is_executable(rtrim($dir, '/') . '/' . $name)) { return true; } } return false; } public function works(): bool { return $this->process !== null && !$this->process->done(); } /** @return array{0: int, 1: int, 2: int, 3: int, 4: int}|null the window in front: x, y, w, h of its inside, its title bar's height */ public function rect(): ?array { if ($this->process === null) { return null; } $lines = $this->process->poll(); $last = end($lines); if ($last !== false) { $v = array_map('intval', preg_split('/\s+/', trim($last)) ?: []); $this->rect = trim($last) === '-' || count($v) < 5 || $v[2] < 200 ? null : [$v[0], $v[1], $v[2], $v[3], $v[4]]; $this->process->lines = []; } return $this->rect; } public function stop(): void { $this->process?->stop(); $this->process?->cleanup(); $this->process = null; } private static function shell(): string { // $1: our pid (our own windows are skipped, the helper ends with us) return <<<'SH'last=""other=""while kill -0 "$1" 2>/dev/null; do # "window id # 0x260005b, 0x0" (XFCE adds a second id): the first one id=$(xprop -root _NET_ACTIVE_WINDOW 2>/dev/null | sed -n 's/.*# *\(0x[0-9a-fA-F]*\).*/\1/p') line="-" case "$id" in 0x0|""|*"not"*) ;; *) pid=$(xprop -id "$id" _NET_WM_PID 2>/dev/null | awk '{print $NF}') type=$(xprop -id "$id" _NET_WM_WINDOW_TYPE 2>/dev/null) if [ "$pid" = "$1" ]; then # one of our windows in front (WinZik just clicked: the mini bar asked): the last window of another program [ -n "$other" ] && line="$other" elif ! echo "$type" | grep -q 'DESKTOP\|DOCK'; then geo=$(xwininfo -id "$id" 2>/dev/null | awk '/Absolute upper-left X/{x=$NF} /Absolute upper-left Y/{y=$NF} /^ *Width/{w=$NF} /^ *Height/{h=$NF} END{print x, y, w, h}') top=$(xprop -id "$id" _NET_FRAME_EXTENTS 2>/dev/null | sed -n 's/.*= *[0-9]*, *[0-9]*, *\([0-9]*\).*/\1/p') [ -n "$geo" ] && line="$geo ${top:-0}" && other="$line" fi ;; esac [ "$line" != "$last" ] && echo "$line" && last="$line" sleep 0.1doneSH; } private static function powershell(int $pid): string { return <<<PS\$code = @'using System; using System.Runtime.InteropServices;public static class Fg { [StructLayout(LayoutKind.Sequential)] public struct R { public int L, T, Ri, B; } [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out R r); [DllImport("user32.dll")] public static extern bool GetClientRect(IntPtr h, out R r); [DllImport("user32.dll")] public static extern bool ClientToScreen(IntPtr h, ref System.Drawing.Point p); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid); [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h); [DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);}'@Add-Type -TypeDefinition \$code -ReferencedAssemblies System.Drawing\$last = ''\$other = ''while (Get-Process -Id {$pid} -ErrorAction SilentlyContinue) { \$h = [Fg]::GetForegroundWindow(); \$line = '-' \$p = 0; [void][Fg]::GetWindowThreadProcessId(\$h, [ref]\$p) if (\$p -eq {$pid} -and \$other) { \$line = \$other } # WinZik in front: the last window of another program elseif (\$h -ne [IntPtr]::Zero -and \$p -ne {$pid} -and [Fg]::IsWindowVisible(\$h) -and -not [Fg]::IsIconic(\$h)) { \$w = New-Object Fg+R; \$c = New-Object Fg+R [void][Fg]::GetWindowRect(\$h, [ref]\$w); [void][Fg]::GetClientRect(\$h, [ref]\$c) \$pt = New-Object System.Drawing.Point 0, 0; [void][Fg]::ClientToScreen(\$h, [ref]\$pt) \$line = "\$(\$pt.X) \$(\$pt.Y) \$(\$c.Ri) \$(\$c.B) \$(\$pt.Y - \$w.T)" \$other = \$line } if (\$line -ne \$last) { [Console]::Out.WriteLine(\$line); [Console]::Out.Flush(); \$last = \$line } Start-Sleep -Milliseconds 100}PS; }}