]> git.sur5r.net Git - i3/i3/blob - contrib/per-workspace-layout.pl
dump-asy.pl: use correct tmp dirname instead of hardcoded /tmp
[i3/i3] / contrib / per-workspace-layout.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2012 Michael Stapelberg
4 # Licensed under BSD license, see https://github.com/i3/i3/blob/next/LICENSE
5 #
6 # Append this line to your i3 config file:
7 #     exec_always ~/per-workspace-layout.pl
8 #
9 # Then, change the %layouts hash like you want your workspaces to be set up.
10 # This script requires i3 >= v4.4 for the extended workspace event.
11
12 use strict;
13 use warnings;
14 use AnyEvent;
15 use AnyEvent::I3;
16 use v5.10;
17
18 my %layouts = (
19     '4' => 'tabbed',
20     '5' => 'stacked',
21 );
22
23 my $i3 = i3();
24
25 die "Could not connect to i3: $!" unless $i3->connect->recv();
26
27 die "Could not subscribe to the workspace event: $!" unless
28     $i3->subscribe({
29         workspace => sub {
30             my ($msg) = @_;
31             return unless $msg->{change} eq 'focus';
32             die "Your version of i3 is too old. You need >= v4.4"
33                 unless exists($msg->{current});
34             my $ws = $msg->{current};
35
36             # If the workspace already has children, don’t change the layout.
37             return unless scalar @{$ws->{nodes}} == 0;
38
39             my $name = $ws->{name};
40             my $con_id = $ws->{id};
41
42             return unless exists $layouts{$name};
43
44             $i3->command(qq|[con_id="$con_id"] layout | . $layouts{$name});
45         },
46         _error => sub {
47             my ($msg) = @_;
48             say "AnyEvent::I3 error: $msg";
49             say "Exiting.";
50             exit 1;
51         },
52     })->recv->{success};
53
54 # Run forever.
55 AnyEvent->condvar->recv