Browse Source

initial commit

master
Adam Pippin 4 years ago
commit
505cfa87f4
  1. 13
      .editorconfig
  2. 32
      .githooks/pre-commit
  3. 36
      .githooks/pre-push
  4. 5
      .gitignore
  5. 67
      .phan/config.php
  6. 50
      .phan/stubs/Log.php
  7. 330
      .php_cs.dist
  8. 0
      app/Commands/.gitkeep
  9. 52
      app/Commands/Layout.php
  10. 71
      app/Commands/Monitor.php
  11. 64
      app/Configuration.php
  12. 71
      app/Display.old.php
  13. 39
      app/Engine.php
  14. 14
      app/ILayoutDriver.php
  15. 170
      app/Layout.php
  16. 76
      app/LayoutDriver/Xrandr.php
  17. 28
      app/Providers/AppServiceProvider.php
  18. 12
      app/Providers/LayoutServiceProvider.php
  19. 56
      app/Screen.php
  20. 93
      app/System/Command.php
  21. 15
      app/System/Command/Xrandr.php
  22. 25
      app/System/CommandArgument.php
  23. 585
      app/System/Process.php
  24. 18
      app/System/RawCommandArgument.php
  25. 13
      app/System/SensitiveCommandArgument.php
  26. 50
      bootstrap/app.php
  27. 18
      box.json
  28. 52
      composer.json
  29. 5109
      composer.lock
  30. 61
      config/app.php
  31. 80
      config/commands.php
  32. 53
      monitor_layout
  33. 24
      phpunit.xml.dist
  34. 42
      sample.yaml
  35. 22
      tests/CreatesApplication.php
  36. 20
      tests/Feature/InspiringCommandTest.php
  37. 10
      tests/TestCase.php
  38. 18
      tests/Unit/ExampleTest.php

13
.editorconfig

@ -0,0 +1,13 @@
root = true
[*]
end_of_line = lf
indent_style = tab
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.yml]
indent_style = space
indent_size = 2

32
.githooks/pre-commit

@ -0,0 +1,32 @@
#!/usr/bin/env bash
CURRENT_DIRECTORY=`pwd`
GIT_HOOKS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIRECTORY="$GIT_HOOKS_DIR/../.."
cd $PROJECT_DIRECTORY;
echo "Running php-cs-fixer to format the code..."
PHP_CS_FIXER="vendor/bin/php-cs-fixer"
HAS_PHP_CS_FIXER=false
if [ -x "$PHP_CS_FIXER" ]; then
HAS_PHP_CS_FIXER=true
fi
if $HAS_PHP_CS_FIXER; then
git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
${PHP_CS_FIXER} fix --config=.php_cs.dist -q --allow-risky=yes ${line};
git add "$line";
done
else
echo ""
echo "Please run `composer install` to include php-cs-fixer."
echo ""
fi
cd $CURRENT_DIRECTORY;
echo "Done."

36
.githooks/pre-push

@ -0,0 +1,36 @@
#!/usr/bin/env bash
CURRENT_DIRECTORY=`pwd`
GIT_HOOKS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIRECTORY="$GIT_HOOKS_DIR/../.."
cd $PROJECT_DIRECTORY;
echo "Running phan to analyze code..."
PHAN="vendor/bin/phan"
HAS_PHAN=false
if [ -x "$PHAN" ]; then
HAS_PHAN=true
fi
if $HAS_PHAN; then
PHAN_RESULT=`$PHAN`
if [ ! -z "$PHAN_RESULT" ]; then
echo ""
echo "Please fix these issues with your code before committing:"
echo ""
echo $PHAN_RESULT
echo ""
exit 1
fi
else
echo ""
echo "Please run `composer install` to include phan."
echo ""
fi
cd $CURRENT_DIRECTORY;
echo "Done."

5
.gitignore

@ -0,0 +1,5 @@
/vendor
/.idea
/.vscode
/.vagrant
.php_cs.cache

67
.phan/config.php

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
return [
'target_php_version' => '7.2',
'directory_list' => [
'.phan/stubs/',
'app/',
'vendor/illuminate/',
'vendor/laravel-zero/',
'vendor/nunomaduro/',
'vendor/composer/',
'vendor/symfony/',
'vendor/aws/'
],
'exclude_analysis_directory_list' => [
'vendor/',
'.phan/stubs/'
],
'plugins' => [
// stricter checks on whether a function returns the proper type
'AlwaysReturnPlugin',
// warn about trying to set duplicate keys in arrays (common mistake)
// warn about duplicate switch case values
// warn about mixing arrays/dictionaries
'DuplicateArrayKeyPlugin',
// validate regular expressions
'PregRegexCheckerPlugin',
// validate printf statements
'PrintfCheckerPlugin',
// Check for unreachable code in functions
'UnreachableCodePlugin',
// warn if you're calling a function where you probably should be using
// the return value (e.g., printf) but are not
'UseReturnValuePlugin',
// Infer values from phpunit tests
//'PHPUnitAssertionPlugin'
// Warn if structures are missing phpdoc plugins
'HasPHPDocPlugin',
// warn on isset(func()['index']) and isset($array[$key]) where $array is not set
//'InvalidVariableIssetPlugin'
// warn about returning non-array values from __sleep
'SleepCheckerPlugin',
// warn about unknown return/parameter type (not documented and unable to
// be inferred)
'UnknownElementTypePlugin',
// warn about expressions that are likely to be a bug (e.g., a == a)
'DuplicateExpressionPlugin',
// try and use some heuristics to detect if parameters are out of order
// on some function calls
//'SuspiciousParamOrderPlugin',
],
'plugin_config' => [
// UseReturnValuePlugin -- slow; check and see if the return value of a
// function is *normally* used and if so, warn where it is not.
//'use_return_value_dynamic_checks'=>true
//'use_return_value_warn_threshold_percentage'=>98,
],
'minimum_severity' => 0
];

50
.phan/stubs/Log.php

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
class Log
{
public static function emergency(string $message, array $context = []): void
{
}
public static function alert(string $message, array $context = []): void
{
}
public static function critical(string $message, array $context = []): void
{
}
public static function error(string $message, array $context = []): void
{
}
public static function warning(string $message, array $context = []): void
{
}
public static function notice(string $message, array $context = []): void
{
}
public static function info(string $message, array $context = []): void
{
}
public static function debug(string $message, array $context = []): void
{
}
public static function log($level, string $message, array $context = []): void
{
}
public static function channel(string $channel = null)
{
}
public static function stack(array $channels, string $channel = null): \Psr\Log\LoggerInterface
{
}
}

330
.php_cs.dist

@ -0,0 +1,330 @@
<?php
$finder = Symfony\Component\Finder\Finder::create()
->exclude('bootstrap/cache')
->exclude('storage')
->exclude('vendor')
->in(__DIR__)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true)
;
return PhpCsFixer\Config::create()
->setRules([
'align_multiline_comment' => [
'comment_type' => 'phpdocs_only'
],
'array_indentation' => true,
'array_syntax' => [
'syntax' => 'short'
],
'binary_operator_spaces' => [
'default' => 'single_space'
],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
//'blank_line_before_statement' => [
// 'statements' => ['break', 'case', 'continue', 'declare', 'default', 'die', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield']
// ]
'braces' => [
'allow_single_line_closure' => true,
'position_after_anonymous_constructs' => 'same',
'position_after_control_structures' => 'next',
'position_after_functions_and_oop_constructs' => 'next'
],
'cast_spaces' => [
'space' => 'none'
],
'class_attributes_separation' => [
'elements' => ['const', 'method', 'property']
],
'class_definition' => [
'multi_line_extends_each_single_line' => false,
'single_item_single_line' => false,
'single_line' => false
],
'class_keyword_remove' => false,
'combine_consecutive_issets' => false,
'combine_consecutive_unsets' => true,
'combine_nested_dirname' => true,
'comment_to_phpdoc' => true,
'compact_nullable_typehint' => true,
'concat_space' => [
'spacing' => 'none'
],
//'date_time_immutable' => false,
'declare_equal_normalize' => [
'space'=>'none'
],
'declare_strict_types' => true,
'dir_constant' => true,
'elseif' => true,
'encoding' => true,
'ereg_to_preg' => true,
'error_suppression' => [
'mute_deprecation_error' => false,
'noise_remaining_usages' => true,
'noise_remaining_usages_exclude' => [] // functions to exclude
],
'escape_implicit_backslashes' => [
'double_quoted' => true,
'heredoc_syntax' => true,
'single_quoted' => true
],
'explicit_indirect_variable' => true,
'explicit_string_variable' => false,
//'final_class' => true, // mark all non-abstract classes as final
//'final_internal_class' => [ see doc for opts ] // mark all internal classes as final
'fopen_flag_order' => true,
'fopen_flags' => [
'b_mode' => true
],
'full_opening_tag' => true,
//'fully_qualified_strict_types' => ???,
'function_declaration' => [
'closure_function_spacing' => 'none'
],
//'function_to_constant' => [
// replaces get_called_class, get_class, php_sapi_name, phpversion, pi with
// constants... not sure how that could be valid?
//],
'function_typehint_space' => true,
'general_phpdoc_annotation_remove' => [
'annotations' => [] // list of @annotations to remove form phpdoc comments
],
//'heredoc_indentation' => true, // requires php 7.3
'heredoc_to_nowdoc' => true,
'implode_call' => true,
'include' => true,
//'increment_style' => [
// 'style' => // post/pre
//],
'indentation_type' => true,
'is_null' => true,
'line_ending' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => [
'syntax' => 'short'
],
'logical_operators' => true,
'lowercase_cast' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'magic_method_casing' => true,
//'mb_str_function' => true, // replace non-mb safe with mb_ methods
'method_argument_space' => [
'keep_multiple_spaces_after_comma' => false,
'on_multiline' => 'ignore' // ensure_fully_multiline, ensure_single_line, ignore
],
'method_chaining_indentation' => true,
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'new_line_for_chained_calls'
],
'native_constant_invocation' => [],
'native_function_casing' => true,
'native_function_invocation' => [],
'native_function_type_declaration_casing' => true,
//'new_with_brances' => true, // no idea what this does? included in @Symfony bundle
'no_alias_functions' => [],
'no_alternative_syntax' => true, // get rid of while {} endwhile; stuff
'no_binary_string' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
//'no_blank_lines_before_namespace' => true,
'no_break_comment' => [
'comment_text' => 'no break'
],
'no_closing_tag' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => [
'tokens' => ['extra']
],
'no_homoglyph_names' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => [
'use' => 'echo'
],
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => false,
//'no_php4_constructor' => true,
'no_short_bool_cast' => true,
'no_short_echo_tag' => false,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_around_offset' => [
'positions' => ['inside', 'outside']
],
'no_spaces_inside_parenthesis' => true,
//'no_superfluous_elseif' => false, // don't know what this would do?
//'no_superfluous_phpdoc_tags' => [
// removes @param/@return that "don't provide any useful information"
//],
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_cast' => true,
'no_unset_on_property' => false,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => [
'after_heredoc' => false
],
'no_whitespace_in_blank_line' => true,
'non_printable_character' => [
'use_escape_sequences_in_strings' => true // don't just remove them, make them visible to the programmer and let them sort it out
],
'normalize_index_brace' => true,
'not_operator_with_space' => false,
'not_operator_with_successor_space' => false,
'object_operator_without_whitespace' => true,
//'ordered_class_elements' => [ // lets us sort methods/props/etc in classes
// 'order' => [],
// 'sortAlgorithm'=>''
//],
//'ordered_imports' => [ // sort use statements, off so we can retain some sort of context
// 'sort_algorithm' => 'alpha'
//],
//'ordered_interfaces' => [ // sort implements or interface extends
//],
'php_unit_construct' => [ // replace phpunit's ->assertSame(true, $foo) with ->assertTrue($foo)
],
'php_unit_dedicate_assert' => [ // try and replace things like assertTrue(file_exists()) with assertFileExists
'target'=>'newest'
],
'php_unit_dedicate_assert_internal_type' => [
'target'=>'newest'
],
'php_unit_expectation' => [ // should use expectException instead of setExpectedException
'target' => 'newest'
],
'php_unit_fqcn_annotation' => true,
'php_unit_internal_class' => [ // phpunit tests should be marked internal
'types' => ['normal', 'final']
],
'php_unit_mock' => [ // use createMock instead of getMock
'target' => 'newest'
],
'php_unit_mock_short_will_return' => true,
'php_unit_namespaced' => true,
'php_unit_no_expectation_annotation' => [
'target'=>'newest'
],
'php_unit_ordered_covers' => true,
'php_unit_set_up_tear_down_visibility' => true,
//'php_unit_size_class' => true, // tests should have @small/@medium/@large annotation
'php_unit_strict' => [
],
'php_unit_test_annotation' => [ // add @test annotation to tests
],
'php_unit_test_case_static_method_calls' => [
],
'php_unit_test_class_requires_covers' => true,
'phpdoc_add_missing_param_annotation' => [
'only_untyped' => false
],
'phpdoc_align' => [
'align' => 'vertical'
],
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag' => true,
'phpdoc_no_access' => true,
//'phpdoc_no_alias_tag' => [],
'phpdoc_no_empty_return' => false,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
'phpdoc_return_self_reference' => [],
'phpdoc_scalar' => true,
'phpdoc_separation' => false,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
//'phpdoc_to_comment' => false, // we sometimes use these for phan suppression and stuff
//'phpdoc_to_return_type' => [ 'scalar_types' => true ],
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => [ // fix casing of types in phpdoc
'groups' => ['simple', 'alias', 'meta']
],
'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
'sort_algorithm' => 'none'
],
'phpdoc_var_annotation_correct_order' => true,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
//'protected_to_private' => true, // convert protected methods to private ones
'psr4' => true, // class name should match filename
'random_api_migration' => [ // replace rand/srand/etc with the mt_ funcs
],
'return_assignment' => true,
'return_type_declaration' => [
'space_before' => 'none'
],
//'self_accessor' => false, // force use of self instead of class name
'semicolon_after_instruction' => true,
'set_type_to_cast' => true, // use casting not settype
'short_scalar_cast' => true, // use bool not boolean, int not integer, etc
'simple_to_complex_string_variable' => false, // convert ${var} to {$var} in strings
'simplified_null_return' => true, // don't `return null`, just `return`
'single_blank_line_at_eof' => true,
'single_blank_line_before_namespace' => true,
'single_class_element_per_statement' => [
'elements' => ['const', 'property']
],
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_line_comment_style' => [
'comment_types' => ['asterisk', 'hash']
],
'single_quote' => [
'strings_containing_single_quote_chars' => false
],
'single_trait_insert_per_statement' => true,
'space_after_semicolon' => [
'remove_in_empty_for_expressions' => false
],
'standardize_increment' => true,
'standardize_not_equals' => true,
'static_lambda' => true,
//'strict_comparsion' => true, // would be good to *check* but not change
//'strict_param' => true,
//'string_line_ending' => true, // could screw up literals that are used for comparison to outside sourced data
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
//'trailing_comma_in_multiline_array' => [ // no option to *remove*
//],
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => ['property', 'method', 'const']
],
//'void_return' => true, // adds a void return type to functions without a @return/:return
'whitespace_after_comma_in_array' => true,
//'yoda_style' => [ // would be a good habit but I find it hard to read
// 'always_move_variable' => false,
// 'equal' => false,
// 'identical' => false,
// 'less_and_greater' => false
//]
])
->setIndent("\t")
->setLineEnding("\n")
->setFinder($finder)
;

0
app/Commands/.gitkeep

52
app/Commands/Layout.php

@ -0,0 +1,52 @@
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class Layout extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'layout {config}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Examine all screens and layouts and effect the best match';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(\App\ILayoutDriver $driver)
{
$config_file = $this->argument('config');
if (!file_exists($config_file) || !is_readable($config_file))
throw new \Exception("Cannot read config file: $config_file");
$config = new \App\Configuration();
$config->load($config_file);
$engine = app()->make(\App\Engine::class, ['config'=>$config]);
$engine->layout();
}
}

71
app/Commands/Monitor.php

@ -0,0 +1,71 @@
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class Monitor extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monitor {config} {--interval=5}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Relayout on a schedule';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(\App\ILayoutDriver $driver)
{
$config_file = $this->argument('config');
if (!file_exists($config_file) || !is_readable($config_file))
throw new \Exception("Cannot read config file: $config_file");
$config = new \App\Configuration();
$config->load($config_file);
$engine = app()->make(\App\Engine::class, ['config'=>$config]);
declare(ticks = 1);
$signal_handler = function($signal) use ($config_file, &$engine) {
switch ($signal)
{
case SIGHUP:
$config = new \App\Configuration();
$config->load($config_file);
$engine = app()->make(\App\Engine::class, ['config'=>$config]);
break;
}
};
pcntl_signal(SIGHUP, $signal_handler);
while (true)
{
$engine->layout();
sleep($this->option('interval'));
}
}
}

64
app/Configuration.php

@ -0,0 +1,64 @@
<?php
namespace App;
use Symfony\Component\Yaml\Yaml;
class Configuration
{
protected $_screens;
protected $_layouts;
public function __construct()
{
$this->_screens = [];
$this->_layouts = [];
}
public function load(string $file): void
{
$config = Yaml::parseFile($file);
foreach ($config['screens'] as $screen_name=>$screen)
{
$this->_screens[$screen_name] = app()->make(\App\Screen::class, [
'outputs'=>is_array($screen['output']) ? $screen['output'] : [$screen['output']]
]);
}
foreach ($config['layouts'] as $layout=>$data)
{
$this->_layouts[$layout] = app()->make(\App\Layout::class, ['screens'=>$data['screens'], 'links'=>$data['links']]);
}
}
public function getScreen(string $screen): Screen
{
return $this->_screens[$screen];
}
public function getPrimaryScreen(): Screen
{
return $this->getScreen($this->getPrimaryScreenName());
}
public function getPrimaryScreenName(): string
{
foreach ($this->_screens as $name=>$screen)
{
if ($screen->isPrimary())
return $name;
}
}
public function getLayout(string $layout): Layout
{
return $this->_layouts[$layout];
}
public function getLayouts(): array
{
return $this->_layouts;
}
}

71
app/Display.old.php

@ -0,0 +1,71 @@
<?php
namespace App;
class Display
{
protected $_names;
protected $_output;
protected $_width;
protected $_height;
protected $_x;
protected $_y;
public function __construct(array $names)
{
$this->_names = $names;
}
public function read()
{
foreach ($this->_names as $name)
{
$xrandr = trim(shell_exec('xrandr | grep "^'.$name.'"'));
if (!empty($xrandr))
break;
}
if (empty($xrandr))
throw new \Exception("Cannot load display: ".end($this->_names));
# DVI-I-2-1 connected 1920x1080+1920+1440 (normal left inverted right x axis y axis) 521mm x 293mm
if (!preg_match('/^(?<output>[^ ]+) connected (?<screen_w>[0-9]+)x(?<screen_h>[0-9]+)\\+(?<screen_x>[0-9]+)\\+(?<screen_y>[0-9]+) /', $xrandr, $matches))
throw new \Exception("Cannot parse xrandr response for: ".$name);
$this->_output = $matches['output'];
$this->_x = $matches['screen_x'];
$this->_y = $matches['screen_y'];
$this->_width = $matches['screen_w'];
$this->_height = $matches['screen_h'];
}
public function getX(): int
{
return (int)$this->_x;
}
public function getY(): int
{
return (int)$this->_y;
}
public function getWidth(): int
{
return (int)$this->_width;
}
public function getHeight(): int
{
return (int)$this->_height;
}
public function setOffset(int $x, int $y): void
{
shell_exec('xrandr --output '.$this->_output.' --pos '.$x.'x'.$y);
}
public function setDimensions(int $width, int $height): void
{
shell_exec('xrandr --output '.$this->_output.' --mode '.$width.'x'.$height);
}
}

39
app/Engine.php

@ -0,0 +1,39 @@
<?php
namespace App;
class Engine
{
protected $_config, $_driver;
public function __construct(\App\Configuration $config, \App\ILayoutDriver $driver)
{
$this->_config = $config;
$this->_driver = $driver;
}
public function layout()
{
foreach ($this->_config->getLayouts() as $layout_name=>$layout)
{
$screens = $layout->getScreenNames();
$all_screens = true;
foreach ($screens as $screen_name)
{
if (!$this->_config->getScreen($screen_name)->isConnected())
{
$all_screens = false;
break;
}
}
if (!$all_screens)
{
continue;
}
$layout->execute($this->_config, $this->_driver);
break;
}
}
}

14
app/ILayoutDriver.php

@ -0,0 +1,14 @@
<?php
namespace App;
interface ILayoutDriver
{
public function setOffset(string $output, int $x, int $y): void;
public function getOffset(string $output): array;
public function setDimensions(string $output, int $x, int $y): void;
public function getDimensions(string $output): array;
public function isConnected(string $output): bool;
public function isPrimary(string $output): bool;
}

170
app/Layout.php

@ -0,0 +1,170 @@
<?php
namespace App;
/**
* Calculate the layout of a collection of screens
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class Layout
{
/**
* List of screens this layout uses
* @var array[]
*/
protected $_screens;
/**
* Relations between the screens
* @var array[]
*/
protected $_links;
/**
* Initialize a new layout
*
* @param array[] $screens
* @param array[] $links
*/
public function __construct(array $screens, array $links)
{
$this->_screens = $screens;
$this->_links = $links;
}
/**
* Fetch the names of the screens this layout depends on
*
* @return string[]
*/
public function getScreenNames(): array
{
return array_keys($this->_screens);
}
/**
* Execute this layout
*
* @param Configuration $config
* @param ILayoutDriver $driver
* @return void
*/
public function execute(Configuration $config, ILayoutDriver $driver): void
{
$primary_name = $config->getPrimaryScreenName();
$primary = $config->getPrimaryScreen();
$layout_stack = [$primary_name];
$did_layout = false;
$layout = [$primary_name=>['x'=>0, 'y'=>0]];
foreach ($this->_screens as $screen_name=>$screen_data)
{
if (isset($screen_data['width']) && isset($screen_data['height']))
{
$config->getScreen($screen_name)->setDimensions($screen_data['width'], $screen_data['height']);
}
}
while (sizeof($layout_stack))
{
$did_layout = false;
$match_screen = array_pop($layout_stack);
foreach ($this->_links as $link)
{
$target_name = $link['display'];
$source_name = $link['above'] ?? $link['below'] ?? $link['right_of'] ?? $link['left_of'];
if ($source_name == $match_screen)
{
$did_layout = true;
$calculate_layout = null;
if (isset($link['above']))
{
$calculate_layout = function($s_x, $s_y, $s_w, $s_h, $d_x, $d_y, $d_w, $d_h) {
$center = $s_x + ($s_w / 2);
$x = $center - ($d_w / 2);
$y = $s_y - $d_h;
return [$x, $y];
};
}
else if (isset($link['below']))
{
$calculate_layout = function($s_x, $s_y, $s_w, $s_h, $d_x, $d_y, $d_w, $d_h) {
$center = $s_x + ($s_w / 2);
$x = $center - ($d_w / 2);
$y = $s_y + $s_h;
return [$x, $y];
};
}
else if (isset($link['left_of']))
{
$calculate_layout = function($s_x, $s_y, $s_w, $s_h, $d_x, $d_y, $d_w, $d_h) {
return [$s_x - $d_w, $s_y];
};
}
else if (isset($link['right_of']))
{
$calculate_layout = function($s_x, $s_y, $s_w, $s_h, $d_x, $d_y, $d_w, $d_h) {
return [$s_x + $s_w, $s_y];
};
}
$source = $config->getScreen($source_name);
$target = $config->getScreen($target_name);
if (isset($layout[$source_name]))
{
$s_x = $layout[$source_name]['x'];
$s_y = $layout[$source_name]['y'];
}
else
{
list($s_x, $s_y) = $source->getOffset();
}
list($s_w, $s_h) = $source->getDimensions();
if (isset($layout[$target_name]))
{
$d_x = $layout[$target_name]['x'];
$d_y = $layout[$target_name]['y'];
}
else
{
list($d_x, $d_y) = $target->getOffset();
}
list($d_w, $d_h) = $target->getDimensions();
list($x, $y) = $calculate_layout($s_x, $s_y, $s_w, $s_h, $d_x, $d_y, $d_w, $d_h);
$layout[$target_name] = ['x'=>$x, 'y'=>$y];
$layout_stack[] = $target_name;
}
}
}
$min_x = PHP_INT_MAX;
$min_y = PHP_INT_MAX;
foreach ($layout as $screen_name=>$screen_offset)
{
$min_x = min($screen_offset['x'], $min_x);
$min_y = min($screen_offset['y'], $min_y);
}
foreach ($layout as $screen_name=>$screen_offset)
{
list($current_x, $current_y) = $config->getScreen($screen_name)->getOffset();
if ($current_x != $screen_offset['x']-$min_x || $current_y != $screen_offset['y']-$min_y)
{
$config->getScreen($screen_name)->setOffset($screen_offset['x']-$min_x, $screen_offset['y']-$min_y);
}
}
}
}

76
app/LayoutDriver/Xrandr.php

@ -0,0 +1,76 @@
<?php
namespace App\LayoutDriver;
class Xrandr implements \App\ILayoutDriver
{
protected $_xrandr;
protected $_displays;
public function __construct(\App\System\Command\Xrandr $xrandr_cmd)
{
$this->_xrandr = $xrandr_cmd;
}
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]);
}
}

28
app/Providers/AppServiceProvider.php

@ -0,0 +1,28 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

12
app/Providers/LayoutServiceProvider.php

@ -0,0 +1,12 @@
<?php
namespace App\Providers;
class LayoutServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{
$this->app->singleton(\App\ILayoutDriver::class, \App\LayoutDriver\Xrandr::class);
$this->app->make(\App\ILayoutDriver::class)->read();
}
}

56
app/Screen.php

@ -0,0 +1,56 @@
<?php
namespace App;
class Screen
{
protected $_outputs;
protected $_driver;
public function __construct(array $outputs, ILayoutDriver $driver)
{
$this->_outputs = $outputs;
$this->_driver = $driver;
}
protected function getOutputName(): ?string
{
foreach ($this->_outputs as $output)
{
if ($this->_driver->isConnected($output))
return $output;
}
return null;
}
public function isConnected(): bool
{
return !empty($this->getOutputName());
}
public function isPrimary(): bool
{
return $this->_driver->isPrimary($this->getOutputName());
}
public function getDimensions(): array
{
return $this->_driver->getDimensions($this->getOutputName());
}
public function getOffset(): array
{
return $this->_driver->getOffset($this->getOutputName());
}
public function setOffset(int $x, int $y): void
{
$this->_driver->setOffset($this->getOutputName(), $x, $y);
}
public function setDimensions(int $w, int $h): void
{
$this->_driver->setDimensions($this->getOutputName(), $w, $h);
}
}

93
app/System/Command.php

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\System;
class Command
{
protected $command;
protected $defaultArguments;
protected $path;
public function __construct(string $command, array $defaultArguments = [])
{
$this->command = $command;
$this->defaultArguments = $defaultArguments;
$this->path = static::find($command);
if (!isset($this->path))
{
throw new \Exception('Command not found: '.$command);
}
}
public function getPath()
{
return $this->path;
}
public function create(array $arguments)
{
$arguments = array_merge($this->defaultArguments, $arguments);
foreach ($arguments as &$argument)
{
if (!is_object($argument) || !($argument instanceof CommandArgument))
{
$argument = new CommandArgument($argument);
}
}
$command = [];
if (strpos($this->path, ' ') === false)
{
$command[] = '"'.$this->path.'"';
}
else
{
$command[] = $this->path;
}
$command = array_merge($command, $arguments);
$process = app()->make('App\\System\\Process', ['command' => $command]);
$process->setStartModeSync();
return $process;
}
public function __invoke(array $arguments)
{
return $this->create($arguments)->start()->getStdout();
}
protected static function find($command)
{
if (PHP_OS == 'WINNT')
{
$result = shell_exec('where '.escapeshellarg($command));
// No idea why Windows is using unix line endings here
// Trim the trailing newline, last result is always empty
$result = explode("\n", trim($result));
$result = end($result);
}
else
{
$result = shell_exec('command -v '.escapeshellarg($command));
}
if (!is_string($result))
{
return;
}
$result = trim($result);
if (empty($result))
{
return;
}
return $result;
}
}

15
app/System/Command/Xrandr.php

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\System\Command;
use App\System\Command;
class Xrandr extends Command
{
public function __construct()
{
parent::__construct('xrandr');
}
}

25
app/System/CommandArgument.php

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\System;
class CommandArgument
{
protected $value;
public function __construct($val)
{
$this->value = $val;
}
public function get()
{
return escapeshellarg($this->value);
}
public function __toString()
{
return escapeshellarg($this->value);
}
}

585
app/System/Process.php

@ -0,0 +1,585 @@
<?php
declare(strict_types=1);
namespace App\System;
class Process
{
/**
* The full command path to execute.
*
* This may include raw strings as well as instances of CommandArgument. When
* executing, the instances will be resolved with the CommandArgument::get()
* method. When printing for errors/logs/etc, it will instead be cast to string.
*
* @var array
*/
protected $command;
/**
* Whether to raise an exception if the process exits with a non-zero (failure)
* exit code.
*
* @var bool
*/
protected $raiseExceptionOnFailure = true;
/**
* An array of callbacks to call with each line of output.
*
* Each element should be an array with two elements. The first is a callable.
* The second is optional and may contain a preg-compatible regular expression.
* If this is set, then only lines matching this regular expression will trigger
* the callback and each invocation will include the matches array.
*
* @var array[][callable,string|null]
*/
protected $outputCallbacks = [];
/**
* An array of callbacks to call when the process is stopping.
*
* @var array[callable]
*/
protected $exitCallback = [];
/**
* How to start the process -- one of the START_MODE constants.
*
* @var int
*/
protected $startMode = self::START_MODE_ASYNC;
/**
* Start the process asynchronously. start() will return immediately.
*
* This uses PHP's tick functions to continue pumping the stdout/stderr
* streams and monitoring the process status.
*/
protected const START_MODE_ASYNC = 0x01;
/**
* Start the process sychronously. start() will return once the process
* exits.
*
* This uses stream_select to monitor for output on stdout/stderr and calls
* tick() to pump the streams and check the process status whenever that
* changes.
*/
protected const START_MODE_SYNC = 0x02;
/**
* Start the process bound to the parent process's streams.
*/
protected const START_MODE_REPLACE = 0x04;
/**
* Override the working directory of the spawned process.
*
* @var string|null
*/
protected $cwd;
/**
* Associative array of additional environment variables to set for the
* child process.
*
* @var array[string]
*/
protected $env = [];
/**
* The process's current status represented by one of the STATUS_ constants.
*
* @var int
*/
protected $status = self::STATUS_CREATED;
/**
* A new Process object has been created. The handle has not been opened.
*/
protected const STATUS_CREATED = 0x01;
/**
* The process is starting.
*/
protected const STATUS_STARTING = 0x02;
/**
* The process has been started and is now running.
*/
protected const STATUS_RUNNING = 0x04;
/**
* It looks like the process has exited. We'll call the exit callbacks.
*/
protected const STATUS_STOPPING = 0x05;
/**
* All exit callbacks have been completed.
*/
protected const STATUS_STOPPED = 0x06;
/**
* The process has exited, we've completed all our callbacks and
* unregistered the tick callback if applicable.
*/
protected const STATUS_EXITED = 0x07;
/**
* The process handle returned from proc_open.
* @var resource
*/
protected $handle;
/**
* The process's stdin stream.
*/
protected $stream_stdin;
/**
* The process's stdout stream.
*/
protected $stream_stdout;
/**
* The process's stderr stream.
*/
protected $stream_stderr;
/**
* All of the process's stdout output when not running in replace mode.
* @var string
*/
protected $stdout = null;
/**
* All of the process's stderr output when not running in replace mode.
* @var string
*/
protected $stderr = null;
/**
* When registering an output callback, bitwise constant to specify to
* listen to stdout output.
*
* @var int
*/
public const STREAM_STDOUT = 1;
/**
* When registering an output callback, bitwise constant to specify to
* listen to stderr output.
*
* @var int
*/
public const STREAM_STDERR = 2;
/**
* The process's exit status once it has exited.
* @var int|null
*/
protected $exitStatus = null;
public function __construct(array $command)
{
$this->command = $command;
}
/**
* Set an environment variable to pass to the child process.
*
* @param string $key
* @param string|null $value
* @return Process
*/
public function setEnv($key, $value)
{
if (isset($value))
{
$this->env[$key] = $value;
}
else
{
unset($this->env[$key]);
}
return $this;
}
/**
* Set multiple environment variables from an associative array.
*
* @param array $values
* @return Process
*/
public function setEnvRange(array $values)
{
foreach ($values as $k => $v)
{
$this->setEnv($k, $v);
}
return $this;
}
/**
* Whether to raise an exception if the process returns a non-zero exit
* code.
*
* @param bool $raiseException true to raise an exception
* @return void
*/
public function setRaiseExceptionOnFailure($raiseException)
{
$this->raiseExceptionOnFailure = $raiseException;
return $this;
}
/**
* Add an output callback to receive process stdout and stderr output.
*
* Signature is function(Process $process, string $line, array $matches = null)
*
* @param callable $callback the callback to call
* @param string $filter regex to use to filter lines before calling callback
* @param mixed $streams
* @return void
*/
public function addOutputCallback(callable $callback, $filter = null, $streams = Process::STREAM_STDOUT)
{
$this->outputCallbacks[] = [$callback, $filter, $streams];
return $this;
}
/**
* Add a callback to be called when the process transitions into stopping.
*
* @param callable $callback
* @return void
*/
public function addExitCallback(callable $callback)
{
$this->exitCallback[] = $callback;
return $this;
}
public function setStartModeSync()
{
$this->startMode = static::START_MODE_SYNC;
return $this;
}
public function setStartModeAsync()
{
$this->startMode = static::START_MODE_ASYNC;
return $this;
}
public function setStartModeReplace()
{
$this->startMode = static::START_MODE_SYNC | static::START_MODE_REPLACE;
return $this;
}
public function getStdout()
{
return $this->stdout;
}
public function getStdoutString()
{
return isset($this->stdout) ? implode(PHP_EOL, $this->stdout) : null;
}
public function getStderr()
{
return $this->stderr;
}
public function getStderrString()
{
return isset($this->stderr) ? implode(PHP_EOL, $this->stderr) : null;
}
public function getExitStatus()
{
return $this->exitStatus;
}
/**
* Check whether the process is still running. This will return false
* during the times it is transitioning to starting or into exiting.
*
* @return bool true if running
*/
public function isRunning()
{
return $this->status == static::STATUS_RUNNING;
}
public function fork()
{
if (PHP_OS == 'WINNT')
{
$command = sprintf(
'start /B %s',
$this->getCommand()
);
}
else
{
$command = sprintf(
'nohup %s > /dev/null 2>&1 &',
$this->getCommand()
);
}
shell_exec($command);
}
public function start()
{
$this->status = static::STATUS_STARTING;
if ($this->startMode & static::START_MODE_REPLACE)
{
$descriptors = [
0 => STDIN,
1 => STDOUT,
2 => STDERR
];
}
else
{
$descriptors = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
}
$pipes = null;
if (PHP_OS == 'WINNT')
{
$commandWrapper = '"';
}
else
{
$commandWrapper = '';
}
$this->handle = $handle = proc_open(
$commandWrapper.$this->getCommand().$commandWrapper,
$descriptors,
$pipes,
$this->cwd,
array_merge(getenv(), $this->env)
);
if (!is_resource($handle))
{
throw new \Exception('Failed to start process');
}
if (sizeof($pipes))
{
$this->stream_stdin = $pipes[0];
$this->stream_stdout = $pipes[1];
$this->stream_stderr = $pipes[2];
stream_set_blocking($pipes[0], false);
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
}
$this->status = static::STATUS_RUNNING;
if ($this->startMode & static::START_MODE_ASYNC)
{
declare(ticks=1);
register_tick_function([&$this, 'tick']);
}
elseif ($this->startMode & static::START_MODE_SYNC)
{
while ($this->status != static::STATUS_EXITED)
{
if ($this->startMode & static::START_MODE_REPLACE)
{
sleep(1);
}
else
{
$read_streams = [$pipes[1], $pipes[2]];
$write_streams = null;
$except_streams = null;
stream_select($read_streams, $write_streams, $except_streams, 1);
}
$this->tick();
}
}
return $this;
}
public function stop()
{
proc_terminate($this->handle);
return $this;
}
protected function tick()
{
if (!($this->startMode & static::START_MODE_REPLACE))
{
$this->tickStreams();
}
$this->tickProcessStatus();
}
protected function tickStreams()
{
// Pump streams if necessary
if (!isset($this->stdout) || !is_array($this->stdout))
{
$this->stdout = [];
}
while ($line = fgets($this->stream_stdout))
{
// This array is initialized a few lines above and not set anywhere else.
// @phan-suppress-next-line PhanTypeMismatchDimEmpty
$this->stdout[] = trim($line);
foreach ($this->outputCallbacks as $callback)
{
if (($callback[2] & static::STREAM_STDOUT) == 0)
{
continue;
}
if (isset($callback[1]))
{
if (preg_match($callback[1], $line, $matches))
{
call_user_func($callback[0], $this, $line, $matches);
}
}
else
{
call_user_func($callback[0], $this, $line);
}
}
}
if (!isset($this->stderr) || !is_array($this->stderr))
{
$this->stderr = [];
}
while ($line = fgets($this->stream_stderr))
{
// This array is initialized a few lines above and not set anywhere else.
// @phan-suppress-next-line PhanTypeMismatchDimEmpty
$this->stderr[] = trim($line);
foreach ($this->outputCallbacks as $callback)
{
if (($callback[2] & static::STREAM_STDERR) == 0)
{
continue;
}
if (isset($callback[1]))
{
if (preg_match($callback[1], $line, $matches))
{
call_user_func($callback[0], $this, $line, $matches);
}
}
else
{
call_user_func($callback[0], $this, $line);
}
}
}
}
protected function tickProcessStatus()
{
// Check current status
$status = proc_get_status($this->handle);
if (isset($status['exitcode']) && $status['exitcode'] !== -1)
{
$this->exitStatus = $status['exitcode'];
}
if ($status['running'] === false && $this->status == static::STATUS_RUNNING)
{
$this->stopping();
}
elseif ($this->status == static::STATUS_STOPPED)
{
$this->status = static::STATUS_EXITED;
if ($this->startMode & static::START_MODE_ASYNC)
{
unregister_tick_function([&$this, 'tick']);
}
$this->exited();
}
}
protected function stopping()
{
$this->status = static::STATUS_STOPPING;
foreach ($this->exitCallback as $exitCallback)
{
call_user_func($exitCallback, $this);
}
$this->status = static::STATUS_STOPPED;
}
protected function exited()
{
if (!($this->startMode & static::START_MODE_REPLACE))
{
fclose($this->stream_stdin);
fclose($this->stream_stdout);
fclose($this->stream_stderr);
}
proc_close($this->handle);
if ($this->raiseExceptionOnFailure && $this->exitStatus != 0)
{
throw new \Exception('Command execution failed ('.$this->exitStatus.'): '.implode(' ', $this->command).PHP_EOL.
'Stdout:'.PHP_EOL.
$this->getStdoutString().PHP_EOL.
'Stderr:'.PHP_EOL.
$this->getStderrString().PHP_EOL);
}
}
public function write($data)
{
fwrite($this->stream_stdin, $data);
return $this;
}
public function close()
{
fclose($this->stream_stdin);
return $this;
}
protected function getCommand()
{
$processed = [];
$command = $this->command;
$commandName = array_shift($command);
$processed[] = strpos($commandName, ' ') === false ? $commandName : '"'.$commandName.'"';
foreach ($command as $part)
{
if (is_object($part) && $part instanceof CommandArgument)
{
$processed[] = $part->get();
}
else
{
$processed[] = $part;
}
}
return implode(' ', $processed);
}
}

18
app/System/RawCommandArgument.php

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\System;
class RawCommandArgument extends CommandArgument
{
public function get()
{
return $this->value;
}
public function __toString()
{
return $this->value;
}
}

13
app/System/SensitiveCommandArgument.php

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\System;
class SensitiveCommandArgument extends CommandArgument
{
public function __toString()
{
return '<'.escapeshellarg(str_repeat('*', strlen($this->value))).'>';
}
}

50
bootstrap/app.php

@ -0,0 +1,50 @@
<?php
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new LaravelZero\Framework\Application(
dirname(__DIR__)
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
LaravelZero\Framework\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
Illuminate\Foundation\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

18
box.json

@ -0,0 +1,18 @@
{
"chmod": "0755",
"directories": [
"app",
"bootstrap",
"config",
"vendor"
],
"files": [
"composer.json"
],
"exclude-composer-files": false,
"compression": "GZ",
"compactors": [
"KevinGH\\Box\\Compactor\\Php",
"KevinGH\\Box\\Compactor\\Json"
]
}

52
composer.json

@ -0,0 +1,52 @@
{
"name": "monitor_layout",
"description": "Tool to handle screen layout/relayout within X",
"homepage": "https://adampippin.ca",
"type": "project",
"license": "proprietary",
"support": {
"issues": "https://git.nucleardog.ca/nucleardog/monitor_layout/issues",
"source": "https://git.nucleardog.ca/nucleardog/monitor_layout"
},
"authors": [
{
"name": "Adam Pippin",
"email": "hello@adampippin.ca"
}
],
"require": {
"php": "^7.2.0",
"laravel-zero/framework": "^7.0",
"symfony/yaml": "^5.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"mockery/mockery": "^1.3.1",
"phan/phan": "^2.7",
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"scripts": {
"post-create-project-cmd": [
"@php application app:rename"
],
"post-autoload-dump": "cp .githooks/* .git/hooks"
},
"minimum-stability": "dev",
"prefer-stable": true,
"bin": ["monitor_layout"]
}

5109
composer.lock

File diff suppressed because it is too large

61
config/app.php

@ -0,0 +1,61 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Application Name
|--------------------------------------------------------------------------
|
| This value is the name of your application. This value is used when the
| framework needs to place the application's name in a notification or
| any other location as required by the application or its packages.
|
*/
'name' => 'Monitor_layout',
/*
|--------------------------------------------------------------------------
| Application Version
|--------------------------------------------------------------------------
|
| This value determines the "version" your application is currently running
| in. You may want to follow the "Semantic Versioning" - Given a version
| number MAJOR.MINOR.PATCH when an update happens: https://semver.org.
|
*/
'version' => app('git.version'),
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. This can be overridden using
| the global command line "--env" option when calling commands.
|
*/
'env' => 'development',
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
App\Providers\AppServiceProvider::class,
App\Providers\LayoutServiceProvider::class
],
];

80
config/commands.php

@ -0,0 +1,80 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Command
|--------------------------------------------------------------------------
|
| Laravel Zero will always run the command specified below when no command name is
| provided. Consider update the default command for single command applications.
| You cannot pass arguments to the default command because they are ignored.
|
*/
'default' => NunoMaduro\LaravelConsoleSummary\SummaryCommand::class,
/*
|--------------------------------------------------------------------------
| Commands Paths
|--------------------------------------------------------------------------
|
| This value determines the "paths" that should be loaded by the console's
| kernel. Foreach "path" present on the array provided below the kernel
| will extract all "Illuminate\Console\Command" based class commands.
|
*/
'paths' => [app_path('Commands')],
/*
|--------------------------------------------------------------------------
| Added Commands
|--------------------------------------------------------------------------
|
| You may want to include a single command class without having to load an
| entire folder. Here you can specify which commands should be added to
| your list of commands. The console's kernel will try to load them.
|
*/
'add' => [
// ..
],
/*
|--------------------------------------------------------------------------
| Hidden Commands
|--------------------------------------------------------------------------
|
| Your application commands will always be visible on the application list
| of commands. But you can still make them "hidden" specifying an array
| of commands below. All "hidden" commands can still be run/executed.
|
*/
'hidden' => [
NunoMaduro\LaravelConsoleSummary\SummaryCommand::class,
Symfony\Component\Console\Command\HelpCommand::class,
Illuminate\Console\Scheduling\ScheduleRunCommand::class,
Illuminate\Console\Scheduling\ScheduleFinishCommand::class,
Illuminate\Foundation\Console\VendorPublishCommand::class,
],
/*
|--------------------------------------------------------------------------
| Removed Commands
|--------------------------------------------------------------------------
|
| Do you have a service provider that loads a list of commands that
| you don't need? No problem. Laravel Zero allows you to specify
| below a list of commands that you don't to see in your app.
|
*/
'remove' => [
// ..
],
];

53
monitor_layout

@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
$autoloader = require file_exists(__DIR__.'/vendor/autoload.php') ? __DIR__.'/vendor/autoload.php' : __DIR__.'/../../autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);

24
phpunit.xml.dist

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
</phpunit>

42
sample.yaml

@ -0,0 +1,42 @@
screens:
hdmi:
output: HDMI-1-2
touchpad:
output: HDMI-1-1
internal:
output: eDP-1-1
usb:
output:
- DVI-I-2-1
- DVI-I-2-2
- DVI-I-2-3
- DVI-I-2-4
- DVI-I-2-5
- DVI-I-2-6
layouts:
all_screens:
screens:
hdmi: null
touchpad: null
internal: null
usb:
width: 1920
height: 1080
links:
- display: usb
right_of: internal
- display: hdmi
above: usb
- display: touchpad
below: internal
no_usb:
screens:
hdmi: null
touchpad: null
internal: null
links:
- display: hdmi
right_of: internal
- display: touchpad
below: internal

22
tests/CreatesApplication.php

@ -0,0 +1,22 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}

20
tests/Feature/InspiringCommandTest.php

@ -0,0 +1,20 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
class InspiringCommandTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testInspiringCommand()
{
$this->artisan('inspiring')
->expectsOutput('Simplicity is the ultimate sophistication.')
->assertExitCode(0);
}
}

10
tests/TestCase.php

@ -0,0 +1,10 @@
<?php
namespace Tests;
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}

18
tests/Unit/ExampleTest.php

@ -0,0 +1,18 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
Loading…
Cancel
Save