scripts/sway/popup-term.pl

82 lines
2.6 KiB
Perl
Raw Normal View History

2020-09-11 15:00:39 +00:00
#!/usr/bin/perl
use strict;
use warnings;
# Workspace to push the terminal to when dismissed
my $hidden = 'grave';
# Terminal application used for pop-up (swaymsg 'name')
my $term = 'uxterm';
use JSON::XS;
my $json = JSON::XS->new();
my $raw = join("\n",`swaymsg -t get_tree`);
my $root = $json->decode($raw);
# Get focused
my $display = $root->{focus}->[0];
my ($workspace, $term_id, $term_ws);
foreach my $d (@{$root->{nodes}}) {
2023-01-06 19:41:31 +00:00
# If both the current workspace and terminal have been found
# nothing else to look for
if (defined($workspace) && defined($term_id)) {
last;
# If the display from this iteration of the loop is in focus
# look for the active workspace
} elsif ($d->{id} eq $display) {
foreach my $w (@{$d->{nodes}}) {
# Again, if both found, skip
if (defined($workspace) && defined($term_id)) {
2020-09-11 15:00:39 +00:00
last;
2023-01-06 19:41:31 +00:00
# Otherwise if the current workspace is active, mark it
} elsif ($w->{id} eq $d->{focus}->[0]) {
$workspace = $w->{name};
}
# In any case, look for the terminal app
foreach my $n (@{$w->{floating_nodes}}) {
if ($n->{name} eq $term) {
$term_id = $n->{id};
$term_ws = $w->{name};
last;
2020-09-11 15:00:39 +00:00
}
2023-01-06 19:41:31 +00:00
}
}
# All other displays only need to be quickly searched for the term_id
} else {
foreach my $w (@{$d->{nodes}}) {
if (defined($term_id)) {
last;
}
foreach my $n (@{$w->{floating_nodes}}) {
if ($n->{name} eq $term) {
$term_id = $n->{id};
$term_ws = $w->{name};
last;
2020-09-11 15:00:39 +00:00
}
2023-01-06 19:41:31 +00:00
}
2020-09-11 15:00:39 +00:00
}
2023-01-06 19:41:31 +00:00
}
2020-09-11 15:00:39 +00:00
}
2020-11-23 09:35:26 +00:00
print "Active workspace = $workspace\n"
2023-01-06 19:41:31 +00:00
. "Terminal ID = $term_id\n"
. "Terminal WS = $term_ws\n";
2020-09-11 15:00:39 +00:00
# If there is no terminal found, spawn one
if (!defined($term_id)) {
2023-01-06 19:41:31 +00:00
print "No term running, starting one\n";
exec $term
2020-11-23 09:35:26 +00:00
# If the current workspace is known and terminal isn't on it, bring and focus
2020-09-11 15:00:39 +00:00
} elsif ($workspace != $term_ws) {
2023-01-06 19:41:31 +00:00
print "Term not on current workspace, bringing it\n";
`swaymsg "[con_id=$term_id]" move workspace $workspace`;
`swaymsg "[con_id=$term_id]" focus`;
`swaymsg "[con_id=$term_id]" sticky enable`;
2020-09-11 15:00:39 +00:00
# Otherwise hide it from whereever it is
} else {
2023-01-06 19:41:31 +00:00
print "Term is on current workspace or lost, moving to $hidden\n";
`swaymsg "[con_id=$term_id]" sticky disable`;
`swaymsg "[con_id=$term_id]" move workspace $hidden`;
2020-09-11 15:00:39 +00:00
}