Skip to content
Snippets Groups Projects

dmenu-activate-window

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Kjetil Torgrim Homme
    Edited
    dmenu-activate-window.pl 1.21 KiB
    #! /usr/bin/perl
    
    use warnings;
    use strict;
    use JSON;
    use IPC::Open3;
    
    sub recurse {
        my ($node) = @_;
        my @titles;
        if (ref($node) eq 'ARRAY') {
            for my $n (@{$node}) {
                push(@titles, recurse($n));
            }
        } else {
            if ($node->{type} && $node->{type} eq 'con' &&
                $node->{window_type} && $node->{name}) {
                my $geo = $node->{geometry};
                push(@titles, [$node->{name},
                               $node->{window},
                               int($geo->{x} + $geo->{width}/2),
                               int($geo->{y} + $geo->{height}/2)],
                    );
            }
            if ($node->{nodes}) {
                push(@titles, recurse($node->{nodes}));
            }
        }
        return @titles;
    }
    
    my $tree = JSON->new->decode(`i3-msg -t get_tree`);
    my %windows = map { $_->[0] => $_ } recurse($tree->{nodes});
    
    my $pid = open3(my $chld_in, my $chld_out, undef,
                    'dmenu', '-i');
    print $chld_in "\n", join("\n", sort keys %windows), "\n";
    close($chld_in);
    
    my $choice = <$chld_out>;
    
    if ($choice && $choice ne "\n") {
        chomp($choice);
        my $win = $windows{$choice};
        system('wmctrl', '-i', '-a', $win->[1]);
    } else {
        # print "No windows chosen\n";
    }
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment