php-x11/src/WindowList.php

90 lines
2.0 KiB
PHP

<?php
namespace NoccyLabs\X11;
use IteratorAggregate;
use ArrayIterator;
use Countable;
class WindowList implements IteratorAggregate, Countable
{
private $windows = [];
public function __construct(array $windows=null)
{
if (null === $windows) {
$this->readRootWindowTree();
} elseif (is_array($windows)) {
$this->windows = $windows;
}
}
public function getIterator()
{
return new ArrayIterator($this->windows);
}
public function count()
{
return count($this->windows);
}
private function readRootWindowTree()
{
exec("xwininfo -root -tree", $output, $status);
$this->windows = [];
foreach ($output as $line) {
if (preg_match('/^\s+0x([0-9a-f]+?) (.+?)\: \((.+?)\)/', $line, $match)) {
$this->windows[] = new Window(hexdec($match[1]));
}
}
}
public function find($title)
{
$found = [];
foreach ($this->windows as $window) {
if (fnmatch($title, $window->getWindowTitle(), FNM_CASEFOLD)) {
$found[] = $window;
}
}
return new WindowList($found);
}
public function findByClass($class)
{
$found = [];
return new WindowList($found);
}
public function getRootWindow()
{
}
public function visible($match=true)
{
$found = [];
$match = (bool)$match;
foreach ($this->windows as $window) {
if ($window->isVisible() == $match)
$found[] = $window;
}
return new WindowList($found);
}
public function first()
{
return count($this->windows)?$this->windows[0]:null;
}
public function dump()
{
printf("WindowList: (count=%d)\n", count($this));
foreach ($this as $window) {
printf(" 0x%08x: %s %s\n", $window->getId(), $window->getWindowTitle(), $window->isVisible()?'(Visible)':'');
}
}
}