From 15c96f8b5ded9f4b67257b29bdc9d623aae532e4 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 23 Feb 2024 04:24:12 -0500 Subject: move a couple scripts to more specific packages --- i3/.bin/kill-focused | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 i3/.bin/kill-focused (limited to 'i3/.bin/kill-focused') diff --git a/i3/.bin/kill-focused b/i3/.bin/kill-focused new file mode 100755 index 0000000..37271ac --- /dev/null +++ b/i3/.bin/kill-focused @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.014; + +use JSON::PP; + +my ($sig) = @ARGV; +my $pid = get_pid(); +my $pgrp = get_pgrp($pid); +kill $sig, -$pgrp; + +sub get_pid { + my $window_id = get_window_id(); + die "couldn't find window" unless defined $window_id; + my $prop = `xprop -id $window_id _NET_WM_PID`; + die "couldn't get pid from focused window" + if $prop =~ /not found/; + chomp($prop); + $prop =~ s/^_NET_WM_PID\(CARDINAL\) = //; + return $prop; +} + +sub get_pgrp { + my ($pid) = @_; + open my $stat_fh, '<', "/proc/$pid/stat" + or die "couldn't open /proc/$pid/stat"; + my $stat = do { local $/; <$stat_fh> }; + my @f = ($stat =~ /\([^)]*\)|[^ ]+/g); + return $f[4]; +} + +sub get_window_id { + return find(decode_json(`i3-msg -t get_tree`))->{window}; +} + +sub find { + my ($t) = @_; + if ($t->{focused}) { + return $t; + } + + for my $subtree (@{ $t->{nodes} }, @{ $t->{floating_nodes} }) { + my $found = find($subtree); + return $found if $found; + } + + return; +} -- cgit v1.2.3-54-g00ecf