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')
|
2023-04-10 01:40:34 +00:00
|
|
|
my $term = 'alacritty';
|
|
|
|
# App ID
|
|
|
|
my $class = 'Alacritty-grave';
|
2020-09-11 15:00:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
# 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";
|
2023-04-10 01:40:34 +00:00
|
|
|
`$term --config-file $ENV{HOME}/.dotfiles/alacritty/grave.yml --class $class`;
|
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
|
|
|
}
|