add screen option: xrandr_extra; add test mode (-n)

This commit is contained in:
Simon Vieille 2021-01-26 08:50:51 +01:00
parent 4224674240
commit 1ff87e8ece
Signed by: deblan
GPG key ID: 03383D15A1D31745

View file

@ -36,6 +36,8 @@ function filterScreen(array $data)
'name' => null, 'name' => null,
'resolutionX' => null, 'resolutionX' => null,
'resolutionY' => null, 'resolutionY' => null,
'dpi' => null,
'xrandr_extra' => null,
]; ];
return filter($data, $default); return filter($data, $default);
@ -85,7 +87,7 @@ function getCurrentMode()
function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $addOff) function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $addOff)
{ {
if ($mode === null) { if (null === $mode) {
if (!array_key_exists($currentMode['mode'], $modes)) { if (!array_key_exists($currentMode['mode'], $modes)) {
return; return;
} }
@ -101,29 +103,25 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode,
$mode = $modes[$mode]; $mode = $modes[$mode];
} }
if ($toggle !== false) { if (false !== $toggle) {
if ($currentMode['mode'] === null) {
return;
}
if ($currentMode['config'] === null) {
return;
}
$configs = $mode['config']; $configs = $mode['config'];
$configsKeys = array_keys($configs); $configsKeys = array_keys($configs);
if (null === $currentMode['mode']) {
$currentMode['config'] = $configsKeys[0];
}
if (!array_key_exists($currentMode['config'], $mode['config'])) { if (!array_key_exists($currentMode['config'], $mode['config'])) {
$currentMode['config'] = $configsKeys[0]; $currentMode['config'] = $configsKeys[0];
} }
if ($toggle === true) { if (true === $toggle) {
foreach ($configsKeys as $k => $key) { foreach ($configsKeys as $k => $key) {
if ($key == $currentMode['config']) { if ($key == $currentMode['config']) {
$currentKey = $k; $currentKey = $k;
} }
} }
;
$config = $configsKeys[($currentKey + 1) % count($configsKeys)]; $config = $configsKeys[($currentKey + 1) % count($configsKeys)];
} elseif (array_key_exists($toggle, $configs)) { } elseif (array_key_exists($toggle, $configs)) {
$config = $toggle; $config = $toggle;
@ -157,6 +155,8 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode,
$name = $tScreen['name']; $name = $tScreen['name'];
$resolutionX = $tScreen['resolutionX']; $resolutionX = $tScreen['resolutionX'];
$resolutionY = $tScreen['resolutionY']; $resolutionY = $tScreen['resolutionY'];
$dpi = $tScreen['dpi'];
$xrandrExtra = $tScreen['xrandr_extra'];
if (empty($name) || empty($resolutionX) || empty($resolutionY)) { if (empty($name) || empty($resolutionX) || empty($resolutionY)) {
error(sprintf('Invalid screen configuration "%s"', $screen)); error(sprintf('Invalid screen configuration "%s"', $screen));
@ -171,12 +171,20 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode,
$command[] = '--primary'; $command[] = '--primary';
} }
if (null !== $dpi) {
$command[] = sprintf('--dpi %d', $dpi);
}
$command[] = '--mode'; $command[] = '--mode';
$command[] = sprintf('%dx%d', $resolutionX, $resolutionY); $command[] = sprintf('%dx%d', $resolutionX, $resolutionY);
$command[] = '--pos'; $command[] = '--pos';
$command[] = sprintf('%dx%d', $x, 0); $command[] = sprintf('%dx%d', $x, 0);
$x += (int) $resolutionX; $x += (int) $resolutionX;
if (null !== $xrandrExtra) {
$command[] = $xrandrExtra;
}
} }
if ($addOff) { if ($addOff) {
@ -211,7 +219,7 @@ foreach ($files as $file) {
if (file_exists($file)) { if (file_exists($file)) {
$parsing = @parse_ini_file($file, true); $parsing = @parse_ini_file($file, true);
if ($parsing === false) { if (false === $parsing) {
error(sprintf('The file "%s" is not a valid ini file', $file)); error(sprintf('The file "%s" is not a valid ini file', $file));
continue; continue;
@ -238,13 +246,13 @@ $modes = [];
foreach ($config as $section => $values) { foreach ($config as $section => $values) {
$type = explode(':', $section); $type = explode(':', $section);
if (count($type) !== 2) { if (2 !== count($type)) {
error(sprintf('Invalid section "%s". Ignored', $section)); error(sprintf('Invalid section "%s". Ignored', $section));
} }
if ($type[0] === 'screen') { if ('screen' === $type[0]) {
$screens[$type[1]] = filterScreen($values); $screens[$type[1]] = filterScreen($values);
} elseif ($type[0] === 'mode') { } elseif ('mode' === $type[0]) {
$modes[$type[1]] = filterMode($values); $modes[$type[1]] = filterMode($values);
} else { } else {
error(sprintf('Section "%s" ignored', $section)); error(sprintf('Section "%s" ignored', $section));
@ -261,10 +269,11 @@ if (empty($modes)) {
die(1); die(1);
} }
$options = getopt('m:t::s'); $options = getopt('m:t::sn');
$mode = isset($options['m']) ? $options['m'] : null; $mode = isset($options['m']) ? $options['m'] : null;
$toggle = isset($options['t']) ? (false === $options['t'] ? true : $options['t']) : false; $toggle = isset($options['t']) ? (false === $options['t'] ? true : $options['t']) : false;
$addOff = !isset($options['s']); $addOff = !isset($options['s']);
$isTest = isset($options['n']);
$build = build($modes, $screens, $mode, $toggle, $currentMode, $addOff); $build = build($modes, $screens, $mode, $toggle, $currentMode, $addOff);
if (!empty($build)) { if (!empty($build)) {
@ -273,15 +282,22 @@ if (!empty($build)) {
$commands = [ $commands = [
$build['mode']['before_command'], $build['mode']['before_command'],
$build['command'], $build['command'],
$build['mode']['after_command'] $build['mode']['after_command'],
]; ];
$output = []; $output = [];
if ($isTest) {
$output[] = 'Mode TEST';
}
foreach ($commands as $command) { foreach ($commands as $command) {
if (!empty($command)) { if (!empty($command)) {
$output[] = 'Command: '.$command; $output[] = 'Command: '.$command;
exec($command, $output);
if (!$isTest) {
exec($command, $output);
}
} }
} }