Command feature, fix issue with config key, some refactoring

This commit is contained in:
Simon Vieille 2017-12-18 18:38:48 +01:00
parent c040a4da02
commit 4224674240
No known key found for this signature in database
GPG Key ID: 919533E2B946EA10
1 changed files with 37 additions and 13 deletions

View File

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