]> git.sur5r.net Git - i3/i3/blob - contrib/per-workspace-layout.pl
Merge pull request #3147 from walker0643/next
[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 use utf8;
18
19 my %layouts = (
20     '4' => 'tabbed',
21     '5' => 'stacked',
22 );
23
24 my $i3 = i3();
25
26 die "Could not connect to i3: $!" unless $i3->connect->recv();
27
28 die "Could not subscribe to the workspace event: $!" unless
29     $i3->subscribe({
30         workspace => sub {
31             my ($msg) = @_;
32             return unless $msg->{change} eq 'focus';
33             die "Your version of i3 is too old. You need >= v4.4"
34                 unless exists($msg->{current});
35             my $ws = $msg->{current};
36
37             # If the workspace already has children, don’t change the layout.
38             return unless scalar @{$ws->{nodes}} == 0;
39
40             my $name = $ws->{name};
41             my $con_id = $ws->{id};
42
43             return unless exists $layouts{$name};
44
45             $i3->command(qq|[con_id="$con_id"] layout | . $layouts{$name});
46         },
47         _error => sub {
48             my ($msg) = @_;
49             say "AnyEvent::I3 error: $msg";
50             say "Exiting.";
51             exit 1;
52         },
53     })->recv->{success};
54
55 # Run forever.
56 AnyEvent->condvar->recv