Compare commits

...

5 Commits

Author SHA1 Message Date
John Mertz c2cb64efea Possible missing '/' in path 2024-07-18 22:48:21 -06:00
John Mertz 242957774e Run restore_display.sh instead of individual commands 2024-07-18 22:47:42 -06:00
John Mertz 35755ecea7 Fix grave script!
TODO: Should probably convert to use AnyEvent::Sway
2024-07-18 22:46:46 -06:00
John Mertz 46247c9fa1 Script to set up plenv and then run displays.pl 2024-07-18 16:54:45 -06:00
John Mertz 0e737d410f Make Logout actually log out 2024-07-18 16:53:38 -06:00
6 changed files with 115 additions and 50 deletions

View File

@ -2,79 +2,128 @@
use strict;
use warnings;
use feature ( 'signatures' );
use JSON::MaybeXS;
my $json = JSON::MaybeXS->new();
our ($workspace, $term_id, $term_ws, $ww, $wh, $tw, $th, $tc);
# Desired terminal dimensions (px or %) and corner (top-right, top-left, bottom-right, bottom-left)
$tw = '100%';
$th = '30%';
$tc = 'bottom-right';
# Workspace to push the terminal to when dismissed
my $hidden = 'grave';
# Terminal application used for pop-up (swaymsg 'name')
# Terminal executable used for pop-up
my $term = 'alacritty';
# App ID
my $class = 'Alacritty-grave';
my $app_id = 'Alacritty-grave';
use JSON::XS;
my $json = JSON::XS->new();
sub search($nodes, $o=undef, $w=undef, $depth=0) {
my $debug = 0;
my $term_id;
$depth++;
my $prefix = " "x($depth*4);
foreach my $n (@{$nodes}) {
print("${prefix}Checking node $n->{id}".(defined($n->{name}) ? " ($n->{name})" : "")."\n") if ($debug);
print("${prefix}Considering $n->{app_id}...\n") if ($debug && defined($n->{app_id}));
if ($n->{type} eq "output") {
$o = $n->{name};
print("${prefix}Searching output $o\n") if ($debug);
} elsif ($n->{type} eq "workspace") {
$w = $n->{name};
print("${prefix}Searching workspace $w\n") if ($debug);
} else {
print("${prefix}Currently on display $o, workspace $w\n") if ($debug);
if (defined($n->{app_id}) && $n->{app_id} eq $app_id) {
$term_id = $n->{id};
$term_ws = $w;
print("${prefix}Found $app_id with id $term_id on workspace $w\n") if ($debug);
return $term_id;
}
}
$term_id = search($n->{floating_nodes}, $o, $w, $depth);
$term_id = search($n->{nodes}, $o, $w, $depth) if ($term_id == 0);
print("${prefix}Found $term_id on $w\n") if ($debug && $term_id);
return $term_id if ($term_id);
}
print("${prefix}Not found, moving up a node\n") if ($debug);
return 0;
}
sub resize() {
my ($d, $r, $h, $w) = (0, 0);
if (my ($hit) = $tw =~ m/^(100|\d\d?)\%$/) {
$w = int(($ww * $hit) / 100);
$r = $ww - $w if ($tc =~ m/right$/);
} elsif (($tw =~ m/^\d+$/) && (1 < $tw <= $ww)) {
$d = $wh - $th if ($tc =~ m/^bottom/);
}
if (my ($hit) = $th =~ m/^(100|\d\d?)\%$/) {
$h = int(($wh * $hit) / 100);
$d = $wh - $h if ($tc =~ m/^bottom/);
} elsif (($tw =~ m/^\d+$/) && (1 < $tw <= $ww)) {
$r = $ww - $tw if ($tc =~ m/right$/);
}
`swaymsg "[con_id=$term_id]" resize set width ${w}px height ${h}px, move position ${r}px ${d}px`;
}
my $raw = join("\n",`swaymsg -t get_tree`);
my $root = $json->decode($raw);
$term_id = search($root->{nodes}) || undef;
# Get focused
my $display = $root->{focus}->[0];
my ($workspace, $term_id, $term_ws);
my $focused = $root->{focus}->[0];
foreach my $d (@{$root->{nodes}}) {
# 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) {
last if (defined($workspace));
if ($d->{id} eq $focused) {
foreach my $w (@{$d->{nodes}}) {
# Again, if both found, skip
if (defined($workspace) && defined($term_id)) {
last;
# Otherwise if the current workspace is active, mark it
} elsif ($w->{id} eq $d->{focus}->[0]) {
if ($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;
}
}
}
# All other displays only need to be quickly searched for the term_id
} else {
foreach my $w (@{$d->{nodes}}) {
if (defined($term_id)) {
$ww = $w->{rect}->{width};
$wh = $w->{rect}->{height};
last;
}
foreach my $n (@{$w->{floating_nodes}}) {
if ($n->{name} eq $term) {
$term_id = $n->{id};
$term_ws = $w->{name};
last;
}
}
}
}
}
#print("Current workspace is $workspace ($ww x $wh)\n");
#print("Found terminal at node $term_id\n") if (defined($term_id));
#print("Found terminal on workspace $term_ws\n") if (defined($term_ws));
# If there is no terminal found, spawn one
my $resize = 0;
if (!defined($term_id)) {
print "No term running, starting one\n";
`$term --config-file $ENV{HOME}/.dotfiles/alacritty/grave.yml --class $class`;
my $pid = fork();
if ($pid) {
print "No term running, starting one with PID $pid\n";
sleep 1;
$raw = join("\n",`swaymsg -t get_tree`);
$root = $json->decode($raw);
$term_id = search($root->{nodes}) || undef;
die("Term not found after starting") unless (defined($term_id));
} else {
print("Starting in fork...\n");
`$term --config-file $ENV{HOME}/.dotfiles/alacritty/grave.toml --class $app_id & disown`;
print("Exitting\n");
exit(1);
}
resize();
}
# If the current workspace is known and terminal isn't on it, bring and focus
} elsif ($workspace != $term_ws) {
if (defined($term_ws) && $workspace ne $term_ws) {
print "Term not on current workspace, bringing it\n";
`swaymsg "[con_id=$term_id]" floating enable`;
resize();
`swaymsg "[con_id=$term_id]" sticky enable`;
`swaymsg "[con_id=$term_id]" move workspace $workspace`;
`swaymsg "[con_id=$term_id]" focus`;
`swaymsg "[con_id=$term_id]" sticky enable`;
# Otherwise hide it from whereever it is
} else {
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`;
`swaymsg "[con_id=$term_id]" floating disable`;
}

9
sway/restore_display.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
OLDPWD=$OLDPWD
PWD=$PWD
cd $HOME/scripts/sway/
source ~/.dotfiles/bash/plenv-path.sh
perl -Ilocal/lib/perl5/5.38.2 displays.pl
cd $OLDPWD
cd $PWD

9
sway/toggle_grave.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
OLDPWD=$OLDPWD
PWD=$PWD
cd $HOME/scripts/sway/
source ~/.dotfiles/bash/plenv-path.sh
perl -Ilocal/lib/perl5/5.38.2 popup-term.pl
cd $OLDPWD
cd $PWD

View File

@ -207,7 +207,7 @@ sub choose_image($self)
sub crop($self, $image, $ow, $oh)
{
my $cropped = $image;
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}$1-cropped.$2#;
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}/$1-cropped.$2#;
use Image::Magick;
my $im = Image::Magick->new();

View File

@ -1,6 +1,6 @@
#!/bin/bash
if [ -z ${SWAYSOCK+x} ]; then
i3lock -c 000000
i3 exit
else
swaylock -c 000000
sway exit
fi

View File

@ -3,7 +3,5 @@ if [ -z ${SWAYSOCK+x} ]; then
i3 reload
else
swaymsg reload
source ${HOME}/.dotfile/bash/bashrc
cd ${HOME}/scripts/sway
perl -Ilocal/lib/perl5/5.38.2 display.pl
${HOME}/scripts/sway/restore_display.sh
fi