This file is indexed.

/usr/bin/i3-save-tree is in i3-wm 4.8-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env perl
# vim:ts=4:sw=4:expandtab
#
# © 2013-2014 Michael Stapelberg
#
# Requires perl ≥ v5.10, AnyEvent::I3 and JSON::XS

use strict;
use warnings qw(FATAL utf8);
use Data::Dumper;
use IPC::Open2;
use POSIX qw(locale_h);
use File::Find;
use File::Basename qw(basename);
use File::Temp qw(tempfile);
use Getopt::Long;
use Pod::Usage;
use AnyEvent::I3;
use JSON::XS;
use List::Util qw(first);
use v5.10;
use utf8;
use open ':encoding(UTF-8)';

binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

my $workspace;
my $output;
my $result = GetOptions(
    'workspace=s' => \$workspace,
    'output=s' => \$output,
    'version' => sub {
        say "i3-save-tree 0.1 © 2013 Michael Stapelberg";
        exit 0;
    },
    'help' => sub {
        pod2usage(-exitval => 0);
    });

die "Could not parse command line options" unless $result;

if (!defined($workspace) && !defined($output)) {
    die "One of --workspace or --output need to be specified";
}

unless (defined($workspace) ^ defined($output)) {
    die "Only one of --workspace or --output can be specified";
}

my $i3 = i3();
if (!$i3->connect->recv) {
    die "Could not connect to i3";
}

sub filter_containers {
    my ($tree, $pred) = @_;

    $_ = $tree;
    return $tree if $pred->();

    for my $child (@{$tree->{nodes}}, @{$tree->{floating_nodes}}) {
        my $result = filter_containers($child, $pred);
        return $result if defined($result);
    }

    return undef;
}

sub leaf_node {
    my ($tree) = @_;

    return $tree->{type} eq 'con' &&
           @{$tree->{nodes}} == 0 &&
           @{$tree->{floating_nodes}} == 0;
}

my %allowed_keys = map { ($_, 1) } qw(
    type
    fullscreen_mode
    layout
    border
    current_border_width
    floating
    percent
    nodes
    floating_nodes
    name
    geometry
    window_properties
);

sub strip_containers {
    my ($tree) = @_;

    # layout is not relevant for a leaf container
    delete $tree->{layout} if leaf_node($tree);

    # fullscreen_mode conveys no state at all, it can either be 0 or 1 and the
    # default is _always_ 0, so skip noop entries.
    delete $tree->{fullscreen_mode} if $tree->{fullscreen_mode} == 0;

    # names for non-leafs are auto-generated and useful only for i3 debugging
    delete $tree->{name} unless leaf_node($tree);

    delete $tree->{geometry} if zero_rect($tree->{geometry});

    delete $tree->{current_border_width} if $tree->{current_border_width} == -1;

    for my $key (keys %$tree) {
        next if exists($allowed_keys{$key});

        delete $tree->{$key};
    }

    for my $key (qw(nodes floating_nodes)) {
        $tree->{$key} = [ map { strip_containers($_) } @{$tree->{$key}} ];
    }

    return $tree;
}

my $json_xs = JSON::XS->new->pretty(1)->allow_nonref->space_before(0)->canonical(1);

sub zero_rect {
    my ($rect) = @_;
    return $rect->{x} == 0 &&
           $rect->{y} == 0 &&
           $rect->{width} == 0 &&
           $rect->{height} == 0;
}

# Dumps the containers in JSON, but with comments to explain the user what she
# needs to fix.
sub dump_containers {
    my ($tree, $ws, $last) = @_;

    $ws //= "";

    say $ws . '{';

    $ws .= (' ' x 4);

    if (!leaf_node($tree)) {
        my $desc = $tree->{layout} . ' split container';
        if ($tree->{type} ne 'con') {
            $desc = $tree->{type};
        }
        say "$ws// $desc with " . @{$tree->{nodes}} . " children";
    }

    # Turn “window_properties” into “swallows” expressions, but only for leaf
    # nodes. It only makes sense for leaf nodes to swallow anything.
    if (leaf_node($tree)) {
        my $swallows = {};
        for my $property (keys %{$tree->{window_properties}}) {
            $swallows->{$property} = '^' . quotemeta($tree->{window_properties}->{$property}) . '$';
        }
        $tree->{swallows} = [ $swallows ];
    }
    delete $tree->{window_properties};

    my @keys = sort keys %$tree;
    for (0 .. (@keys-1)) {
        my $key = $keys[$_];
        # Those are handled recursively, not printed.
        next if $key eq 'nodes' || $key eq 'floating_nodes';

        # JSON::XS’s encode appends a newline
        chomp(my $val = $json_xs->encode($tree->{$key}));

        # Fix indentation. Keep in mind we are producing output to be
        # read/modified by a human.
        $val =~ s/^/$ws/mg;
        $val =~ s/^\s+//;

        # Comment out all swallows criteria, they are just suggestions.
        if ($key eq 'swallows') {
            $val =~ s,^(\s*)\s{3}",\1// ",gm;
        }

        # Append a comma unless this is the last value.
        # Ugly, but necessary so that we can print all values before recursing.
        my $comma = ($_ == (@keys-1) &&
                     @{$tree->{nodes}} == 0 &&
                     @{$tree->{floating_nodes}} == 0 ? '' : ',');
        say qq#$ws"$key": $val$comma#;
    }

    for my $key (qw(nodes floating_nodes)) {
        my $num = scalar @{$tree->{$key}};
        next if !$num;

        say qq#$ws"$key": [#;
        for (0 .. ($num-1)) {
            dump_containers(
                $tree->{$key}->[$_],
                $ws . (' ' x 4),
                ($_ == ($num-1)));
        }
        say qq#$ws]#;
    }

    $ws =~ s/\s{4}$//;

    say $ws . ($last ? '}' : '},');
}

my $tree = $i3->get_tree->recv;

my $dump;
if (defined($workspace)) {
    $dump = filter_containers($tree, sub {
        $_->{type} eq 'workspace' && $_->{name} eq $workspace
    });
} else {
    $dump = filter_containers($tree, sub {
        $_->{type} eq 'output' && $_->{name} eq $output
    });
    # Get the output’s content container (living beneath dockarea containers).
    $dump = first { $_->{type} eq 'con' } @{$dump->{nodes}};
}

$dump = strip_containers($dump);

say "// vim:ts=4:sw=4:et";
for my $key (qw(nodes floating_nodes)) {
    for (0 .. (@{$dump->{$key}} - 1)) {
        dump_containers($dump->{$key}->[$_], undef, 1);
        # Newlines separate containers so that one can use { and } in vim to
        # jump out of the current container.
        say '';
    }
}

=encoding utf-8

=head1 NAME

    i3-save-tree - save (parts of) the layout tree for restoring

=head1 SYNOPSIS

    i3-save-tree [--workspace=name] [--output=name]

=head1 DESCRIPTION

Dumps a workspace (or an entire output) to stdout. The data is supposed to be
edited a bit by a human, then later fed to i3 via the append_layout command.

The append_layout command will create placeholder windows, arranged in the
layout the input file specifies. Each container should have a swallows
specification. When a window is mapped (made visible on the screen) that
matches the specification, i3 will put it into that place and kill the
placeholder.

=head1 OPTIONS

=over

=item B<--workspace=name>

Specifies the workspace that should be dumped, e.g. 1. Either this or --output
need to be specified.

=item B<--output=name>

Specifies the output that should be dumped, e.g. LVDS-1. Either this or
--workspace need to be specified.

=back

=head1 VERSION

Version 0.1

=head1 AUTHOR

Michael Stapelberg, C<< <michael at i3wm.org> >>

=head1 LICENSE AND COPYRIGHT

Copyright 2013 Michael Stapelberg.

This program is free software; you can redistribute it and/or modify it
under the terms of the BSD license.

=cut