My i3 configurations and scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i3_switch_focus_container.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/php5
  2. <?php
  3. function getTree()
  4. {
  5. return json_decode(shell_exec('i3-msg -t get_tree'));
  6. }
  7. function getFocusedWindowContainer($tree)
  8. {
  9. if (empty($tree->nodes)) {
  10. return null;
  11. }
  12. foreach ($tree->nodes as $k => $container) {
  13. if (true === $container->focused) {
  14. return $tree->nodes;
  15. }
  16. if (null !== $container = getFocusedWindowContainer($container)) {
  17. return $container;
  18. }
  19. }
  20. return null;
  21. }
  22. function getFocusWindowNumber($default, $max)
  23. {
  24. global $argv;
  25. if (!isset($argv[1])) {
  26. $argv[1] = $default;
  27. }
  28. return min($max, max(1, $argv[1])) - 1;
  29. }
  30. function getDefaultFocusWindowNumber($container)
  31. {
  32. foreach ($container as $k => $v) {
  33. if ($v->focused) {
  34. return $k + 1;
  35. }
  36. }
  37. }
  38. function setFocusToWindow($window)
  39. {
  40. shell_exec(sprintf("i3-msg '[con_id=\"%s\"] focus'", $window->id));
  41. }
  42. $container = getFocusedWindowContainer(getTree());
  43. if ($container !== null) {
  44. $number = getFocusWindowNumber(getDefaultFocusWindowNumber($container), count($container));
  45. foreach ($container as $k => $window) {
  46. if ($k == $number) {
  47. setFocusToWindow($window);
  48. }
  49. }
  50. }