87 lines
1.8 KiB
PHP
87 lines
1.8 KiB
PHP
|
#!/usr/bin/php
|
||
|
<?php
|
||
|
|
||
|
function getWorkspaces(): array
|
||
|
{
|
||
|
$output = shell_exec('i3-msg -t get_workspaces');
|
||
|
|
||
|
return json_decode($output, true);
|
||
|
}
|
||
|
|
||
|
function getVisibleWorkspaces(): array
|
||
|
{
|
||
|
$workspaces = [];
|
||
|
|
||
|
foreach (getWorkspaces() as $wp) {
|
||
|
if (true === $wp['visible']) {
|
||
|
$workspaces[] = $wp;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $workspaces;
|
||
|
}
|
||
|
|
||
|
function getTree(): array
|
||
|
{
|
||
|
$output = shell_exec('i3-msg -t get_tree');
|
||
|
|
||
|
return json_decode($output, true);
|
||
|
}
|
||
|
|
||
|
function getVisibleWorkspacesNodes(array $tree, array $workspaces): array
|
||
|
{
|
||
|
$nodes = [];
|
||
|
$num = $tree['num'] ?? null;
|
||
|
$output = $tree['output'] ?? null;
|
||
|
|
||
|
foreach ($workspaces as $workspace) {
|
||
|
if ($num === $workspace['num']) {
|
||
|
$nodes = array_merge(
|
||
|
$nodes,
|
||
|
$tree['nodes']
|
||
|
);
|
||
|
|
||
|
$nodes = array_merge(
|
||
|
$nodes,
|
||
|
$tree['floating_nodes']
|
||
|
);
|
||
|
} elseif ($output === '__i3' && isset($tree['window'])) {
|
||
|
$nodes[] = $tree;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($tree as $node) {
|
||
|
if (is_array($node)) {
|
||
|
$nodes = array_merge(
|
||
|
$nodes,
|
||
|
getVisibleWorkspacesNodes($node, $workspaces)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach ($nodes as $k => $n) {
|
||
|
if (!isset($n['window'])) {
|
||
|
unset($nodes[$k]);
|
||
|
|
||
|
foreach ($n['nodes'] as $node) {
|
||
|
$nodes[] = $node;
|
||
|
}
|
||
|
|
||
|
foreach ($n['floating_nodes'] as $node) {
|
||
|
$nodes[] = $node;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $nodes;
|
||
|
}
|
||
|
|
||
|
$nodes = getVisibleWorkspacesNodes(getTree(), getVisibleWorkspaces());
|
||
|
|
||
|
foreach ($nodes as $key => $node) {
|
||
|
if ($node['focused']) {
|
||
|
shell_exec(sprintf('i3-msg "[id=%s] focus"', $nodes[(++$key) % count($nodes)]['window']));
|
||
|
}
|
||
|
}
|