summaryrefslogtreecommitdiff
path: root/windowpos.pl
blob: d070f4a04328a23362d8e34532f478a3fbc7d256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
#
# save/restore the workspace location of every window
#
# TODO? restore geometry (not needed for tiling wm though)
#

use strict;
use warnings;

use Getopt::Long;
use JSON;

my $filename = "$ENV{HOME}/.windowpositions.json";

sub save_windows {
    my $windows = {};

    foreach my $line (split('\n', `wmctrl -l`)) {
        if ($line =~ m/^([0-9a-fx]+)\s+(\d+)\s+/) {
            $windows->{$1} = $2;
        }
    }

    open FILE, ">$filename" or die "Can't store positions.";
    print FILE encode_json($windows);
    close FILE;
}

sub restore_windows {
    my $desk_count = `wmctrl -d | wc -l`;

    open FILE, "<$filename" or die "No positions stored.";
    my $windows = decode_json(<FILE>);
    close FILE;

    foreach my $win (keys %$windows) {
        my $desk = $windows->{$win} % $desk_count;
        `wmctrl -i -r $win -t $desk`;
    }
}

GetOptions(
    'save' => \&save_windows,
    'restore' => \&restore_windows,
    'help' => sub { print "Usage: $0 <--save|--restore>\n"; },
);