A variety of simple automation scripts and enhancements to Sway and i3.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

76 lines
2.4KB

  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, nothing else to look for
  17. if (defined($workspace) && defined($term_id)) {
  18. last;
  19. # If the display from this iteration of the loop is in focus, look for the active workspace
  20. } elsif ($d->{id} eq $display) {
  21. foreach my $w (@{$d->{nodes}}) {
  22. # Again, if both found, skip
  23. if (defined($workspace) && defined($term_id)) {
  24. last;
  25. # Otherwise if the current workspace is active, mark it
  26. } elsif ($w->{id} eq $d->{focus}->[0]) {
  27. $workspace = $w->{name};
  28. }
  29. # In any case, look for the terminal app
  30. foreach my $n (@{$w->{floating_nodes}}) {
  31. if ($n->{name} eq $term) {
  32. $term_id = $n->{id};
  33. $term_ws = $w->{name};
  34. last;
  35. }
  36. }
  37. }
  38. # All other displays only need to be quickly searched for the term_id
  39. } else {
  40. foreach my $w (@{$d->{nodes}}) {
  41. if (defined($term_id)) {
  42. last;
  43. }
  44. foreach my $n (@{$w->{floating_nodes}}) {
  45. if ($n->{name} eq $term) {
  46. $term_id = $n->{id};
  47. $term_ws = $w->{name};
  48. last;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. print "Active workspace = $workspace\nTerminal ID = $term_id\nTerminal WS = $term_ws\n";
  55. # If there is no terminal found, spawn one
  56. if (!defined($term_id)) {
  57. print "No term running, starting one\n";
  58. exec $term
  59. # If the current workspace is known and terminal isn't on it, bring and give focus
  60. } elsif ($workspace != $term_ws) {
  61. print "Term not on current workspace, bringing it\n";
  62. `swaymsg "[con_id=$term_id]" move workspace $workspace`;
  63. `swaymsg "[con_id=$term_id]" focus`;
  64. # Otherwise hide it from whereever it is
  65. } else {
  66. print "Term is on current workspace or lost, moving to $hidden\n";
  67. `swaymsg "[con_id=$term_id]" move workspace $hidden`;
  68. }