119 lines
2.9 KiB
PHP
119 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\X11;
|
|
|
|
|
|
|
|
class Window
|
|
{
|
|
|
|
protected $windowId;
|
|
|
|
protected $windowTitle;
|
|
|
|
protected $isVisible;
|
|
|
|
public function __construct($id=null)
|
|
{
|
|
if (!$id) { $id = (int)exec("xdotool getactivewindow"); }
|
|
$this->windowId = $id;
|
|
$this->readWindowInfo();
|
|
}
|
|
|
|
private function readWindowInfo()
|
|
{
|
|
exec("xwininfo -id {$this->windowId} -all -children", $output, $status);
|
|
foreach ($output as $line) {
|
|
if (preg_match('/^(.+?): (.+?)$/', trim($line), $match)) {
|
|
switch ($match[1]) {
|
|
case 'Map State':
|
|
$this->isVisible = ($match[2]=='IsViewable');
|
|
break;
|
|
default:
|
|
//printf("[%s]->'%s'\n", $match[1], $match[2]);
|
|
}
|
|
}
|
|
}
|
|
//printf("---\n");
|
|
}
|
|
|
|
public function getId()
|
|
{
|
|
return hexdec($this->windowId);
|
|
}
|
|
|
|
public function isVisible()
|
|
{
|
|
return $this->isVisible;
|
|
}
|
|
|
|
public function getWindowTitle()
|
|
{
|
|
return trim(exec("xdotool getwindowname {$this->windowId}"));
|
|
}
|
|
|
|
public function setWindowTitle($title)
|
|
{
|
|
exec("xdotool set_window --name ".escapeshellarg($title)." {$this->windowId}");
|
|
}
|
|
|
|
public function sendKeys($keys, $forceWindow=false)
|
|
{
|
|
if ($forceWindow) {
|
|
$cmdl = sprintf("xdotool key --delay 100 --window %d %s", $this->windowId, $keys);
|
|
} else {
|
|
$cmdl = sprintf("xdotool key %s", $keys);
|
|
}
|
|
echo "$ {$cmdl}\n";
|
|
exec($cmdl);
|
|
return $this;
|
|
}
|
|
|
|
public function focus()
|
|
{
|
|
$cmdl = sprintf("xdotool windowfocus %d", $this->windowId);
|
|
echo "$ {$cmdl}\n";
|
|
exec($cmdl);
|
|
return $this;
|
|
}
|
|
|
|
public function activate()
|
|
{
|
|
$cmdl = sprintf("xdotool windowactivate %d", $this->windowId);
|
|
echo "$ {$cmdl}\n";
|
|
exec($cmdl);
|
|
return $this;
|
|
}
|
|
|
|
public function getWindowSize()
|
|
{
|
|
exec("xdotool getwindowgeometry {$this->windowId}", $output, $status);
|
|
$str = join("\n",$output);
|
|
|
|
if (preg_match('/Geometry: ([0-9]+?)x([0-9]+?)$/i', $str, $match)) {
|
|
return (object)[
|
|
'width' => intval($match[1]),
|
|
'height' => intval($match[2])
|
|
];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function setWindowSize($width, $height)
|
|
{
|
|
exec("xdotool windowsize {$this->windowId} {$width} {$height}");
|
|
return $this;
|
|
}
|
|
|
|
public function dump()
|
|
{
|
|
$size = $this->getWindowSize();
|
|
printf("Window: (id=0x%08x)\n Title: %s\n Visible: %s\n Size: %dx%d\n",
|
|
$this->windowId,
|
|
$this->windowTitle,
|
|
$this->isVisible?'true':'false',
|
|
$size->width, $size->height
|
|
);
|
|
}
|
|
}
|