2 # vim:ts=4:sw=4:expandtab
4 # © 2013-2014 Michael Stapelberg
6 # Requires perl ≥ v5.10, AnyEvent::I3 and JSON::XS
9 use warnings qw(FATAL utf8);
12 use POSIX qw(locale_h);
14 use File::Basename qw(basename);
15 use File::Temp qw(tempfile);
20 use List::Util qw(first);
23 use open ':encoding(UTF-8)';
25 binmode STDOUT, ':utf8';
26 binmode STDERR, ':utf8';
30 my $result = GetOptions(
31 'workspace=s' => \$workspace,
32 'output=s' => \$output,
34 say "i3-save-tree 0.1 © 2013 Michael Stapelberg";
38 pod2usage(-exitval => 0);
41 die "Could not parse command line options" unless $result;
43 if (!defined($workspace) && !defined($output)) {
44 die "One of --workspace or --output need to be specified";
47 unless (defined($workspace) ^ defined($output)) {
48 die "Only one of --workspace or --output can be specified";
52 if (!$i3->connect->recv) {
53 die "Could not connect to i3";
56 sub filter_containers {
57 my ($tree, $pred) = @_;
60 return $tree if $pred->();
62 for my $child (@{$tree->{nodes}}, @{$tree->{floating_nodes}}) {
63 my $result = filter_containers($child, $pred);
64 return $result if defined($result);
73 return $tree->{type} eq 'con' &&
74 @{$tree->{nodes}} == 0 &&
75 @{$tree->{floating_nodes}} == 0;
78 my %allowed_keys = map { ($_, 1) } qw(
93 sub strip_containers {
96 # layout is not relevant for a leaf container
97 delete $tree->{layout} if leaf_node($tree);
99 # fullscreen_mode conveys no state at all, it can either be 0 or 1 and the
100 # default is _always_ 0, so skip noop entries.
101 delete $tree->{fullscreen_mode} if $tree->{fullscreen_mode} == 0;
103 # names for non-leafs are auto-generated and useful only for i3 debugging
104 delete $tree->{name} unless leaf_node($tree);
106 delete $tree->{geometry} if zero_rect($tree->{geometry});
108 delete $tree->{current_border_width} if $tree->{current_border_width} == -1;
110 for my $key (keys %$tree) {
111 next if exists($allowed_keys{$key});
113 delete $tree->{$key};
116 for my $key (qw(nodes floating_nodes)) {
117 $tree->{$key} = [ map { strip_containers($_) } @{$tree->{$key}} ];
123 my $json_xs = JSON::XS->new->pretty(1)->allow_nonref->space_before(0)->canonical(1);
127 return $rect->{x} == 0 &&
129 $rect->{width} == 0 &&
130 $rect->{height} == 0;
133 # Dumps the containers in JSON, but with comments to explain the user what she
135 sub dump_containers {
136 my ($tree, $ws, $last) = @_;
144 if (!leaf_node($tree)) {
145 my $desc = $tree->{layout} . ' split container';
146 if ($tree->{type} ne 'con') {
147 $desc = $tree->{type};
149 say "$ws// $desc with " . @{$tree->{nodes}} . " children";
152 # Turn “window_properties” into “swallows” expressions, but only for leaf
153 # nodes. It only makes sense for leaf nodes to swallow anything.
154 if (leaf_node($tree)) {
156 for my $property (keys %{$tree->{window_properties}}) {
157 $swallows->{$property} = '^' . quotemeta($tree->{window_properties}->{$property}) . '$';
159 $tree->{swallows} = [ $swallows ];
161 delete $tree->{window_properties};
163 my @keys = sort keys %$tree;
164 for (0 .. (@keys-1)) {
166 # Those are handled recursively, not printed.
167 next if $key eq 'nodes' || $key eq 'floating_nodes';
169 # JSON::XS’s encode appends a newline
170 chomp(my $val = $json_xs->encode($tree->{$key}));
172 # Fix indentation. Keep in mind we are producing output to be
173 # read/modified by a human.
177 # Comment out all swallows criteria, they are just suggestions.
178 if ($key eq 'swallows') {
179 $val =~ s,^(\s*)\s{3}",\1// ",gm;
182 # Append a comma unless this is the last value.
183 # Ugly, but necessary so that we can print all values before recursing.
184 my $comma = ($_ == (@keys-1) &&
185 @{$tree->{nodes}} == 0 &&
186 @{$tree->{floating_nodes}} == 0 ? '' : ',');
187 say qq#$ws"$key": $val$comma#;
190 for my $key (qw(nodes floating_nodes)) {
191 my $num = scalar @{$tree->{$key}};
194 say qq#$ws"$key": [#;
195 for (0 .. ($num-1)) {
206 say $ws . ($last ? '}' : '},');
209 my $tree = $i3->get_tree->recv;
212 if (defined($workspace)) {
213 $dump = filter_containers($tree, sub {
214 $_->{type} eq 'workspace' && $_->{name} eq $workspace
217 $dump = filter_containers($tree, sub {
218 $_->{type} eq 'output' && $_->{name} eq $output
220 # Get the output’s content container (living beneath dockarea containers).
221 $dump = first { $_->{type} eq 'con' } @{$dump->{nodes}};
224 $dump = strip_containers($dump);
226 say "// vim:ts=4:sw=4:et";
227 for my $key (qw(nodes floating_nodes)) {
228 for (0 .. (@{$dump->{$key}} - 1)) {
229 dump_containers($dump->{$key}->[$_], undef, 1);
230 # Newlines separate containers so that one can use { and } in vim to
231 # jump out of the current container.
240 i3-save-tree - save (parts of) the layout tree for restoring
244 i3-save-tree [--workspace=name] [--output=name]
248 Dumps a workspace (or an entire output) to stdout. The data is supposed to be
249 edited a bit by a human, then later fed to i3 via the append_layout command.
251 The append_layout command will create placeholder windows, arranged in the
252 layout the input file specifies. Each container should have a swallows
253 specification. When a window is mapped (made visible on the screen) that
254 matches the specification, i3 will put it into that place and kill the
261 =item B<--workspace=name>
263 Specifies the workspace that should be dumped, e.g. 1. Either this or --output
264 need to be specified.
266 =item B<--output=name>
268 Specifies the output that should be dumped, e.g. LVDS-1. Either this or
269 --workspace need to be specified.
279 Michael Stapelberg, C<< <michael at i3wm.org> >>
281 =head1 LICENSE AND COPYRIGHT
283 Copyright 2013 Michael Stapelberg.
285 This program is free software; you can redistribute it and/or modify it
286 under the terms of the BSD license.