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.
101 lines
2.2 KiB
101 lines
2.2 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\LayoutDriver;
|
|
|
|
/**
|
|
* Interface to xrandr for configuring outputs
|
|
*
|
|
* @author Adam Pippin <hello@adampippin.ca>
|
|
*/
|
|
class Xrandr implements \App\ILayoutDriver
|
|
{
|
|
/**
|
|
* The xrandr command
|
|
* @var \App\System\Command\Xrandr
|
|
*/
|
|
protected $_xrandr;
|
|
|
|
/**
|
|
* Cache of current display parameters
|
|
* @var array<string, array>
|
|
*/
|
|
protected $_displays;
|
|
|
|
/**
|
|
* Create a new xrandr driver
|
|
*
|
|
* @param \App\System\Command\Xrandr $xrandr_cmd
|
|
*/
|
|
public function __construct(\App\System\Command\Xrandr $xrandr_cmd)
|
|
{
|
|
$this->_xrandr = $xrandr_cmd;
|
|
}
|
|
|
|
/**
|
|
* Read display parameters from xrandr
|
|
*
|
|
* @return void
|
|
*/
|
|
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]);
|
|
}
|
|
}
|
|
|