From 1ff87e8ece02992554f515a356b047ad425f255c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 26 Jan 2021 08:50:51 +0100 Subject: [PATCH] add screen option: xrandr_extra; add test mode (-n) --- monitordisplay | 54 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/monitordisplay b/monitordisplay index 7c87168..0d5bf2b 100755 --- a/monitordisplay +++ b/monitordisplay @@ -36,6 +36,8 @@ function filterScreen(array $data) 'name' => null, 'resolutionX' => null, 'resolutionY' => null, + 'dpi' => null, + 'xrandr_extra' => null, ]; return filter($data, $default); @@ -85,7 +87,7 @@ function getCurrentMode() function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $addOff) { - if ($mode === null) { + if (null === $mode) { if (!array_key_exists($currentMode['mode'], $modes)) { return; } @@ -101,29 +103,25 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $mode = $modes[$mode]; } - if ($toggle !== false) { - if ($currentMode['mode'] === null) { - return; - } - - if ($currentMode['config'] === null) { - return; - } - + if (false !== $toggle) { $configs = $mode['config']; $configsKeys = array_keys($configs); + if (null === $currentMode['mode']) { + $currentMode['config'] = $configsKeys[0]; + } + if (!array_key_exists($currentMode['config'], $mode['config'])) { $currentMode['config'] = $configsKeys[0]; } - if ($toggle === true) { + if (true === $toggle) { foreach ($configsKeys as $k => $key) { if ($key == $currentMode['config']) { $currentKey = $k; } } -; + $config = $configsKeys[($currentKey + 1) % count($configsKeys)]; } elseif (array_key_exists($toggle, $configs)) { $config = $toggle; @@ -157,6 +155,8 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $name = $tScreen['name']; $resolutionX = $tScreen['resolutionX']; $resolutionY = $tScreen['resolutionY']; + $dpi = $tScreen['dpi']; + $xrandrExtra = $tScreen['xrandr_extra']; if (empty($name) || empty($resolutionX) || empty($resolutionY)) { error(sprintf('Invalid screen configuration "%s"', $screen)); @@ -171,12 +171,20 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $command[] = '--primary'; } + if (null !== $dpi) { + $command[] = sprintf('--dpi %d', $dpi); + } + $command[] = '--mode'; $command[] = sprintf('%dx%d', $resolutionX, $resolutionY); $command[] = '--pos'; $command[] = sprintf('%dx%d', $x, 0); $x += (int) $resolutionX; + + if (null !== $xrandrExtra) { + $command[] = $xrandrExtra; + } } if ($addOff) { @@ -211,7 +219,7 @@ foreach ($files as $file) { if (file_exists($file)) { $parsing = @parse_ini_file($file, true); - if ($parsing === false) { + if (false === $parsing) { error(sprintf('The file "%s" is not a valid ini file', $file)); continue; @@ -238,13 +246,13 @@ $modes = []; foreach ($config as $section => $values) { $type = explode(':', $section); - if (count($type) !== 2) { + if (2 !== count($type)) { error(sprintf('Invalid section "%s". Ignored', $section)); } - if ($type[0] === 'screen') { + if ('screen' === $type[0]) { $screens[$type[1]] = filterScreen($values); - } elseif ($type[0] === 'mode') { + } elseif ('mode' === $type[0]) { $modes[$type[1]] = filterMode($values); } else { error(sprintf('Section "%s" ignored', $section)); @@ -261,10 +269,11 @@ if (empty($modes)) { die(1); } -$options = getopt('m:t::s'); +$options = getopt('m:t::sn'); $mode = isset($options['m']) ? $options['m'] : null; $toggle = isset($options['t']) ? (false === $options['t'] ? true : $options['t']) : false; $addOff = !isset($options['s']); +$isTest = isset($options['n']); $build = build($modes, $screens, $mode, $toggle, $currentMode, $addOff); if (!empty($build)) { @@ -273,15 +282,22 @@ if (!empty($build)) { $commands = [ $build['mode']['before_command'], $build['command'], - $build['mode']['after_command'] + $build['mode']['after_command'], ]; $output = []; + if ($isTest) { + $output[] = 'Mode TEST'; + } + foreach ($commands as $command) { if (!empty($command)) { $output[] = 'Command: '.$command; - exec($command, $output); + + if (!$isTest) { + exec($command, $output); + } } }