diff --git a/monitordisplay b/monitordisplay index 96b5269..7c87168 100755 --- a/monitordisplay +++ b/monitordisplay @@ -46,6 +46,8 @@ function filterMode(array $data) $default = [ 'config' => [], 'primary' => null, + 'before_command' => null, + 'after_command' => null, ]; $data = filter($data, $default); @@ -107,7 +109,7 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, if ($currentMode['config'] === null) { return; } - + $configs = $mode['config']; $configsKeys = array_keys($configs); @@ -117,7 +119,7 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, if ($toggle === true) { foreach ($configsKeys as $k => $key) { - if ($key === $currentMode['config']) { + if ($key == $currentMode['config']) { $currentKey = $k; } } @@ -130,7 +132,7 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, die(1); } } else { - $config = 0; + $config = array_keys($mode['config'])[0]; } if (!array_key_exists($config, $mode['config'])) { @@ -162,7 +164,7 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, } $command[] = '--output'; - $command[] = $name; + $command[] = escapeshellarg($name); $screensUsed[] = $name; if ($mode['primary'] === $name) { @@ -174,14 +176,14 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, $command[] = '--pos'; $command[] = sprintf('%dx%d', $x, 0); - $x += $resolutionX; + $x += (int) $resolutionX; } if ($addOff) { foreach ($screens as $screen) { if (!in_array($screen['name'], $screensUsed)) { $command[] = '--output'; - $command[] = $screen['name']; + $command[] = escapeshellarg($screen['name']); $command[] = '--off'; } } @@ -192,6 +194,7 @@ function build(array $modes, array $screens, $mode, $toggle, array $currentMode, return [ 'config' => sprintf("mode=%s\nconfig=%s", $tmpMode, $tmpConfig), 'command' => $command, + 'mode' => $mode, ]; } @@ -209,7 +212,7 @@ foreach ($files as $file) { $parsing = @parse_ini_file($file, true); if ($parsing === false) { - 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; } @@ -225,7 +228,7 @@ foreach ($files as $file) { if (empty($config)) { error('No configuration found'); - error(sprintf('Paths checked: %s', implode(', ', $files))); + error(sprintf('Paths checked: \n%s', implode(', ', $files))); die(1); } @@ -236,7 +239,7 @@ foreach ($config as $section => $values) { $type = explode(':', $section); if (count($type) !== 2) { - error(sprintf('Invalid section "%s". Ignored.', $section)); + error(sprintf('Invalid section "%s". Ignored', $section)); } if ($type[0] === 'screen') { @@ -244,17 +247,17 @@ foreach ($config as $section => $values) { } elseif ($type[0] === 'mode') { $modes[$type[1]] = filterMode($values); } else { - error(sprintf('Invalid section "%s". Ignored.', $section)); + error(sprintf('Section "%s" ignored', $section)); } } if (empty($screens)) { - error('No screen found.'); + error('No screen found'); die(1); } if (empty($modes)) { - error('No mode found.'); + error('No mode found'); die(1); } @@ -266,5 +269,26 @@ $build = build($modes, $screens, $mode, $toggle, $currentMode, $addOff); if (!empty($build)) { file_put_contents(getCurrentModeFile(), $build['config']); - shell_exec($build['command']); + + $commands = [ + $build['mode']['before_command'], + $build['command'], + $build['mode']['after_command'] + ]; + + $output = []; + + foreach ($commands as $command) { + if (!empty($command)) { + $output[] = 'Command: '.$command; + exec($command, $output); + } + } + + notice(implode("\n", $output)); + + die(0); } + +error(sprintf('Invalid mode "%s"', $mode)); +die(1);