Tool for laying out displays on Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
1.7 KiB

<?php
namespace App\LayoutDriver;
class Xrandr implements \App\ILayoutDriver
{
protected $_xrandr;
protected $_displays;
public function __construct(\App\System\Command\Xrandr $xrandr_cmd)
{
$this->_xrandr = $xrandr_cmd;
}
public function read()
{
$xrandr = $this->_xrandr->__invoke([]);
$this->_displays = [];
foreach ($xrandr as $line)
{
if (preg_match('/^(?<output>[^ ]+) connected (?<primary>primary )?(?<screen_w>[0-9]+)x(?<screen_h>[0-9]+)\\+(?<screen_x>[0-9]+)\\+(?<screen_y>[0-9]+) /', $line, $match))
{
$this->_displays[$match['output']] = [
'x' => $match['screen_x'],
'y' => $match['screen_y'],
'w' => $match['screen_w'],
'h' => $match['screen_h'],
'primary' => !empty($match['primary'])
];
}
}
}
public function setOffset(string $output, int $x, int $y): void
{
$this->_displays[$output]['x'] = $x;
$this->_displays[$output]['y'] = $y;
$this->_xrandr->__invoke(['--output', $output, '--pos', $x.'x'.$y]);
echo "Put $output at ${x}x${y}".PHP_EOL;
}
public function getOffset(string $output): array
{
return [
$this->_displays[$output]['x'],
$this->_displays[$output]['y']
];
}
public function setDimensions(string $output, int $w, int $h): void
{
$this->_displays[$output]['w'] = $w;
$this->_displays[$output]['h'] = $h;
$this->_xrandr->__invoke(['--output', $output, '--mode', $w.'x'.$h]);
}
public function getDimensions(string $output): array
{
return [
$this->_displays[$output]['w'],
$this->_displays[$output]['h']
];
}
public function isPrimary(string $output): bool
{
return $this->_displays[$output]['primary'];
}
public function isConnected(string $output): bool
{
return isset($this->_displays[$output]);
}
}