The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
0

How do i subscribe to i3-events using bash easily?

asked Apr 2 '15

gurkensalat gravatar image

updated Apr 26 '15

How do i subscribe to i3-events using bash easily?

1 answer

Sort by » oldest newest most voted
1

answered Apr 2 '15

gurkensalat gravatar image

updated May 25 '15

You can use the following perl script "i3subscribe" to subscribe to i3-events:

#!/usr/bin/env perl

BEGIN { $| = 1 } # flush \n

use strict;
use warnings;
use Data::Dumper;
use AnyEvent::I3;
use v5.10;

my $i3 = i3();
$i3->connect->recv or die "Error connecting to i3";

sub subscribe {
    my $ev = $_[0];
    my $dump = $_[1];
    if($i3->subscribe({
        $ev => sub {
            my ($msg) = @_;
            say "$ev:$msg->{'change'}";
            if($dump) {
                print Dumper($msg);
            }
        }
    })->recv->{success}) {
        say "Successfully subscribed to $ev-event";
    }
}

my $nextArg = shift;
if(!$nextArg) {
    say "Subscribe to i3-events";
    say "Usage:   $0 workspace|output|mode|window|barconfig_update|binding [dump]";
    say "Example: $0 workspace dump window binding dump";
    exit 1;
}
while($nextArg) {
    my $arg = $nextArg;
    $nextArg = shift;
    my $dump = 0;
    if($nextArg and $nextArg eq "dump") {
        $dump = 1;
        $nextArg = shift;
    }
    subscribe("$arg", $dump);
}
AE::cv->recv;

Example:

i3subscribe window workspace | while read -r event; do
...
done

Example use case: Different keyboard layout for every window

Comments

The link seems to be dead. Do you have some source repository for it (github, bitbucket, etc.)? If it is not to long you could also post it here entirely.

Adaephon gravatar imageAdaephon (Apr 24 '15)edit

updated my post, thank you

gurkensalat gravatar imagegurkensalat (Apr 26 '15)edit

Question Tools

Stats

Asked: Apr 2 '15

Seen: 443 times

Last updated: May 25