A variety of simple automation scripts and enhancements to Sway and i3.
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.

80 lines
2.9KB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Workspace to push the terminal to when dismissed
  5. my $hidden = 'grave';
  6. # Terminal application used for pop-up (swaymsg 'name')
  7. my $term = 'uxterm';
  8. use JSON::XS;
  9. my $json = JSON::XS->new();
  10. my $raw = join("\n",`swaymsg -t get_tree`);
  11. my $root = $json->decode($raw);
  12. # Get focused
  13. my $display = $root->{focus}->[0];
  14. my ($workspace, $term_id, $term_ws);
  15. foreach my $d (@{$root->{nodes}}) {
  16. # If both the current workspace and terminal have been found
  17. # nothing else to look for
  18. if (defined($workspace) && defined($term_id)) {
  19. last;
  20. # If the display from this iteration of the loop is in focus
  21. # look for the active workspace
  22. } elsif ($d->{id} eq $display) {
  23. foreach my $w (@{$d->{nodes}}) {
  24. # Again, if both found, skip
  25. if (defined($workspace) && defined($term_id)) {
  26. last;
  27. # Otherwise if the current workspace is active, mark it
  28. } elsif ($w->{id} eq $d->{focus}->[0]) {
  29. $workspace = $w->{name};
  30. }
  31. # In any case, look for the terminal app
  32. foreach my $n (@{$w->{floating_nodes}}) {
  33. if ($n->{name} eq $term) {
  34. $term_id = $n->{id};
  35. $term_ws = $w->{name};
  36. last;
  37. }
  38. }
  39. }
  40. # All other displays only need to be quickly searched for the term_id
  41. } else {
  42. foreach my $w (@{$d->{nodes}}) {
  43. if (defined($term_id)) {
  44. last;
  45. }
  46. foreach my $n (@{$w->{floating_nodes}}) {
  47. if ($n->{name} eq $term) {
  48. $term_id = $n->{id};
  49. $term_ws = $w->{name};
  50. last;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. print "Active workspace = $workspace\n"
  57. . "Terminal ID = $term_id\n"
  58. . "Terminal WS = $term_ws\n";
  59. # If there is no terminal found, spawn one
  60. if (!defined($term_id)) {
  61. print "No term running, starting one\n";
  62. exec $term
  63. # If the current workspace is known and terminal isn't on it, bring and focus
  64. } elsif ($workspace != $term_ws) {
  65. print "Term not on current workspace, bringing it\n";
  66. `swaymsg "[con_id=$term_id]" move workspace $workspace`;
  67. `swaymsg "[con_id=$term_id]" focus`;
  68. # Otherwise hide it from whereever it is
  69. } else {
  70. print "Term is on current workspace or lost, moving to $hidden\n";
  71. `swaymsg "[con_id=$term_id]" move workspace $hidden`;
  72. }