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.1 done SH; } private static function powershell(int $pid): string { return <<