64 lines
1.2 KiB
PHP
Executable file
64 lines
1.2 KiB
PHP
Executable file
#!/usr/bin/php
|
|
<?php
|
|
|
|
function getTree()
|
|
{
|
|
return json_decode(shell_exec('i3-msg -t get_tree'));
|
|
}
|
|
|
|
function getFocusedWindowContainer($tree)
|
|
{
|
|
if (empty($tree->nodes)) {
|
|
return null;
|
|
}
|
|
|
|
foreach ($tree->nodes as $k => $container) {
|
|
if (true === $container->focused) {
|
|
return $tree->nodes;
|
|
}
|
|
|
|
if (null !== $container = getFocusedWindowContainer($container)) {
|
|
return $container;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function getFocusWindowNumber($default, $max)
|
|
{
|
|
global $argv;
|
|
|
|
if (!isset($argv[1])) {
|
|
$argv[1] = $default;
|
|
}
|
|
|
|
return min($max, max(1, $argv[1])) - 1;
|
|
}
|
|
|
|
function getDefaultFocusWindowNumber($container)
|
|
{
|
|
foreach ($container as $k => $v) {
|
|
if ($v->focused) {
|
|
return $k + 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
function setFocusToWindow($window)
|
|
{
|
|
shell_exec(sprintf("i3-msg '[con_id=\"%s\"] focus'", $window->id));
|
|
}
|
|
|
|
$container = getFocusedWindowContainer(getTree());
|
|
|
|
if (null !== $container) {
|
|
$number = getFocusWindowNumber(getDefaultFocusWindowNumber($container), count($container));
|
|
|
|
foreach ($container as $k => $window) {
|
|
if ($k == $number) {
|
|
setFocusToWindow($window);
|
|
}
|
|
}
|
|
}
|